Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-ideas-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Fix an issue where content refs to spaces outside of site was resolving with share links
40 changes: 40 additions & 0 deletions packages/gitbook/e2e/internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down
18 changes: 16 additions & 2 deletions packages/gitbook/src/lib/references.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -381,7 +396,6 @@ async function resolveContentRefInSpace(
context.structure,
(siteSpace) => siteSpace.space.id === spaceId
);

return (
foundSiteSpace?.siteSpace.title ??
foundSiteSpace?.siteSection?.title ??
Expand Down