Skip to content

Commit 8cd9f32

Browse files
committed
fix(sdk/js): handle invalid hex in appId and remove stale browser-compat test
- hexToBytes now validates hex format (even length, valid chars, 0x/0X prefix) and returns null instead of producing corrupt bytes or throwing on malformed input. - Both verifyEnvEncryptPublicKey and verifyEnvEncryptPublicKeyLegacy return null when appId is not valid hex, preserving the null-on-error contract. - Remove browser-compatibility.test.ts which imported deleted .browser modules (encrypt-env-vars.browser, get-compose-hash.browser, verify-env-encrypt-public-key.browser).
1 parent 85afdc1 commit 8cd9f32

2 files changed

Lines changed: 9 additions & 258 deletions

File tree

sdk/js/src/__tests__/browser-compatibility.test.ts

Lines changed: 0 additions & 254 deletions
This file was deleted.

sdk/js/src/verify-env-encrypt-public-key.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ function bigintToBeBytes(value: bigint, length: number): Uint8Array {
2121
return bytes
2222
}
2323

24-
function hexToBytes(hex: string): Uint8Array {
25-
if (hex.startsWith("0x")) hex = hex.slice(2)
24+
function hexToBytes(hex: string): Uint8Array | null {
25+
if (hex.startsWith("0x") || hex.startsWith("0X")) hex = hex.slice(2)
26+
if (hex.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(hex)) return null
2627
const bytes = new Uint8Array(hex.length / 2)
2728
for (let i = 0; i < hex.length; i += 2) {
2829
bytes[i / 2] = parseInt(hex.substr(i, 2), 16)
@@ -90,9 +91,11 @@ export function verifyEnvEncryptPublicKey(
9091
return null
9192
}
9293

94+
const appIdBytes = hexToBytes(appId)
95+
if (!appIdBytes) return null
96+
9397
const prefix = new TextEncoder().encode("dstack-env-encrypt-pubkey")
9498
const separator = new TextEncoder().encode(":")
95-
const appIdBytes = hexToBytes(appId)
9699
const timestampBytes = bigintToBeBytes(ts, 8)
97100
const message = concat(
98101
prefix,
@@ -115,9 +118,11 @@ export function verifyEnvEncryptPublicKeyLegacy(
115118
): string | null {
116119
if (signature.length !== 65) return null
117120

121+
const appIdBytes = hexToBytes(appId)
122+
if (!appIdBytes) return null
123+
118124
const prefix = new TextEncoder().encode("dstack-env-encrypt-pubkey")
119125
const separator = new TextEncoder().encode(":")
120-
const appIdBytes = hexToBytes(appId)
121126
const message = concat(prefix, separator, appIdBytes, publicKey)
122127
return recoverSigner(keccak_256(message), signature)
123128
}

0 commit comments

Comments
 (0)