Skip to content

Commit f0dd1f9

Browse files
authored
repo sync
2 parents 31e555d + 314a8ac commit f0dd1f9

3 files changed

Lines changed: 12 additions & 52 deletions

File tree

middleware/csp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const { contentSecurityPolicy } = require('helmet')
55
const isArchivedVersion = require('../lib/is-archived-version')
66
const versionSatisfiesRange = require('../lib/version-satisfies-range')
7+
const AZURE_STORAGE_URL = 'githubdocs.azureedge.net'
78

89
// module.exports = contentSecurityPolicy({
910
module.exports = async (req, res, next) => {
@@ -18,13 +19,15 @@ module.exports = async (req, res, next) => {
1819
fontSrc: [
1920
"'self'",
2021
'data:',
21-
'github-images.s3.amazonaws.com'
22+
'github-images.s3.amazonaws.com',
23+
AZURE_STORAGE_URL
2224
],
2325
imgSrc: [
2426
"'self'",
2527
'data:',
2628
'github.githubassets.com',
2729
'github-images.s3.amazonaws.com',
30+
AZURE_STORAGE_URL,
2831
'placehold.it',
2932
'*.githubusercontent.com',
3033
'github.com'
Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,8 @@
1-
const FASTLY_TTL = process.env.FASTLY_TTL || String(60 * 60 * 24) // 24 hours
2-
const STALE_TTL = String(60 * 10) // 10 minutes
3-
41
module.exports = (req, res, next) => {
5-
const BYPASS_FASTLY = process.env.TEST_BYPASS_FASTLY === 'true'
6-
7-
// Bypass Fastly caching for all rendered pages
8-
if (BYPASS_FASTLY) {
9-
// Disallow both Fastly AND the browser from caching HTML pages
10-
res.set({
11-
'surrogate-control': 'private, no-store',
12-
'cache-control': 'private, no-store'
13-
})
14-
return next()
15-
}
16-
2+
// Disallow both Fastly AND the browser from caching HTML pages
173
res.set({
18-
19-
// Say you want Fastly to cache your content but you don't want it cached by browsers.
20-
// The best way to do this would be to send Fastly both the Cache-Control header as you want
21-
// it to go to the browsers, and use Surrogate-Control to tell us how long to cache for.
22-
23-
// Fastly does not currently respect no-store or no-cache directives.
24-
// Including either or both of these in a Cache-Control header has no effect on
25-
// Fastly's caching decision, unless you alter this behavior using custom VCL.
26-
27-
// https://docs.fastly.com/en/guides/configuring-caching
28-
'cache-control': 'no-store, must-revalidate',
29-
30-
// This header gets stripped and is only visible to Fastly caches.
31-
// https://docs.fastly.com/en/guides/serving-stale-content#manually-enabling-serve-stale
32-
'surrogate-control': `max-age=${FASTLY_TTL}, stale-if-error=${STALE_TTL}, stale-while-revalidate=${STALE_TTL}`,
33-
34-
// Fastly provides a Soft Purge feature that allows you to mark content as outdated (stale) instead of permanently
35-
// purging and thereby deleting it from Fastly's caches. Objects invalidated with Soft Purge will be treated as
36-
// outdated (stale) while Fastly fetches a new version from origin.
37-
//
38-
// Use of a surrogate key is required for soft purging
39-
// https://docs.fastly.com/en/guides/soft-purges
40-
// https://docs.fastly.com/en/guides/getting-started-with-surrogate-keys
41-
'surrogate-key': 'all-the-things'
4+
'surrogate-control': 'private, no-store',
5+
'cache-control': 'private, no-store'
426
})
43-
44-
next()
7+
return next()
458
}

tests/rendering/server.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { describeViaActionsOnly } = require('../helpers/conditional-runs')
55
const path = require('path')
66
const { loadPages } = require('../../lib/pages')
77
const builtAssets = require('../../lib/built-asset-urls')
8+
const AZURE_STORAGE_URL = 'githubdocs.azureedge.net'
89

910
describe('server', () => {
1011
jest.setTimeout(60 * 1000)
@@ -45,12 +46,14 @@ describe('server', () => {
4546

4647
expect(csp.get('font-src').includes("'self'")).toBe(true)
4748
expect(csp.get('font-src').includes('github-images.s3.amazonaws.com')).toBe(true)
49+
expect(csp.get('font-src').includes(AZURE_STORAGE_URL)).toBe(true)
4850

4951
expect(csp.get('connect-src').includes("'self'")).toBe(true)
5052
expect(csp.get('connect-src').includes('*.algolia.net')).toBe(true)
5153
expect(csp.get('connect-src').includes('*.algolianet.com')).toBe(true)
5254

5355
expect(csp.get('img-src').includes("'self'")).toBe(true)
56+
expect(csp.get('img-src').includes(AZURE_STORAGE_URL)).toBe(true)
5457
expect(csp.get('img-src').includes('github-images.s3.amazonaws.com')).toBe(true)
5558

5659
expect(csp.get('script-src').includes("'self'")).toBe(true)
@@ -59,16 +62,7 @@ describe('server', () => {
5962
expect(csp.get('style-src').includes("'unsafe-inline'")).toBe(true)
6063
})
6164

62-
test('sets Fastly cache control headers', async () => {
63-
const res = await get('/en')
64-
expect(res.headers['cache-control']).toBe('no-store, must-revalidate')
65-
expect(res.headers['surrogate-control']).toBe('max-age=86400, stale-if-error=600, stale-while-revalidate=600')
66-
expect(res.headers['surrogate-key']).toBe('all-the-things')
67-
})
68-
69-
test('sets Fastly cache control headers to bypass if enabled', async () => {
70-
process.env.TEST_BYPASS_FASTLY = 'true'
71-
65+
test('sets Fastly cache control headers to bypass pages', async () => {
7266
const res = await get('/en')
7367
expect(res.headers['cache-control']).toBe('private, no-store')
7468
expect(res.headers['surrogate-control']).toBe('private, no-store')

0 commit comments

Comments
 (0)