Skip to content

Commit dcf88ed

Browse files
gatsbybotpieh
andauthored
fix(gatsby-plugin-sharp): don't serve static assets that are not result of currently triggered deferred job (#37796) (#37802)
* fix(gatsby-plugin-sharp): don't serve static assets that are not result of currently triggered deferred job (#37796) * add tests * fix(gatsby-plugin-sharp): don't serve static assets that are not result of currently triggered deferred job (cherry picked from commit 6539860) * ignore browserlist update prompt * fix ignore --------- Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 3be4a80 commit dcf88ed

File tree

7 files changed

+25
-8
lines changed

7 files changed

+25
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this file shouldn't be allowed to be served

e2e-tests/development-runtime/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
"license": "MIT",
3333
"scripts": {
3434
"build": "gatsby build",
35-
"develop": "cross-env CYPRESS_SUPPORT=y ENABLE_GATSBY_REFRESH_ENDPOINT=true GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND=y gatsby develop",
35+
"develop": "cross-env CYPRESS_SUPPORT=y ENABLE_GATSBY_REFRESH_ENDPOINT=true GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND=y GATSBY_ENABLE_LAZY_IMAGES_IN_CI=y gatsby develop",
3636
"serve-static-files": "node ./serve-static-files.mjs",
3737
"serve": "gatsby serve",
3838
"clean": "gatsby clean",
3939
"typecheck": "tsc --noEmit",
4040
"start": "npm run develop",
4141
"format": "prettier --write \"src/**/*.js\"",
4242
"test": "npm run start-server-and-test || (npm run reset && exit 1)",
43+
"test:dir-traversel-access": "! curl -f http://localhost:8000/%2e%2e/SHOULD_NOT_SERVE",
4344
"posttest": "npm run reset",
4445
"reset": "node scripts/reset.js",
4546
"reset:preview": "curl -X POST http://localhost:8000/__refresh",
@@ -55,7 +56,7 @@
5556
"playwright:debug": "playwright test --project=chromium --debug",
5657
"start-server-and-test:playwright": "start-server-and-test develop http://localhost:8000 serve-static-files http://localhost:8888 playwright",
5758
"start-server-and-test:playwright-debug": "start-server-and-test develop http://localhost:8000 serve-static-files http://localhost:8888 playwright:debug",
58-
"combined": "npm run playwright && npm run cy:run",
59+
"combined": "npm run playwright && npm run cy:run && npm run test:dir-traversel-access",
5960
"postinstall": "playwright install chromium"
6061
},
6162
"devDependencies": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this file shouldn't be allowed to be served

e2e-tests/production-runtime/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"start": "npm run develop",
3737
"clean": "gatsby clean",
3838
"test": "npm run build && npm run start-server-and-test && npm run test-env-vars",
39+
"test:dir-traversel-access": "! curl -f http://localhost:9000/%2e%2e/SHOULD_NOT_SERVE",
3940
"test:offline": "npm run build:offline && yarn start-server-and-test:offline && npm run test-env-vars",
4041
"test-env-vars": " node __tests__/env-vars.js",
4142
"start-server-and-test": "start-server-and-test serve http://localhost:9000 serve-static-files http://localhost:8888 combined",
@@ -51,7 +52,7 @@
5152
"playwright:debug": "playwright test --project=chromium --debug",
5253
"start-server-and-test:playwright": "start-server-and-test serve http://localhost:9000 serve-static-files http://localhost:8888 playwright",
5354
"start-server-and-test:playwright-debug": "start-server-and-test serve http://localhost:9000 serve-static-files http://localhost:8888 playwright:debug",
54-
"combined": "npm run playwright && npm run cy:run",
55+
"combined": "npm run playwright && npm run cy:run && npm run test:dir-traversel-access",
5556
"postinstall": "playwright install chromium"
5657
},
5758
"devDependencies": {

packages/gatsby-plugin-sharp/src/gatsby-node.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ exports.onCreateDevServer = async ({ app, cache, reporter }) => {
3333
const decodedURI = decodeURIComponent(req.path)
3434
const pathOnDisk = path.resolve(path.join(`./public/`, decodedURI))
3535

36-
if (await pathExists(pathOnDisk)) {
37-
return res.sendFile(pathOnDisk)
38-
}
39-
4036
const jobContentDigest = await cache.get(decodedURI)
4137
const cacheResult = jobContentDigest
4238
? await cache.get(jobContentDigest)
4339
: null
4440

4541
if (!cacheResult) {
42+
// this handler is meant to handle lazy images only (images that were registered for
43+
// processing, but deffered to be processed only on request in develop server).
44+
// If we don't have cache result - it means that this is not lazy image or that
45+
// image was already handled in which case `express.static` handler (that is earlier
46+
// than this handler) should take care of handling request.
4647
return next()
4748
}
4849

@@ -64,6 +65,9 @@ exports.onCreateDevServer = async ({ app, cache, reporter }) => {
6465
await removeCachedValue(cache, jobContentDigest)
6566
}
6667

68+
// we reach this point only when this is a lazy image that we just processed
69+
// because `express.static` is earlier handler, we do have to manually serve
70+
// produced file for current request
6771
return res.sendFile(pathOnDisk)
6872
})
6973
}

packages/gatsby-plugin-sharp/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function createJob(job, { reporter }) {
149149
function lazyJobsEnabled() {
150150
return (
151151
process.env.gatsby_executing_command === `develop` &&
152-
!isCI() &&
152+
(!isCI() || process.env.GATSBY_ENABLE_LAZY_IMAGES_IN_CI) &&
153153
!(
154154
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `true` ||
155155
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `1`

packages/gatsby/cache-dir/__tests__/minimal-config.js

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ it(`Builds cache-dir with minimal config`, done => {
2525
})
2626

2727
spawn.on(`close`, function () {
28+
stderr = stderr
29+
.replace(`Browserslist: caniuse-lite is outdated. Please run:`, ``)
30+
.replace(`npx update-browserslist-db@latest`, ``)
31+
.replace(
32+
`Why you should do it regularly: https://github.com/browserslist/update-db#readme`,
33+
``
34+
)
35+
.trim()
36+
2837
expect(stderr).toEqual(``)
2938
expect(stdout).not.toEqual(``)
3039
done()

0 commit comments

Comments
 (0)