1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154 |
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x47
x47
x47
x47
x2
x47
x2
x47
x2
x47
x2
x47
x2
x47
x37
x47
x47
x50
x50
x50
x50
x49
x3
x3
x1
x1
x49
x3
x3
x1
x1
x49
x3
x3
x1
x1
x49
x5
x5
x5
x1
x1
x1
x50
x181
x181
x109
x109
x72
x181
x135
x135
x135
x135
x135
x135
x135
x135
x1
x1
x1
x135
x1
x1
x1
x135
x2
x2
x2
x135
x7
x1
x1
x1
x7
x1
x1
x1
x5
x5
x7
x7
x7
x7
x135
x97
x97
x27
x135
x3
x3
x3
x3
x3
x3
x3
x3
x3
x3
x97
x97
x97
x97
x97
x97
x89
x1
x1
x88
x88
x93
x7
x1
x1
x6
x6
x1
x97
x88
x88
x88
x88
x88
x88
x88
x35
x35
x86
x20
x88
x1
x88
x1
x88
x5
x88
x2
x88
x6
x1
x1
x1
x5
x5
x5
x6
x6
x6
x6
x1
x1
x1
x6
x1
x1
x1
x3
x3
x88
x88
x3
x3
x3
x3
x3
x27
x27
x27
x27
x27
x27
x1
x1
x27
x6
x6
x27
x6
x6
x27
x3
x3
x11
x27
x6
x6
x6
x6
x6
x6
x6
x3
x3
x6
x1
x1
x1
x1
x1
x6
x6
x6
x6
x6
x6
x6
x6
x6
x1
x1
x1
x1
x5
x6
x1
x1
x1
x6
x3
x3
x1
x1
x6
x3
x3
x3
x5
x5
x3
x3
x3
x135
x135
x135
x135
x135
x135
x135
x135
x135
x135
x135
x90
x90
x90
x90
x159
x159
x159
x159
x5
x5
x159
x159
x159
x4
x4
x159
x90
x90
x90
x88
x88
x90
x5
x5
x90
x3
x3
x90
x3
x3
x90
x78
x78
x90
x3
x3
x90
x90
x90
x90
x90
x90
x90
x180
x180
x180
x180
x90
x90
x3
x3
x81
x81
x81
x81
x81
x81
x81
x133
x1
x1
x1
x133
x1
x1
x1
x131
x133
x1
x1
x1
x130
x130
x130
x130
x130
x130
x130
x130
x130
x62
x62
x62
x81
x41
x41
x2
x2
x2
x41
x41
x41
x1
x1
x1
x41
x38
x38
x41
x3
x3
x41
x41
x41
x1
x1
x1
x41
x3
x3
x41
x41
x41
x41
x41
x41
x41
x28
x28
x28
x28
x28
x28
x28
x28
x28
x28
x28
x28
x28
x28
x41
x56
x56
x56
x2
x56
x54
x54
x56
x1
x37
x37
x71
x71
x6
x6
x6
x69
x1
x1
x1
x1
x69
x3
x3
x3
x71
x37
x3
x3
x3
x3
x3
x34
x34
x34
x34
x1
x1
x1
x33
x34
x1
x1
x32
x34
x1
x1
x31
x31
x34
x32
x1
x1
x1
x32
x34
x32
x1
x1
x1
x32
x29
x29
x34
x30
x30
x1
x1
x1
x28
x30
x2
x2
x2
x2
x2
x2
x1
x1
x1
x2
x1
x30
x2
x1
x1
x1
x2
x30
x2
x1
x1
x1
x1
x1
x1
x2
x23
x23
x30
x30
x30
x30
x30
x30
x30
x30
x30
x30
x30
x30
x30
x30
x30
x23
x23
x23
x30
x1
x1
x1
x30
x1
x1
x1
x20
x20
x20
x30
x30
x30
x1
x1
x30
x30
x30
x30
x30
x30
x30
x30
x2
x2
x2
x17
x17
x17
x34
x28
x28
x28
x28
x28
x46
x1
x1
x1
x45
x45
x46
x7
x7
x1
x1
x7
x1
x1
x7
x1
x1
x7
x2
x2
x7
x1
x1
x7
x1
x1
x7
x7
x45
x46
x46
x27
x28
x56
x56
x26
x26
x56
x2
x2
x56
x1
x1
x56
x1
x1
x1
x1
x56
x25
x25
x56
x1
x1
x56
x56
x27
x28 |
I
I
I
I
|
// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.
/**
* Utilities for creating and verifying
* {@link https://www.rfc-editor.org/rfc/rfc9421 | RFC 9421} HTTP Message Signatures.
*
* HTTP Message Signatures provide end-to-end integrity and authenticity for
* components of an HTTP message by using detached digital signatures or MACs.
* The `Signature-Input` and `Signature` headers are serialized as Structured
* Fields Dictionaries ({@link https://www.rfc-editor.org/rfc/rfc9651 | RFC 9651}).
*
* @example Signing a request
* ```ts ignore
* import { signMessage } from "@std/http/unstable-message-signatures";
*
* const key = await crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"]);
* const request = new Request("https://example.com/api", {
* method: "POST",
* headers: { "Content-Type": "application/json" },
* body: '{"hello":"world"}',
* });
*
* const signed = await signMessage({
* message: request,
* params: {
* components: ["@method", "@authority", "@path", "content-type"],
* keyId: "my-key",
* created: Math.floor(Date.now() / 1000),
* },
* key: key.privateKey,
* });
* ```
*
* @example Verifying a signed request
* ```ts ignore
* import { verifyMessage } from "@std/http/unstable-message-signatures";
*
* const results = await verifyMessage(
* signedRequest,
* async (keyId) => lookupPublicKey(keyId),
* );
* ```
*
* Note: The `;bs` (Binary-wrapped Structured Field) parameter relies on
* splitting header values by `", "` because the Fetch API `Headers.get()`
* joins multiple values with this separator and does not expose `getAll()`.
* This means `;bs` will produce incorrect results for headers whose single
* value contains a literal `", "` (e.g. the `Date` header). Avoid using `;bs`
* on such fields; prefer `;sf` where the field is a known Structured Field.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/
import type { Uint8Array_ } from "./_types.ts";
export type { Uint8Array_ };
import type {
BareItem,
Dictionary,
InnerList,
Item,
} from "@std/http/unstable-structured-fields";
import {
binary,
innerList as sfInnerList,
integer as sfInteger,
isInnerList,
isItem,
item as sfItem,
parseDictionary,
parseItem,
parseList,
serializeDictionary,
serializeItem,
serializeList,
string as sfString,
} from "@std/http/unstable-structured-fields";
const UTF8_ENCODER = new TextEncoder();
const SF_KEY_REGEXP = /^[a-z*][a-z0-9_\-.*]*$/;
// =============================================================================
// Public Types
// =============================================================================
/**
* Algorithm identifiers per
* {@link https://www.rfc-editor.org/rfc/rfc9421#section-3.3 | RFC 9421 section 3.3}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export type SignatureAlgorithm =
| "rsa-pss-sha512"
| "rsa-v1_5-sha256"
| "hmac-sha256"
| "ecdsa-p256-sha256"
| "ecdsa-p384-sha384"
| "ed25519";
/**
* Parameters that can be attached to a component identifier.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @see {@link https://www.rfc-editor.org/rfc/rfc9421#section-2.1}
*/
export interface ComponentParameters {
/** Strict Structured Field serialization. */
sf?: true;
/** Dictionary member key. */
key?: string;
/**
* Binary-wrapped field values. Each field value is individually wrapped as
* a Byte Sequence. Because the Fetch API `Headers.get()` joins multiple
* values with `", "`, this flag will produce incorrect results for headers
* whose single value contains a literal `", "` (e.g. `Date`). Avoid using
* `;bs` on such fields.
*/
bs?: true;
/** Derive value from the related request. */
req?: true;
/** Derive value from trailer fields. */
tr?: true;
/** Query parameter name (for `@query-param`). */
name?: string;
}
/**
* A component identifier consisting of a name and optional parameters.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @see {@link https://www.rfc-editor.org/rfc/rfc9421#section-2}
*/
export interface ComponentIdentifier {
/** Lowercased field name or derived component name (e.g. `"@method"`). */
name: string;
/** Optional parameters per RFC 9421 section 2.1. */
parameters?: ComponentParameters;
}
/**
* Known derived component names per
* {@link https://www.rfc-editor.org/rfc/rfc9421#section-2.2 | RFC 9421 section 2.2}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export type DerivedComponent =
| "@method"
| "@target-uri"
| "@authority"
| "@scheme"
| "@request-target"
| "@path"
| "@query"
| "@query-param"
| "@status";
/**
* Convenience type accepting either a plain string or a full
* {@linkcode ComponentIdentifier}. Known derived component names are
* autocompleted.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export type ComponentInput =
| DerivedComponent
| (string & NonNullable<unknown>)
| ComponentIdentifier;
/**
* Signature parameters used when signing a message.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @see {@link https://www.rfc-editor.org/rfc/rfc9421#section-2.3}
*/
export interface SignatureParams {
/** Ordered list of covered components. */
components: ComponentInput[];
/** Signature algorithm. */
algorithm?: SignatureAlgorithm;
/** Key identifier. Mapped to/from the wire-format parameter name `keyid`. */
keyId?: string;
/** Creation time as seconds since Unix epoch (not milliseconds). */
created?: number;
/** Expiration time as seconds since Unix epoch (not milliseconds). */
expires?: number;
/** Nonce for replay protection. */
nonce?: string;
/** Application-specific tag. */
tag?: string;
/**
* Signature label, defaults to `"sig"`. Must be a valid sf-key (lowercase
* alphanumeric, `_`, `-`, `.`, `*`). Must also be unique among existing
* signatures on the target message per RFC 9421 section 4; a `TypeError`
* is thrown on collision.
*/
label?: string;
}
/**
* Parsed signature parameters returned from verification. Components are always
* fully resolved {@linkcode ComponentIdentifier} objects.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export interface ParsedSignatureParams {
/** Ordered list of covered components. */
components: ComponentIdentifier[];
/** Signature algorithm, if specified. */
algorithm?: SignatureAlgorithm;
/** Key identifier. */
keyId?: string;
/** Creation time as seconds since Unix epoch. */
created?: number;
/** Expiration time as seconds since Unix epoch. */
expires?: number;
/** Nonce value. */
nonce?: string;
/** Application-specific tag. */
tag?: string;
}
/**
* Options for {@linkcode signMessage}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export interface SignOptions<
T extends Request | Response = Request | Response,
> {
/** The HTTP request or response to sign. */
message: T;
/** Signature parameters. */
params: SignatureParams;
/** The signing key. */
key: CryptoKey;
/** The originating request, needed when signing a response with `;req` components. */
request?: Request;
}
/**
* Options for {@linkcode verifyMessage}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export interface VerifyOptions {
/** Maximum allowed age of the signature in seconds. */
maxAge?: number;
/** Components that must be covered by each verified signature. */
requiredComponents?: ComponentInput[];
/** Specific signature label(s) to verify. If omitted, all are verified. */
labels?: string[];
/** The originating request, needed when verifying response signatures with `;req` components. */
request?: Request;
}
/**
* Result of a successful signature verification.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export interface VerifyResult {
/** The label of the verified signature. */
label: string;
/** The parsed signature parameters. */
params: ParsedSignatureParams;
}
// =============================================================================
// Algorithm Dispatch
// =============================================================================
const SUPPORTED_ALGORITHMS: ReadonlySet<string> = new Set([
"rsa-pss-sha512",
"rsa-v1_5-sha256",
"hmac-sha256",
"ecdsa-p256-sha256",
"ecdsa-p384-sha384",
"ed25519",
]);
function isSupportedAlgorithm(value: string): value is SignatureAlgorithm {
return SUPPORTED_ALGORITHMS.has(value);
}
function getSignParams(
algorithm: SignatureAlgorithm,
): AlgorithmIdentifier | RsaPssParams | EcdsaParams {
switch (algorithm) {
case "rsa-pss-sha512":
return { name: "RSA-PSS", saltLength: 64 } as RsaPssParams;
case "rsa-v1_5-sha256":
return { name: "RSASSA-PKCS1-v1_5" };
case "hmac-sha256":
return { name: "HMAC" };
case "ecdsa-p256-sha256":
return { name: "ECDSA", hash: "SHA-256" } as EcdsaParams;
case "ecdsa-p384-sha384":
return { name: "ECDSA", hash: "SHA-384" } as EcdsaParams;
case "ed25519":
return { name: "Ed25519" };
}
}
function inferAlgorithm(key: CryptoKey): SignatureAlgorithm {
const alg = key.algorithm;
const name = alg.name;
if (name === "Ed25519") return "ed25519";
if (name === "HMAC") {
const hash = (alg as HmacKeyAlgorithm).hash.name;
if (hash === "SHA-256") return "hmac-sha256";
throw new TypeError(`Unsupported HMAC hash: "${hash}"`);
}
if (name === "RSA-PSS") {
const hash = (alg as RsaHashedKeyAlgorithm).hash.name;
if (hash === "SHA-512") return "rsa-pss-sha512";
throw new TypeError(`Unsupported RSA-PSS hash: "${hash}"`);
}
if (name === "RSASSA-PKCS1-v1_5") {
const hash = (alg as RsaHashedKeyAlgorithm).hash.name;
if (hash === "SHA-256") return "rsa-v1_5-sha256";
throw new TypeError(`Unsupported RSASSA-PKCS1-v1_5 hash: "${hash}"`);
}
if (name === "ECDSA") {
const curve = (alg as EcKeyAlgorithm).namedCurve;
if (curve === "P-256") return "ecdsa-p256-sha256";
if (curve === "P-384") return "ecdsa-p384-sha384";
throw new TypeError(`Unsupported ECDSA curve: "${curve}"`);
}
throw new TypeError(`Cannot infer signature algorithm from key: "${name}"`);
}
// =============================================================================
// Component Value Resolution
// =============================================================================
function normalizeIdentifier(input: ComponentInput): ComponentIdentifier {
if (typeof input === "string") {
return { name: input };
}
return input;
}
function resolveComponentValue(
id: ComponentIdentifier,
message: Request | Response,
parsedUrl: { value?: URL },
relatedRequest?: Request,
relatedParsedUrl?: { value?: URL },
): string {
const params = id.parameters ?? {};
// Validate incompatible parameter combinations
if (params.bs && params.sf) {
throw new TypeError(
`Cannot combine "bs" and "sf" parameters on component "${id.name}"`,
);
}
if (params.bs && params.key !== undefined) {
throw new TypeError(
`Cannot combine "bs" and "key" parameters on component "${id.name}"`,
);
}
if (params.tr) {
throw new TypeError(
`Trailer field resolution (";tr") is not supported for component "${id.name}"`,
);
}
// Handle ;req parameter
if (params.req) {
if (message instanceof Request) {
throw new TypeError(
`Cannot use "req" parameter on component "${id.name}" for a request message`,
);
}
if (!relatedRequest) {
throw new TypeError(
`Cannot resolve "req" parameter on component "${id.name}": no related request provided`,
);
}
const { req: _, ...restParams } = params;
return resolveComponentValue(
{ name: id.name, parameters: restParams },
relatedRequest,
relatedParsedUrl ?? {},
);
}
if (id.name.startsWith("@")) {
return resolveDerivedComponent(id.name, message, params, parsedUrl);
}
return resolveFieldComponent(id.name, message, params);
}
const REQUEST_ONLY_DERIVED: ReadonlySet<string> = new Set<DerivedComponent>([
"@method",
"@target-uri",
"@authority",
"@scheme",
"@request-target",
"@path",
"@query",
"@query-param",
]);
function resolveDerivedComponent(
name: string,
message: Request | Response,
params: ComponentParameters,
parsedUrl: { value?: URL },
): string {
if (REQUEST_ONLY_DERIVED.has(name)) {
if (!(message instanceof Request)) {
throw new TypeError(`Cannot use "${name}" on a response message`);
}
return resolveRequestDerived(name, message, params, parsedUrl);
}
if (name === "@status") {
if (message instanceof Request) {
throw new TypeError(`Cannot use "${name}" on a request message`);
}
return String(message.status);
}
throw new TypeError(`Unknown derived component "${name}"`);
}
function resolveRequestDerived(
name: string,
request: Request,
params: ComponentParameters,
parsedUrl: { value?: URL },
): string {
if (name === "@method") return request.method.toUpperCase();
if (name === "@target-uri") return request.url;
const url = parsedUrl.value ??= new URL(request.url);
switch (name) {
case "@authority":
return url.host;
case "@scheme":
return url.protocol.slice(0, -1);
case "@request-target":
return url.pathname + url.search;
case "@path":
return url.pathname || "/";
case "@query":
return url.search || "?";
case "@query-param": {
if (params.name === undefined) {
throw new TypeError(
`Component "${name}" requires "name" parameter`,
);
}
const decoded = decodeURIComponent(params.name);
const searchParams = new URLSearchParams(url.search);
const values: string[] = [];
for (const [k, v] of searchParams) {
if (k === decoded) values.push(v);
}
if (values.length === 0) {
throw new TypeError(
`Query parameter "${params.name}" not found in request URL`,
);
}
// RFC 9421 section 2.2.8: "If a parameter name occurs multiple times
// in a request, the named query parameter MUST NOT be included."
if (values.length > 1) {
throw new TypeError(
`Query parameter "${params.name}" occurs multiple times`,
);
}
return encodeQueryParamValue(values[0]!);
}
default:
// Unreachable: gated by REQUEST_ONLY_DERIVED; kept for exhaustiveness.
throw new TypeError(`Unknown derived component "${name}"`);
}
}
function encodeQueryParamValue(value: string): string {
// RFC 9421 section 2.2.8: use "percent-encode after encoding" from the
// WHATWG URL spec (application/x-www-form-urlencoded serializing), which
// differs from encodeURIComponent in that it also encodes !'()* characters.
// URLSearchParams serializes with + for spaces; convert back to %20.
return new URLSearchParams([["", value]]).toString().slice(1).replaceAll(
"+",
"%20",
);
}
function resolveFieldComponent(
name: string,
message: Request | Response,
params: ComponentParameters,
): string {
const headerValue = message.headers.get(name);
if (headerValue === null) {
throw new TypeError(`Missing "${name}" header field`);
}
if (params.sf) {
return resolveStrictStructuredField(headerValue, name);
}
if (params.key !== undefined) {
return resolveDictionaryMember(headerValue, name, params.key);
}
if (params.bs) {
return resolveBinaryWrapped(headerValue);
}
return headerValue;
}
function resolveStrictStructuredField(
headerValue: string,
fieldName: string,
): string {
// Try Dictionary, then List, then Item — the first successful parse wins
try {
return serializeDictionary(parseDictionary(headerValue));
} catch { /* not a dictionary */ }
try {
return serializeList(parseList(headerValue));
} catch { /* not a list */ }
try {
return serializeItem(parseItem(headerValue));
} catch { /* not an item */ }
throw new TypeError(
`Cannot apply "sf" parameter to field "${fieldName}": unknown Structured Field type`,
);
}
function resolveDictionaryMember(
headerValue: string,
fieldName: string,
key: string,
): string {
let dict: Dictionary;
try {
dict = parseDictionary(headerValue);
} catch (cause) {
throw new TypeError(
`Cannot parse "${fieldName}" as Dictionary for "key" parameter`,
{ cause },
);
}
const member = dict.get(key);
if (member === undefined) {
throw new TypeError(
`Dictionary key "${key}" not found in "${fieldName}" header`,
);
}
if (isItem(member)) {
return serializeItem(member);
}
// Inner list member — serialize as inner list value (member_value)
return serializeList([member]);
}
function resolveBinaryWrapped(headerValue: string): string {
// Each field value is individually wrapped as a Byte Sequence.
// The Fetch API Headers.get() joins multiple values with ", " and does not
// expose getAll(). Splitting on ", " is therefore the best we can do, but
// it will mishandle single header values that contain a literal ", " (e.g.
// the Date header). Avoid using ;bs on such fields.
const values = headerValue.split(", ");
const items: Item[] = values.map((v) => {
const bytes = UTF8_ENCODER.encode(v.trim());
return sfItem(binary(bytes));
});
return serializeList(items);
}
// =============================================================================
// Signature Base Construction
// =============================================================================
function serializeComponentIdentifier(id: ComponentIdentifier): string {
let result = `"${id.name}"`;
const params = id.parameters ?? {};
if (params.sf) result += ";sf";
if (params.key !== undefined) result += `;key="${params.key}"`;
if (params.bs) result += ";bs";
if (params.req) result += ";req";
if (params.tr) result += ";tr";
if (params.name !== undefined) result += `;name="${params.name}"`;
return result;
}
function buildSignatureParamsValue(
components: ComponentIdentifier[],
params: SignatureParams,
): string {
// Build the inner list items (component identifiers as String items with params)
const items: Item[] = components.map((id) => {
const sfParams = new Map<string, BareItem>();
const p = id.parameters ?? {};
if (p.sf) sfParams.set("sf", { type: "boolean", value: true });
if (p.key !== undefined) {
sfParams.set("key", { type: "string", value: p.key });
}
if (p.bs) sfParams.set("bs", { type: "boolean", value: true });
if (p.req) sfParams.set("req", { type: "boolean", value: true });
// `;tr` is rejected upstream; add handling here when trailer support lands.
if (p.name !== undefined) {
sfParams.set("name", { type: "string", value: p.name });
}
return sfItem(sfString(id.name), sfParams);
});
// Build the inner list parameters (signature metadata)
const listParams = new Map<string, BareItem>();
if (params.created !== undefined) {
listParams.set("created", sfInteger(params.created));
}
if (params.expires !== undefined) {
listParams.set("expires", sfInteger(params.expires));
}
if (params.nonce !== undefined) {
listParams.set("nonce", sfString(params.nonce));
}
if (params.algorithm !== undefined) {
listParams.set("alg", sfString(params.algorithm));
}
if (params.keyId !== undefined) {
listParams.set("keyid", sfString(params.keyId));
}
if (params.tag !== undefined) {
listParams.set("tag", sfString(params.tag));
}
const il = sfInnerList(items, listParams);
// Serialize just the inner list (not as a dictionary member)
return serializeInnerListValue(il);
}
function serializeInnerListValue(il: InnerList): string {
const items = il.items.map((i) => serializeItem(i)).join(" ");
let result = `(${items})`;
for (const [key, value] of il.parameters) {
// buildSignatureParamsValue only ever puts integer (created/expires) or
// string (nonce/alg/keyid/tag) bare items into listParams.
result += `;${key}=${
value.type === "integer" ? String(value.value) : `"${value.value}"`
}`;
}
return result;
}
/**
* Options for {@linkcode createSignatureBase}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*/
export interface CreateSignatureBaseOptions {
/** The HTTP request or response. */
message: Request | Response;
/** Signature parameters including covered components. */
params: SignatureParams;
/** The originating request when signing a response with `;req` components. */
request?: Request;
}
/**
* Construct the signature base string for a message per
* {@link https://www.rfc-editor.org/rfc/rfc9421#section-2.5 | RFC 9421 section 2.5}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { createSignatureBase } from "@std/http/unstable-message-signatures";
* import { assert } from "@std/assert";
*
* const request = new Request("https://example.com/path", {
* method: "GET",
* headers: { "Content-Type": "text/plain" },
* });
* const base = createSignatureBase({
* message: request,
* params: {
* components: ["@method", "@authority"],
* keyId: "my-key",
* created: 1618884473,
* },
* });
*
* assert(base.includes('"@method": GET'));
* ```
*
* @param options The message, signature parameters, and optional related request.
* @returns The signature base string.
*/
export function createSignatureBase(
options: CreateSignatureBaseOptions,
): string {
const { message, params, request: relatedRequest } = options;
const components = params.components.map(normalizeIdentifier);
const seen = new Set<string>();
const lines: string[] = [];
const parsedUrl: { value?: URL } = {};
const relatedParsedUrl: { value?: URL } = {};
for (const id of components) {
if (id.name === "@signature-params") {
throw new TypeError(
`"@signature-params" must not be listed in covered components`,
);
}
if (id.name !== id.name.toLowerCase()) {
throw new TypeError(
`Component name "${id.name}" must be lowercase`,
);
}
const serializedId = serializeComponentIdentifier(id);
if (seen.has(serializedId)) {
throw new TypeError(
`Duplicate component identifier ${serializedId} in covered components`,
);
}
seen.add(serializedId);
const value = resolveComponentValue(
id,
message,
parsedUrl,
relatedRequest,
relatedParsedUrl,
);
lines.push(`${serializedId}: ${value}`);
}
const sigParamsValue = buildSignatureParamsValue(components, params);
lines.push(`"@signature-params": ${sigParamsValue}`);
return lines.join("\n");
}
// =============================================================================
// Input Validation
// =============================================================================
function validateTimestamp(value: number, name: string): void {
if (!Number.isInteger(value) || value < 0) {
throw new RangeError(
`${name} must be a non-negative integer, got ${value}`,
);
}
}
function validateSignParams(params: SignatureParams): void {
if (params.label !== undefined && !SF_KEY_REGEXP.test(params.label)) {
throw new TypeError(
`Invalid signature label "${params.label}": must be a valid sf-key (lowercase alphanumeric, _, -, ., *)`,
);
}
if (params.created !== undefined) {
validateTimestamp(params.created, "created");
}
if (params.expires !== undefined) {
validateTimestamp(params.expires, "expires");
}
if (
params.algorithm !== undefined && !isSupportedAlgorithm(params.algorithm)
) {
throw new TypeError(
`Unsupported signature algorithm: "${params.algorithm}"`,
);
}
}
// =============================================================================
// signMessage
// =============================================================================
/**
* Sign an HTTP message per
* {@link https://www.rfc-editor.org/rfc/rfc9421 | RFC 9421}.
*
* Returns a new Request/Response with `Signature` and `Signature-Input`
* headers appended. The original message is not mutated.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { signMessage } from "@std/http/unstable-message-signatures";
* import { assert } from "@std/assert";
*
* const key = await crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"]) as CryptoKeyPair;
* const request = new Request("https://example.com/", { method: "POST" });
* const signed = await signMessage({
* message: request,
* params: {
* components: ["@method", "@authority"],
* keyId: "test-key",
* created: Math.floor(Date.now() / 1000),
* },
* key: key.privateKey,
* });
*
* assert(signed.headers.has("Signature"));
* assert(signed.headers.has("Signature-Input"));
* ```
*
* @typeParam T The message type ({@linkcode Request} or {@linkcode Response}).
* @param options Signing options.
* @returns A new message with signature headers appended.
*/
export async function signMessage<T extends Request | Response>(
options: SignOptions<T>,
): Promise<T> {
const { message, params, key, request } = options;
const label = params.label ?? "sig";
validateSignParams(params);
assertUniqueLabel(message.headers, label);
const algorithm = params.algorithm ?? inferAlgorithm(key);
const baseOpts: CreateSignatureBaseOptions = { message, params };
if (request) baseOpts.request = request;
const base = createSignatureBase(baseOpts);
const baseBytes = UTF8_ENCODER.encode(base);
const signParams = getSignParams(algorithm);
const signatureBytes: Uint8Array_ = new Uint8Array(
await crypto.subtle.sign(signParams, key, baseBytes),
);
// Build Signature-Input value
const components = params.components.map(normalizeIdentifier);
const sigParamsValue = buildSignatureParamsValue(components, params);
// Build headers
const sigInputHeader = `${label}=${sigParamsValue}`;
const sigHeader = serializeDictionary(
new Map([[label, sfItem(binary(signatureBytes))]]),
);
// clone() preserves the concrete type at runtime
const clone = message.clone() as T;
appendHeader(clone.headers, "Signature-Input", sigInputHeader);
appendHeader(clone.headers, "Signature", sigHeader);
return clone;
}
function appendHeader(headers: Headers, name: string, value: string): void {
const existing = headers.get(name);
if (existing) {
headers.set(name, `${existing}, ${value}`);
} else {
headers.set(name, value);
}
}
// Per RFC 9421 §4, signature labels MUST be unique within an HTTP message
// across both the Signature-Input and Signature dictionaries. Appending a
// signature with a colliding label would produce duplicate dictionary keys
// that silently corrupt prior signatures on parse (last value wins per
// RFC 8941).
function assertUniqueLabel(headers: Headers, label: string): void {
for (const name of ["Signature-Input", "Signature"] as const) {
const value = headers.get(name);
if (value === null) continue;
let dict: Dictionary;
try {
dict = parseDictionary(value);
} catch (cause) {
throw new TypeError(
`Cannot parse existing "${name}" header on message to check label uniqueness`,
{ cause },
);
}
if (dict.has(label)) {
throw new TypeError(
`Signature label "${label}" is already present in the "${name}" header on the message; choose a unique label (RFC 9421 section 4)`,
);
}
}
}
// =============================================================================
// verifyMessage
// =============================================================================
/**
* Verify one or more signatures on an HTTP message per
* {@link https://www.rfc-editor.org/rfc/rfc9421 | RFC 9421}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts ignore
* import { verifyMessage } from "@std/http/unstable-message-signatures";
*
* const results = await verifyMessage(
* signedRequest,
* async (keyId) => lookupPublicKey(keyId),
* { requiredComponents: ["@method", "@authority"] },
* );
* ```
*
* @param message The HTTP request or response to verify.
* @param keyLookup Resolves a key identifier to a CryptoKey, or `null` if the
* key is not found. When the signature has no `keyid` parameter, the empty
* string `""` is passed.
* @param options Optional verification constraints.
* @returns Array of verified signature results.
*/
export async function verifyMessage(
message: Request | Response,
keyLookup: (
keyId: string,
algorithm?: SignatureAlgorithm,
) => Promise<CryptoKey | null> | CryptoKey | null,
options?: VerifyOptions,
): Promise<VerifyResult[]> {
if (
options?.maxAge !== undefined &&
(!Number.isInteger(options.maxAge) || options.maxAge < 0)
) {
throw new RangeError(
`maxAge must be a non-negative integer, got ${options.maxAge}`,
);
}
const sigInputHeader = message.headers.get("Signature-Input");
if (sigInputHeader === null) {
throw new TypeError('Missing "Signature-Input" header');
}
const sigHeader = message.headers.get("Signature");
if (sigHeader === null) {
throw new TypeError('Missing "Signature" header');
}
const sigInputDict = parseDictionary(sigInputHeader);
const sigDict = parseDictionary(sigHeader);
// Validate label consistency
for (const [label] of sigInputDict) {
if (!sigDict.has(label)) {
throw new TypeError(
`Label "${label}" found in Signature-Input but missing in Signature`,
);
}
}
for (const [label] of sigDict) {
if (!sigInputDict.has(label)) {
throw new TypeError(
`Label "${label}" found in Signature but missing in Signature-Input`,
);
}
}
const results: VerifyResult[] = [];
const now = Math.floor(Date.now() / 1000);
for (const [label, sigInputMember] of sigInputDict) {
// Filter by labels option
if (options?.labels && !options.labels.includes(label)) continue;
if (!isInnerList(sigInputMember)) {
throw new TypeError(
`Signature-Input member "${label}" is not an Inner List`,
);
}
// Parse covered components from the inner list
const parsedParams = parseSignatureInput(sigInputMember, label);
// Enforce required components
if (options?.requiredComponents) {
for (const required of options.requiredComponents) {
const reqId = normalizeIdentifier(required);
const reqKey = serializeComponentIdentifier(reqId);
const found = parsedParams.components.some(
(c) => serializeComponentIdentifier(c) === reqKey,
);
if (!found) {
throw new Error(
`Signature "${label}" does not cover required component ${reqKey}`,
);
}
}
}
// Check expires
if (parsedParams.expires !== undefined) {
if (now > parsedParams.expires) {
throw new Error(
`Signature "${label}" has expired (past "expires" timestamp)`,
);
}
}
// Check maxAge
if (options?.maxAge !== undefined) {
if (parsedParams.created === undefined) {
throw new Error(
`Signature "${label}" has no "created" timestamp but maxAge was requested`,
);
}
if (now - parsedParams.created > options.maxAge) {
throw new Error(`Signature "${label}" has expired`);
}
}
// Reconstruct signature base
const reconstructedParams: SignatureParams = {
components: parsedParams.components,
...(parsedParams.algorithm !== undefined &&
{ algorithm: parsedParams.algorithm }),
...(parsedParams.keyId !== undefined && { keyId: parsedParams.keyId }),
...(parsedParams.created !== undefined &&
{ created: parsedParams.created }),
...(parsedParams.expires !== undefined &&
{ expires: parsedParams.expires }),
...(parsedParams.nonce !== undefined && { nonce: parsedParams.nonce }),
...(parsedParams.tag !== undefined && { tag: parsedParams.tag }),
};
const verifyBaseOpts: CreateSignatureBaseOptions = {
message,
params: reconstructedParams,
};
if (options?.request) verifyBaseOpts.request = options.request;
const base = createSignatureBase(verifyBaseOpts);
const baseBytes = UTF8_ENCODER.encode(base);
// Get signature bytes
const sigMember = sigDict.get(label);
if (!sigMember || !isItem(sigMember)) {
throw new TypeError(
`Signature member "${label}" is not an Item`,
);
}
if (sigMember.value.type !== "binary") {
throw new TypeError(
`Signature member "${label}" is not a Byte Sequence`,
);
}
const sigBytes: Uint8Array_ = new Uint8Array(
sigMember.value.value,
);
// Look up key
const algorithm = parsedParams.algorithm;
const keyId = parsedParams.keyId ?? "";
const verifyKey = await keyLookup(keyId, algorithm);
if (verifyKey === null) {
throw new TypeError(`Key not found for keyId "${keyId}"`);
}
const verifyAlgorithm = algorithm ?? inferAlgorithm(verifyKey);
const verifyParams = getSignParams(verifyAlgorithm);
const valid = await crypto.subtle.verify(
verifyParams,
verifyKey,
sigBytes,
baseBytes,
);
if (!valid) {
throw new Error(
`Signature verification failed for label "${label}"`,
);
}
results.push({ label, params: parsedParams });
}
return results;
}
function parseSignatureInput(
il: InnerList,
label: string,
): ParsedSignatureParams {
const components: ComponentIdentifier[] = [];
for (const member of il.items) {
if (member.value.type !== "string") {
throw new TypeError(
`Component identifier in "${label}" is not a String`,
);
}
const name = member.value.value;
const params: ComponentParameters = {};
for (const [key, value] of member.parameters) {
switch (key) {
case "sf":
if (value.type === "boolean" && value.value) params.sf = true;
break;
case "key":
if (value.type === "string") params.key = value.value;
break;
case "bs":
if (value.type === "boolean" && value.value) params.bs = true;
break;
case "req":
if (value.type === "boolean" && value.value) params.req = true;
break;
case "tr":
if (value.type === "boolean" && value.value) params.tr = true;
break;
case "name":
if (value.type === "string") params.name = value.value;
break;
}
}
const hasParams = Object.values(params).some((v) => v !== undefined);
components.push(hasParams ? { name, parameters: params } : { name });
}
const result: ParsedSignatureParams = { components };
for (const [key, value] of il.parameters) {
switch (key) {
case "created":
if (value.type === "integer") result.created = value.value;
break;
case "expires":
if (value.type === "integer") result.expires = value.value;
break;
case "nonce":
if (value.type === "string") result.nonce = value.value;
break;
case "alg":
if (value.type === "string" && isSupportedAlgorithm(value.value)) {
result.algorithm = value.value;
}
break;
case "keyid":
if (value.type === "string") result.keyId = value.value;
break;
case "tag":
if (value.type === "string") result.tag = value.value;
break;
}
}
return result;
}
|