Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): broken link checker should not report false positives when using encoded chars #4407

Merged
merged 1 commit into from
Mar 12, 2021
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
50 changes: 50 additions & 0 deletions packages/docusaurus/src/server/__tests__/brokenLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,54 @@ describe('brokenLinks', () => {
});
expect(result).toEqual(allCollectedLinksFiltered);
});

describe('Encoded link', () => {
test('getAllBrokenLinks', async () => {
const routes: RouteConfig[] = [
{
path: '/docs',
component: '',
routes: [
{path: '/docs/some doc', component: ''},
{path: '/docs/some other doc', component: ''},
{path: '/docs/weird%20file%20name', component: ''},
],
},
{
path: '*',
component: '',
},
];

const allCollectedLinks = {
'/docs/some doc': [
// good - valid file with spaces in name
'./some%20other%20doc',
// good - valid file with percent-20 in its name
'./weird%20file%20name',
// bad - non-existant file with spaces in name
'./some%20other%20non-existant%20doc',
// evil - trying to use ../../ but '/' won't get decoded
'./break%2F..%2F..%2Fout',
],
};

const expectedBrokenLinks = {
'/docs/some doc': [
{
link: './some%20other%20non-existant%20doc',
resolvedLink: '/docs/some%20other%20non-existant%20doc',
},
{
link: './break%2F..%2F..%2Fout',
resolvedLink: '/docs/break%2F..%2F..%2Fout',
},
],
};

expect(getAllBrokenLinks({allCollectedLinks, routes})).toEqual(
expectedBrokenLinks,
);
});
});
});
4 changes: 3 additions & 1 deletion packages/docusaurus/src/server/brokenLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function getPageBrokenLinks({
}

function isBrokenLink(link: string) {
const matchedRoutes = matchRoutes(toReactRouterRoutes(routes), link);
const matchedRoutes = [link, decodeURI(link)]
.map((l) => matchRoutes(toReactRouterRoutes(routes), l))
.reduce((prev, cur) => prev.concat(cur));
return matchedRoutes.length === 0;
}

Expand Down