Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
Open
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/dirty-pears-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hashicorp/react-docs-page': minor
---

catches invalid request and 404s
14 changes: 14 additions & 0 deletions packages/docs-page/server/__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)
})
})
17 changes: 17 additions & 0 deletions packages/docs-page/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ContentApiError } from '../content-api'
import FileSystemLoader from './loaders/file-system'
import RemoteContentLoader from './loaders/remote-content'
import { DataLoader } from './loaders/types'
import { isInvalidURI } from './is-invalid-uri'
import { DEFAULT_PARAM_ID } from './consts'

// We currently export most utilities individually,
// since we have cases such as Packer remote plugin docs
Expand Down Expand Up @@ -67,6 +69,21 @@ export function getStaticGenerationFunctions(
}
},
getStaticProps: async (ctx) => {
const pathParams = ctx.params?.[opts.paramId || DEFAULT_PARAM_ID]

if (pathParams) {
if (Array.isArray(pathParams)) {
const path = pathParams.join('/')
if (isInvalidURI(path)) {
return { notFound: true }
}
} else if (typeof pathParams === 'string') {
if (isInvalidURI(pathParams)) {
return { notFound: true }
}
}
}

try {
const props = await loader.loadStaticProps(ctx)
return {
Expand Down
12 changes: 12 additions & 0 deletions packages/docs-page/server/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: any) {
console.warn(err.message, uri)
return true
}
}