Unverified Commit 0ae76cbc authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Use crypto API to generate prefetch token (#72348)

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.

(Doing this in its own step because I saw some bundler errors when I
tried this in the last PR and didn't want that to block the rest of the
change)
parent 19f93c89
...@@ -83,11 +83,8 @@ export async function collectSegmentData( ...@@ -83,11 +83,8 @@ export async function collectSegmentData(
// A mutable map to collect the results as we traverse the route tree. // A mutable map to collect the results as we traverse the route tree.
const resultMap = new Map<string, Buffer>() const resultMap = new Map<string, Buffer>()
// A mutable array to collect the promises for each segment stream, so that
// they can run in parallel.
const collectedTasks: Array<Promise<void>> = []
const tree = collectSegmentDataImpl( const tree = await collectSegmentDataImpl(
flightRouterState, flightRouterState,
fullPageDataBuffer, fullPageDataBuffer,
clientModules, clientModules,
...@@ -95,15 +92,9 @@ export async function collectSegmentData( ...@@ -95,15 +92,9 @@ export async function collectSegmentData(
[], [],
'', '',
'', '',
resultMap, resultMap
collectedTasks
) )
// This will resolve either after a microtask (if none of the segments
// have dynamic data) or in the next tick (because of the abort signal passed
// to renderToReadableStream).
await Promise.all(collectedTasks)
// Render the route tree to a special `/_tree` segment. // Render the route tree to a special `/_tree` segment.
const treePrefetch: RootTreePrefetch = { const treePrefetch: RootTreePrefetch = {
tree, tree,
...@@ -117,7 +108,7 @@ export async function collectSegmentData( ...@@ -117,7 +108,7 @@ export async function collectSegmentData(
return resultMap return resultMap
} }
function collectSegmentDataImpl( async function collectSegmentDataImpl(
route: FlightRouterState, route: FlightRouterState,
fullPageDataBuffer: Buffer, fullPageDataBuffer: Buffer,
clientModules: ManifestNode, clientModules: ManifestNode,
...@@ -125,9 +116,8 @@ function collectSegmentDataImpl( ...@@ -125,9 +116,8 @@ function collectSegmentDataImpl(
segmentPath: Array<[string, Segment]>, segmentPath: Array<[string, Segment]>,
segmentPathStr: string, segmentPathStr: string,
accessToken: string, accessToken: string,
segmentBufferMap: Map<string, Buffer>, segmentBufferMap: Map<string, Buffer>
collectedTasks: Array<Promise<void>> ): Promise<TreePrefetch> {
): TreePrefetch {
// Metadata about the segment. Sent as part of the tree prefetch. Null if // Metadata about the segment. Sent as part of the tree prefetch. Null if
// there are no children. // there are no children.
let slotMetadata: { [parallelRouteKey: string]: TreePrefetch } | null = null let slotMetadata: { [parallelRouteKey: string]: TreePrefetch } | null = null
...@@ -148,12 +138,12 @@ function collectSegmentDataImpl( ...@@ -148,12 +138,12 @@ function collectSegmentDataImpl(
'/' + '/' +
encodeChildSegmentAsFilesystemSafePathname(parallelRouteKey, childSegment) encodeChildSegmentAsFilesystemSafePathname(parallelRouteKey, childSegment)
// Create an access token for each child slot // Create an access token for each child slot.
const childAccessToken = createSegmentAccessToken( const childAccessToken = await createSegmentAccessToken(
segmentPathStr, segmentPathStr,
parallelRouteKey parallelRouteKey
) )
const childTree = collectSegmentDataImpl( const childTree = await collectSegmentDataImpl(
childRoute, childRoute,
fullPageDataBuffer, fullPageDataBuffer,
clientModules, clientModules,
...@@ -161,8 +151,7 @@ function collectSegmentDataImpl( ...@@ -161,8 +151,7 @@ function collectSegmentDataImpl(
childSegmentPath, childSegmentPath,
childSegmentPathStr, childSegmentPathStr,
childAccessToken, childAccessToken,
segmentBufferMap, segmentBufferMap
collectedTasks
) )
if (slotMetadata === null) { if (slotMetadata === null) {
slotMetadata = {} slotMetadata = {}
...@@ -175,18 +164,16 @@ function collectSegmentDataImpl( ...@@ -175,18 +164,16 @@ function collectSegmentDataImpl(
childAccessTokens[parallelRouteKey] = childAccessToken childAccessTokens[parallelRouteKey] = childAccessToken
} }
// Spawn a task to render the segment data to a stream. // Render the segment data to a stream.
collectedTasks.push( await renderSegmentDataToStream(
renderSegmentDataToStream( fullPageDataBuffer,
fullPageDataBuffer, clientModules,
clientModules, serverConsumerManifest,
serverConsumerManifest, segmentPath,
segmentPath, segmentPathStr,
segmentPathStr, accessToken,
accessToken, childAccessTokens,
childAccessTokens, segmentBufferMap
segmentBufferMap
)
) )
// Metadata about the segment. Sent to the client as part of the // Metadata about the segment. Sent to the client as part of the
...@@ -414,10 +401,10 @@ function encodeParamValue(segment: string): string { ...@@ -414,10 +401,10 @@ function encodeParamValue(segment: string): string {
return '$' + Buffer.from(segment, 'utf-8').toString('base64url') return '$' + Buffer.from(segment, 'utf-8').toString('base64url')
} }
function createSegmentAccessToken( async function createSegmentAccessToken(
parentSegmentPathStr: string, parentSegmentPathStr: string,
parallelRouteKey: string parallelRouteKey: string
): string { ): Promise<string> {
// Create an access token that the client passes when requesting a segment. // Create an access token that the client passes when requesting a segment.
// The token is sent to the client as part of the parent layout's data. // The token is sent to the client as part of the parent layout's data.
// //
...@@ -430,9 +417,20 @@ function createSegmentAccessToken( ...@@ -430,9 +417,20 @@ function createSegmentAccessToken(
// TODO: Because this only affects prefetches, this doesn't need to be secure. // TODO: Because this only affects prefetches, this doesn't need to be secure.
// It's just for obfuscation. But eventually we will use this technique when // It's just for obfuscation. But eventually we will use this technique when
// performing dynamic navigations, to support auth checks in a layout that // performing dynamic navigations, to support auth checks in a layout that
// conditionally renders its slots. At that point we'll need to create an // conditionally renders its slots. At that point we'll need to add a salt.
// actual cryptographic hash with a salt.
return Buffer.from(parentSegmentPathStr + parallelRouteKey, 'utf-8') // Encode the inputs as Uint8Array
.toString('hex') const encoder = new TextEncoder()
.slice(0, 7) const data = encoder.encode(parentSegmentPathStr + parallelRouteKey)
// Use the Web Crypto API to generate a SHA-256 hash.
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
// Convert the ArrayBuffer to a hex string
const hashArray = new Uint8Array(hashBuffer)
const hashHex = Array.from(hashArray)
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('')
return hashHex
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment