Skip to content

Commit 2a2e218

Browse files
Refactor createSignaturePayload to conditionally exclude content-type header based on request body presence
1 parent dd61bf0 commit 2a2e218

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

core/security/src/lib/crypto/utils.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ export function createSignaturePayload(request: {
163163
}
164164
}
165165

166+
// Normalize body: undefined/null -> {} to match client behavior
167+
const normalizedBody =
168+
request.body !== undefined && request.body !== null ? request.body : {};
169+
170+
// Client only includes content-type in signed payload when request has a body (hasBody).
171+
// For PATCH/POST with body {} or no body, client does not sign content-type; exclude it here too.
172+
const hasBody =
173+
normalizedBody !== undefined &&
174+
normalizedBody !== null &&
175+
(typeof normalizedBody !== "object" ||
176+
Object.keys(normalizedBody).length > 0);
177+
if (!hasBody) {
178+
delete otherHeaders["content-type"];
179+
}
180+
166181
// Build headers object with keys in sorted order so JSON string is deterministic
167182
// (client and server must produce identical payload string for signature verification)
168183
const sortedHeaderKeys = Object.keys(otherHeaders).sort();
@@ -171,10 +186,6 @@ export function createSignaturePayload(request: {
171186
canonicalHeaders[k] = otherHeaders[k];
172187
}
173188

174-
// Normalize body: undefined/null -> {} to match client behavior
175-
const normalizedBody =
176-
request.body !== undefined && request.body !== null ? request.body : {};
177-
178189
// Debug: raw values going into canonical payload
179190
log.info("createSignaturePayload input (server, pre-payload)", {
180191
method: request.method,

0 commit comments

Comments
 (0)