Skip to content

Commit

Permalink
fix(gatsby): Check rawPath in loadPage (#37451)
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
LekoArts committed Jan 12, 2023
1 parent 3ef4f44 commit 4a721df
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/gatsby/cache-dir/loader.js
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

0 comments on commit 4a721df

Please sign in to comment.