Skip to content
Closed
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
9 changes: 9 additions & 0 deletions src/layouts/sidebar-sidecar/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
generateTopLevelSidebarNavData,
} from 'components/sidebar/helpers'

import { isInvalidURI } from './utils/is-invalid-uri'

/**
* @TODO update the basePaths inside of `src/data/${productSLug}.json` files to
* be arrays of objects that look like:
Expand Down Expand Up @@ -118,6 +120,13 @@ export function getStaticGenerationFunctions<
const pathParts = (ctx.params.page || []) as string[]
const headings = [] // populated by anchorLinks plugin below

// catch invalid URIs early
if (isInvalidURI(pathParts.join('/'))) {
return {
notFound: true,
}
}
Comment on lines +123 to +128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’­ Can we expand on the approach / heuristic here for how we determine an invalid URL / why we are doing this? I think that will be a helpful breadcrumb for the future!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thinking about this more, i'm curious the reasoning behind catching only 404's and returning notFound: true...

if (error.status === 404) {
return { notFound: true }
}

This class of bad requests cause our Content API to return a Vercel 400, so maybe it'd be simpler to include that in the detected status codes


const loader = getLoader({
mainBranch,
remarkPlugins: [
Expand Down
14 changes: 14 additions & 0 deletions src/layouts/sidebar-sidecar/utils/__tests__/is-invalid-uri.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isInvalidURI } from '../is-invalid-uri'

describe('isInvalidURI', () => {
it.each([
['/docs/upgrade', false],
['foo/bar%23anchor', false],
[
"/docs/upgrade%25'%20AND%202*3*8=6*8%20AND%20'zVVl'!='zVVl%25/upgrade-specific",
true,
],
])('given `%s`, returns `%s`', (a, expected) => {
expect(isInvalidURI(a)).toBe(expected)
})
})
12 changes: 12 additions & 0 deletions src/layouts/sidebar-sidecar/utils/is-invalid-uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** matches any whitespace or % */
const RE = /(\s|%)/gi
/** decodes a URI once, and returns if it is invalid */
export const isInvalidURI = (uri: string) => {
try {
const res = decodeURIComponent(uri)
return !!res.match(RE)
} catch (err) {
console.warn(err.message, uri)
return true
}
}