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(gatsby): Check rawPath in loadPage #37451

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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
12 changes: 12 additions & 0 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const createPageDataUrl = rawPath => {
}`
}

/**
* Utility to check the path that goes into doFetch for e.g. potential malicious intentions.
* It checks for "//" because with this you could do a fetch request to a different domain.
*/
const shouldAbortFetch = rawPath => rawPath.startsWith(`//`)

function doFetch(url, method = `GET`) {
return new Promise(resolve => {
const req = new XMLHttpRequest()
Expand Down Expand Up @@ -876,6 +882,9 @@ export class ProdLoader extends BaseLoader {
loadPageDataJson(rawPath) {
return super.loadPageDataJson(rawPath).then(data => {
if (data.notFound) {
if (shouldAbortFetch(rawPath)) {
return data
}
// check if html file exist using HEAD request:
// if it does we should navigate to it instead of showing 404
return doFetch(rawPath, `HEAD`).then(req => {
Expand All @@ -900,6 +909,9 @@ export class ProdLoader extends BaseLoader {
loadPartialHydrationJson(rawPath) {
return super.loadPartialHydrationJson(rawPath).then(data => {
if (data.notFound) {
if (shouldAbortFetch(rawPath)) {
return data
}
// check if html file exist using HEAD request:
// if it does we should navigate to it instead of showing 404
return doFetch(rawPath, `HEAD`).then(req => {
Expand Down