Skip to content

Commit d30802a

Browse files
committed
get nonce out of express and into RR
1 parent 97c7611 commit d30802a

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

app/entry.server.tsx

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import crypto from 'node:crypto'
12
import { PassThrough } from 'node:stream'
23
import { styleText } from 'node:util'
34
import { contentSecurity } from '@nichtsam/helmet/content'
@@ -26,13 +27,8 @@ const MODE = process.env.NODE_ENV ?? 'development'
2627
type DocRequestArgs = Parameters<HandleDocumentRequestFunction>
2728

2829
export default async function handleRequest(...args: DocRequestArgs) {
29-
const [
30-
request,
31-
responseStatusCode,
32-
responseHeaders,
33-
reactRouterContext,
34-
loadContext,
35-
] = args
30+
const [request, responseStatusCode, responseHeaders, reactRouterContext] =
31+
args
3632
const { currentInstance, primaryInstance } = await getInstanceInfo()
3733
responseHeaders.set('fly-region', process.env.FLY_REGION ?? 'unknown')
3834
responseHeaders.set('fly-app', process.env.FLY_APP_NAME ?? 'unknown')
@@ -47,7 +43,7 @@ export default async function handleRequest(...args: DocRequestArgs) {
4743
? 'onAllReady'
4844
: 'onShellReady'
4945

50-
const nonce = loadContext.cspNonce?.toString() ?? ''
46+
const nonce = crypto.randomBytes(16).toString('hex')
5147
return new Promise(async (resolve, reject) => {
5248
let didError = false
5349
// NOTE: this timing will only include things that are rendered in the shell

server/index.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import crypto from 'node:crypto'
21
import { styleText } from 'node:util'
32
import { helmet } from '@nichtsam/helmet/node-http'
43
import { createRequestHandler } from '@react-router/express'
@@ -110,11 +109,6 @@ app.use(
110109
}),
111110
)
112111

113-
app.use((_, res, next) => {
114-
res.locals.cspNonce = crypto.randomBytes(16).toString('hex')
115-
next()
116-
})
117-
118112
// When running tests or running in development, we want to effectively disable
119113
// rate limiting because playwright tests are very fast and we don't want to
120114
// have to wait for the rate limit to reset between tests.
@@ -201,10 +195,7 @@ if (!ALLOW_INDEXING) {
201195
app.all(
202196
'*',
203197
createRequestHandler({
204-
getLoadContext: (_: any, res: any) => ({
205-
cspNonce: res.locals.cspNonce,
206-
serverBuild: getBuild(),
207-
}),
198+
getLoadContext: () => ({ serverBuild: getBuild() }),
208199
mode: MODE,
209200
build: async () => {
210201
const { error, build } = await getBuild()

0 commit comments

Comments
 (0)