Skip to content

Commit 34b65d1

Browse files
committed
fix: resolve size_bytes type parsing and test AAD decryption issues
1 parent 5843448 commit 34b65d1

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/services/crypto/open-envelope.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ function str(value: unknown, field: string): string {
120120
return value;
121121
}
122122

123+
function num(value: unknown, field: string): number {
124+
if (typeof value === "number" && !Number.isNaN(value) && value >= 0) {
125+
return value;
126+
}
127+
if (typeof value === "string") {
128+
const parsed = Number(value);
129+
if (!Number.isNaN(parsed) && parsed >= 0) {
130+
return parsed;
131+
}
132+
}
133+
throw new OpenEnvelopeError(`missing or invalid ${field}`, "crypto_validation_error");
134+
}
135+
123136
/**
124137
* Open (decrypt) a sealed envelope.
125138
*
@@ -221,9 +234,7 @@ export async function openEnvelope(
221234
(a as { content_type?: unknown }).content_type,
222235
"attachment.content_type",
223236
),
224-
size_bytes: Number(
225-
str((a as { size_bytes?: unknown }).size_bytes, "attachment.size_bytes"),
226-
),
237+
size_bytes: num((a as { size_bytes?: unknown }).size_bytes, "attachment.size_bytes"),
227238
content_hash: str(
228239
(a as { content_hash?: unknown }).content_hash,
229240
"attachment.content_hash",

tests/unit/crypto/open-envelope.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type KeyProvider,
77
} from "../../../src/services/crypto/open-envelope";
88
import { createCommitment } from "../../../src/services/crypto/commitment";
9+
import { canonicalizeAttachmentDescriptors } from "../../../src/services/crypto/attachment-metadata";
910

1011
function toBase64(bytes: Uint8Array): string {
1112
let binary = "";
@@ -23,7 +24,14 @@ function toHex(bytes: Uint8Array): string {
2324
async function buildEnvelope(body: string, key: CryptoKey, recipient: string) {
2425
const iv = crypto.getRandomValues(new Uint8Array(12));
2526
const plaintext = new TextEncoder().encode(body);
26-
const ct = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, plaintext));
27+
const aad = canonicalizeAttachmentDescriptors([]);
28+
const ct = new Uint8Array(
29+
await crypto.subtle.encrypt(
30+
{ name: "AES-GCM", iv, additionalData: aad as BufferSource },
31+
key,
32+
plaintext,
33+
),
34+
);
2735
const tag = ct.slice(ct.length - 16);
2836
const ciphertextWithTag = ct; // GCM output already includes the tag
2937
const commitment = await createCommitment(ciphertextWithTag);

0 commit comments

Comments
 (0)