Skip to content

Commit a56d811

Browse files
authored
Merge branch 'develop' into fixing-e2e-dnt
2 parents e810f58 + fa067c1 commit a56d811

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/pwa-kit-runtime/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## v3.9.0-dev (Oct 29, 2024)
2+
- Fix stale service worker file that could cause requests to still use old Content-Security-Policy [#2191](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2191)
23

34
## v3.8.0 (Oct 28, 2024)
45
- Add proxy handling for trusted agent on behalf of (TAOB) requests [#2077](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2077)

packages/pwa-kit-runtime/src/ssr/server/build-remote-server.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
SET_COOKIE,
1313
CACHE_CONTROL,
1414
NO_CACHE,
15-
X_ENCODED_HEADERS
15+
X_ENCODED_HEADERS,
16+
CONTENT_SECURITY_POLICY
1617
} from './constants'
1718
import {
1819
catchAndLog,
@@ -911,8 +912,27 @@ export const RemoteServerFactory = {
911912

912913
const content = fs.readFileSync(workerFilePath, {encoding: 'utf8'})
913914

915+
// If the service worker is not updated when content security policy headers inside
916+
// ssr.js are changed, then service worker initiated requests will continue to use
917+
// the old CSP headers.
918+
//
919+
// This is problematic in stacked CDN setups where an old service worker with
920+
// old CSPs can remain cached if the content of the service worker itself is not changed.
921+
//
922+
// To ensure the service worker is refetched when CSPs are changed, we factor in
923+
// the CSP headers when generating the Etag.
924+
//
925+
// See https://gus.lightning.force.com/lightning/r/ADM_Work__c/a07EE000025yeu9YAA/view
926+
// and https://salesforce-internal.slack.com/archives/C01GLHLBPT5/p1730739370922629
927+
// for more details.
928+
929+
const contentSecurityPolicyHeader = res.getHeaders()[CONTENT_SECURITY_POLICY] || ''
930+
914931
// Serve the file, with a strong ETag
915-
res.set('etag', getHashForString(content))
932+
// For this to be a valid ETag, the string must be placed between ""
933+
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag#etag_value for
934+
// more details
935+
res.set('etag', `"${getHashForString(content + contentSecurityPolicyHeader)}"`)
916936
res.set(CONTENT_TYPE, 'application/javascript')
917937
res.send(content)
918938
},

0 commit comments

Comments
 (0)