Skip to content

Commit

Permalink
Use crypto module to generate prefetch token
Browse files Browse the repository at this point in the history
These tokens don't need to be cryptographically secure (yet) but
eventually they will be so might as well use an actual hash function
to generate them.
  • Loading branch information
acdlite committed Nov 5, 2024
1 parent 0128d7c commit e4bcf3f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/next/src/server/app-render/collect-segment-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createFromReadableStream } from 'react-server-dom-webpack/client.edge'
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToReadableStream } from 'react-server-dom-webpack/server.edge'

import { createHash } from 'crypto'
import {
streamFromBuffer,
streamToBuffer,
Expand Down Expand Up @@ -432,7 +433,8 @@ function createSegmentAccessToken(
// performing dynamic navigations, to support auth checks in a layout that
// conditionally renders its slots. At that point we'll need to create an
// actual cryptographic hash with a salt.
return Buffer.from(parentSegmentPathStr + parallelRouteKey, 'utf-8')
.toString('hex')
.slice(0, 7)
return createHash('sha1')
.update(parentSegmentPathStr)
.update(parallelRouteKey)
.digest('hex')
}

0 comments on commit e4bcf3f

Please sign in to comment.