Fix an issue where content refs to spaces outside of site was resolving with share links (#3889)

This commit is contained in:
Taran Vohra
2026-01-07 11:59:02 +05:30
committed by GitHub
parent 7b7231602f
commit ebf3b2beb7
3 changed files with 61 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Fix an issue where content refs to spaces outside of site was resolving with share links
+40
View File
@@ -1318,6 +1318,46 @@ const testCases: TestsCase[] = [
},
],
},
{
name: 'Navigation of links on a share links site',
contentBaseURL: 'https://gitbook-open-e2e-sites.gitbook.io/api-multi-versions-share-links/',
tests: [
{
name: 'Link in same site has the share link token preserved',
url: '8tNo6MeXg7CkFMzSSz81/link-in-same-site',
run: async (page) => {
const linkToDifferentPage = page.getByRole('link', { name: 'Other Page' });
await linkToDifferentPage.click();
await page.waitForURL((url) =>
url.pathname.includes(
'/api-multi-versions-share-links/8tNo6MeXg7CkFMzSSz81/3.0/other-page'
)
);
await expect(
page.getByRole('heading', { level: 1, name: 'Other Page' })
).toBeVisible();
},
screenshot: false,
},
{
name: 'Link to different site should not have the share link token preserved',
url: '8tNo6MeXg7CkFMzSSz81/link-in-different-site',
run: async (page) => {
const linkToDifferentSite = page.getByRole('link', { name: 'Basics' });
await linkToDifferentSite.click();
await page.waitForURL(
(url) =>
url.toString() ===
'https://gitbook-open-e2e-sites.gitbook.io/sections/sections-4/basics/editor'
);
await expect(
page.getByRole('heading', { level: 1, name: 'Editor' })
).toBeVisible();
},
screenshot: false,
},
],
},
{
name: 'Visitor Auth - Space',
contentBaseURL: 'https://gitbook.gitbook.io/gbo-va-space/',
+16 -2
View File
@@ -362,7 +362,22 @@ async function resolveContentRefInSpace(
contentRef: ContentRef,
options: ResolveContentRefOptions = {}
) {
const ctx = await createContextForSpace(spaceId, context);
const ctx = await createContextForSpace(spaceId, {
...context,
shareKey: (() => {
// If the space is found in the current site, we use the current share key to generate links.
if ('site' in context) {
return findSiteSpaceBy(
context.structure,
(siteSpace) => siteSpace.space.id === spaceId
)
? context.shareKey
: undefined;
}
return context.space.id === spaceId ? context.shareKey : undefined;
})(),
});
if (!ctx) {
return null;
@@ -381,7 +396,6 @@ async function resolveContentRefInSpace(
context.structure,
(siteSpace) => siteSpace.space.id === spaceId
);
return (
foundSiteSpace?.siteSpace.title ??
foundSiteSpace?.siteSection?.title ??