Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify iv prefixing of Server Actions encryption #57274

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/next/src/server/app-render/action-encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ async function decodeActionBoundArg(actionId: string, arg: string) {
)
}

const [ivPrefix, payload] = arg.split(':', 2)
// Get the payload and iv from the arg. 18 bytes * 8/6 = 24 chars in base64.
const ivPrefix = arg.slice(0, 24)
const payload = arg.slice(24)
if (payload === undefined) {
throw new Error('Invalid Server Action payload.')
}
Expand All @@ -56,7 +58,7 @@ async function encodeActionBoundArg(actionId: string, arg: string) {
}

// Get some random bytes for iv.
const randomBytes = new Uint16Array(8)
const randomBytes = new Uint8Array(18)
crypto.getRandomValues(randomBytes)
const ivPrefix = btoa(arrayBufferToString(randomBytes.buffer))

Expand All @@ -65,7 +67,7 @@ async function encodeActionBoundArg(actionId: string, arg: string) {
SALT_PREFIX + ivPrefix + actionId,
stringToUint8Array(arg)
)
return ivPrefix + ':' + btoa(arrayBufferToString(encoded))
return ivPrefix + btoa(arrayBufferToString(encoded))
}

// Encrypts the action's bound args into a string.
Expand Down
Loading