@originator-profile/sign 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2471 -0
- package/dist/index.js +2429 -0
- package/package.json +16 -21
- package/dist/index.cjs +0 -308
- package/dist/index.d.cts +0 -211
- package/dist/index.d.mts +0 -211
- package/dist/index.mjs +0 -293
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@originator-profile/sign",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"homepage": "https://docs.originator-profile.org",
|
|
6
6
|
"repository": {
|
|
@@ -13,14 +13,8 @@
|
|
|
13
13
|
},
|
|
14
14
|
"type": "module",
|
|
15
15
|
"exports": {
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
"default": "./dist/index.cjs"
|
|
19
|
-
},
|
|
20
|
-
"import": {
|
|
21
|
-
"types": "./dist/index.d.mts",
|
|
22
|
-
"default": "./dist/index.mjs"
|
|
23
|
-
}
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
24
18
|
},
|
|
25
19
|
"files": [
|
|
26
20
|
"dist",
|
|
@@ -30,22 +24,23 @@
|
|
|
30
24
|
],
|
|
31
25
|
"dependencies": {
|
|
32
26
|
"jose": "^6.2.2",
|
|
33
|
-
"@originator-profile/cryptography": "0.5.
|
|
34
|
-
"@originator-profile/securing-mechanism": "0.5.
|
|
35
|
-
"@originator-profile/model": "0.5.
|
|
27
|
+
"@originator-profile/cryptography": "0.5.2",
|
|
28
|
+
"@originator-profile/securing-mechanism": "0.5.2",
|
|
29
|
+
"@originator-profile/model": "0.5.2"
|
|
36
30
|
},
|
|
37
31
|
"devDependencies": {
|
|
38
|
-
"date-fns": "
|
|
39
|
-
"eslint": "
|
|
40
|
-
"pkgroll": "
|
|
41
|
-
"typescript": "
|
|
42
|
-
"vitest": "
|
|
43
|
-
"websri": "
|
|
44
|
-
"
|
|
45
|
-
"eslint-config-originator-profile": "0.5.
|
|
32
|
+
"date-fns": "4.1.0",
|
|
33
|
+
"eslint": "10.2.0",
|
|
34
|
+
"pkgroll": "2.27.0",
|
|
35
|
+
"typescript": "6.0.2",
|
|
36
|
+
"vitest": "4.1.4",
|
|
37
|
+
"websri": "1.0.1",
|
|
38
|
+
"zod": "4.4.3",
|
|
39
|
+
"eslint-config-originator-profile": "0.5.2",
|
|
40
|
+
"@originator-profile/tsconfig": "0.5.2"
|
|
46
41
|
},
|
|
47
42
|
"scripts": {
|
|
48
|
-
"build": "pkgroll --clean-dist
|
|
43
|
+
"build": "pkgroll --clean-dist",
|
|
49
44
|
"test": "vitest run",
|
|
50
45
|
"lint": "eslint --fix .",
|
|
51
46
|
"type-check": "tsc --noEmit"
|
package/dist/index.cjs
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var securingMechanism = require('@originator-profile/securing-mechanism');
|
|
4
|
-
|
|
5
|
-
const supportedHashAlgorithms = {
|
|
6
|
-
/** SHA-256 hash algorithm */
|
|
7
|
-
sha256: "SHA-256",
|
|
8
|
-
/** SHA-384 hash algorithm */
|
|
9
|
-
sha384: "SHA-384",
|
|
10
|
-
/** SHA-512 hash algorithm */
|
|
11
|
-
sha512: "SHA-512"
|
|
12
|
-
};
|
|
13
|
-
const IntegrityMetadataRegex = /^(?<alg>sha256|sha384|sha512)-(?<val>(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?)(?:[?](?<opt>[\x21-\x7e]*))?$/;
|
|
14
|
-
class IntegrityMetadata {
|
|
15
|
-
/** Hash algorithm */
|
|
16
|
-
alg;
|
|
17
|
-
/** The base64-encoded hash value of the resource */
|
|
18
|
-
val;
|
|
19
|
-
/** Optional additional attributes */
|
|
20
|
-
opt;
|
|
21
|
-
/**
|
|
22
|
-
* Creates an instance of `IntegrityMetadata` from a given object or string.
|
|
23
|
-
* @param integrity The integrity metadata input, which can be a string or object.
|
|
24
|
-
* @example
|
|
25
|
-
* ```js
|
|
26
|
-
* new IntegrityMetadata("sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=")
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* or
|
|
30
|
-
*
|
|
31
|
-
* ```js
|
|
32
|
-
* new IntegrityMetadata({
|
|
33
|
-
* alg: "sha256",
|
|
34
|
-
* val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
35
|
-
* })
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
constructor(integrity) {
|
|
39
|
-
const integrityString = typeof integrity === "object" && integrity !== null ? IntegrityMetadata.stringify(integrity) : String(integrity ?? "").trim();
|
|
40
|
-
const {
|
|
41
|
-
alg = "",
|
|
42
|
-
val = "",
|
|
43
|
-
opt
|
|
44
|
-
} = IntegrityMetadataRegex.exec(integrityString)?.groups ?? {};
|
|
45
|
-
Object.assign(this, {
|
|
46
|
-
alg,
|
|
47
|
-
val,
|
|
48
|
-
opt: opt?.split("?") ?? []
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Compares the current integrity metadata with another object or string.
|
|
53
|
-
* @param integrity The integrity metadata to compare with.
|
|
54
|
-
* @returns `true` if the integrity metadata matches, `false` otherwise.
|
|
55
|
-
* @example
|
|
56
|
-
* ```js
|
|
57
|
-
* integrityMetadata.match("sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=")
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* or
|
|
61
|
-
*
|
|
62
|
-
* ```js
|
|
63
|
-
* integrityMetadata.match({
|
|
64
|
-
* alg: "sha256",
|
|
65
|
-
* val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
66
|
-
* })
|
|
67
|
-
* ```
|
|
68
|
-
*/
|
|
69
|
-
match(integrity) {
|
|
70
|
-
const { alg, val } = new IntegrityMetadata(integrity);
|
|
71
|
-
if (!alg) return false;
|
|
72
|
-
if (!val) return false;
|
|
73
|
-
if (!(alg in supportedHashAlgorithms)) return false;
|
|
74
|
-
return alg === this.alg && val === this.val;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Converts the integrity metadata into a string representation.
|
|
78
|
-
* @returns The string representation of the integrity metadata.
|
|
79
|
-
*/
|
|
80
|
-
toString() {
|
|
81
|
-
return IntegrityMetadata.stringify(this);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Converts the integrity metadata into a JSON string.
|
|
85
|
-
* @returns The JSON string representation of the integrity metadata.
|
|
86
|
-
*/
|
|
87
|
-
toJSON() {
|
|
88
|
-
return this.toString();
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Static method to stringify an integrity metadata object.
|
|
92
|
-
* @param integrity The integrity metadata object to stringify.
|
|
93
|
-
* @returns The stringified integrity metadata.
|
|
94
|
-
* @example
|
|
95
|
-
* ```js
|
|
96
|
-
* IntegrityMetadata.stringify({
|
|
97
|
-
* alg: "sha256",
|
|
98
|
-
* val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",
|
|
99
|
-
* }) // "sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM="
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
static stringify({ alg, val, opt = [] }) {
|
|
103
|
-
if (!alg) return "";
|
|
104
|
-
if (!val) return "";
|
|
105
|
-
if (!(alg in supportedHashAlgorithms)) return "";
|
|
106
|
-
return `${alg}-${[val, ...opt].join("?")}`;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
async function createIntegrityMetadata(hashAlgorithm, data, opt = []) {
|
|
110
|
-
const alg = hashAlgorithm.toLowerCase();
|
|
111
|
-
if (!(alg in supportedHashAlgorithms)) {
|
|
112
|
-
return new IntegrityMetadata("");
|
|
113
|
-
}
|
|
114
|
-
const hashAlgorithmIdentifier = supportedHashAlgorithms[alg];
|
|
115
|
-
const arrayBuffer = await crypto.subtle.digest(hashAlgorithmIdentifier, data);
|
|
116
|
-
const val = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
|
117
|
-
const integrity = IntegrityMetadata.stringify({ alg, val, opt });
|
|
118
|
-
return new IntegrityMetadata(integrity);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async function createDigestSri(alg, resource, fetcher = fetch) {
|
|
122
|
-
if (!(alg in supportedHashAlgorithms)) {
|
|
123
|
-
return { id: resource.id };
|
|
124
|
-
}
|
|
125
|
-
const meta = await Promise.all(
|
|
126
|
-
[resource.content ?? resource.id].flat().map(async (content) => {
|
|
127
|
-
const res = await fetcher(content);
|
|
128
|
-
const data = await res.arrayBuffer();
|
|
129
|
-
return await createIntegrityMetadata(alg, data);
|
|
130
|
-
})
|
|
131
|
-
);
|
|
132
|
-
return {
|
|
133
|
-
id: resource.id,
|
|
134
|
-
digestSRI: meta.join(" ")
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
class FetchFailed extends Error {
|
|
139
|
-
static get code() {
|
|
140
|
-
return "ERR_FETCH_FAILED";
|
|
141
|
-
}
|
|
142
|
-
code = FetchFailed.code;
|
|
143
|
-
ok = false;
|
|
144
|
-
error;
|
|
145
|
-
constructor(message, error) {
|
|
146
|
-
super(message);
|
|
147
|
-
this.error = error;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async function fetchAndSetDigestSri(alg, content) {
|
|
152
|
-
if (!content) return content;
|
|
153
|
-
if (typeof content.digestSRI !== "string") {
|
|
154
|
-
Object.assign(
|
|
155
|
-
content,
|
|
156
|
-
await createDigestSri(alg, content)
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
delete content.content;
|
|
160
|
-
return content;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const fetchHtmlContent = async (elements) => {
|
|
164
|
-
const text = elements.map((element) => element.outerHTML).join("");
|
|
165
|
-
return [new Response(text)];
|
|
166
|
-
};
|
|
167
|
-
const fetchTextContent = async (elements) => {
|
|
168
|
-
const text = elements.map((element) => element.textContent ?? "").join("");
|
|
169
|
-
return [new Response(text)];
|
|
170
|
-
};
|
|
171
|
-
const fetchVisibleTextContent = async (elements) => {
|
|
172
|
-
const text = elements.map((element) => element.innerText).join("");
|
|
173
|
-
return [new Response(text)];
|
|
174
|
-
};
|
|
175
|
-
const fetchExternalResource = async (elements, fetcher = fetch) => {
|
|
176
|
-
return await Promise.all(
|
|
177
|
-
elements.map(async (element) => {
|
|
178
|
-
const el = element;
|
|
179
|
-
const src = el.currentSrc || el.src;
|
|
180
|
-
if (!src) {
|
|
181
|
-
throw new Error("Element has no src or currentSrc property");
|
|
182
|
-
}
|
|
183
|
-
try {
|
|
184
|
-
return await fetcher(src);
|
|
185
|
-
} catch (e) {
|
|
186
|
-
if (e instanceof Error || e instanceof window.Error) {
|
|
187
|
-
throw new FetchFailed(`Failed to fetch`, e);
|
|
188
|
-
}
|
|
189
|
-
throw e;
|
|
190
|
-
}
|
|
191
|
-
})
|
|
192
|
-
);
|
|
193
|
-
};
|
|
194
|
-
const selectByCss = (params) => {
|
|
195
|
-
return Array.from(
|
|
196
|
-
params.document.querySelectorAll(params.cssSelector)
|
|
197
|
-
);
|
|
198
|
-
};
|
|
199
|
-
const selectByIntegrity = (params) => {
|
|
200
|
-
return selectByCss({
|
|
201
|
-
...params,
|
|
202
|
-
cssSelector: `[integrity=${JSON.stringify(String(params.integrity))}]`
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
async function createIntegrity(alg, { content = "", ...target }, doc) {
|
|
206
|
-
if (![
|
|
207
|
-
"TextTargetIntegrity",
|
|
208
|
-
"VisibleTextTargetIntegrity",
|
|
209
|
-
"HtmlTargetIntegrity",
|
|
210
|
-
"ExternalResourceTargetIntegrity"
|
|
211
|
-
].includes(target.type)) {
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
if (!(alg in supportedHashAlgorithms)) {
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
if (target.type === "ExternalResourceTargetIntegrity") {
|
|
218
|
-
const meta2 = await Promise.all(
|
|
219
|
-
[content].flat().map(async (content2) => {
|
|
220
|
-
const res2 = URL.canParse(content2) ? await fetch(content2) : new Response(content2);
|
|
221
|
-
const data2 = await res2.arrayBuffer();
|
|
222
|
-
return await createIntegrityMetadata(alg, data2);
|
|
223
|
-
})
|
|
224
|
-
);
|
|
225
|
-
return {
|
|
226
|
-
...target,
|
|
227
|
-
integrity: meta2.join(" ")
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
doc ??= document;
|
|
231
|
-
const { contentFetcher, elementSelector } = {
|
|
232
|
-
HtmlTargetIntegrity: {
|
|
233
|
-
contentFetcher: fetchHtmlContent,
|
|
234
|
-
elementSelector: selectByCss
|
|
235
|
-
},
|
|
236
|
-
TextTargetIntegrity: {
|
|
237
|
-
contentFetcher: fetchTextContent,
|
|
238
|
-
elementSelector: selectByCss
|
|
239
|
-
},
|
|
240
|
-
VisibleTextTargetIntegrity: {
|
|
241
|
-
contentFetcher: fetchVisibleTextContent,
|
|
242
|
-
elementSelector: selectByCss
|
|
243
|
-
}
|
|
244
|
-
}[target.type];
|
|
245
|
-
const elements = elementSelector({ ...target, document: doc });
|
|
246
|
-
if (elements.length === 0) return null;
|
|
247
|
-
const [res] = await contentFetcher(elements);
|
|
248
|
-
if (!res) return null;
|
|
249
|
-
const data = await res.arrayBuffer();
|
|
250
|
-
const meta = await createIntegrityMetadata(alg, data);
|
|
251
|
-
return {
|
|
252
|
-
...target,
|
|
253
|
-
integrity: meta.toString()
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
class IntegrityCalculationError extends Error {
|
|
258
|
-
}
|
|
259
|
-
async function fetchAndSetTargetIntegrity(alg, obj, documentProvider = async () => document) {
|
|
260
|
-
const target = await Promise.all(
|
|
261
|
-
obj.target.map(async (raw, i) => {
|
|
262
|
-
if (raw.integrity) {
|
|
263
|
-
const { content: _, ...target3 } = raw;
|
|
264
|
-
return target3;
|
|
265
|
-
}
|
|
266
|
-
const doc = raw.type === "ExternalResourceTargetIntegrity" ? void 0 : await documentProvider(raw);
|
|
267
|
-
const target2 = await createIntegrity(alg, raw, doc);
|
|
268
|
-
if (!target2) {
|
|
269
|
-
throw new IntegrityCalculationError(
|
|
270
|
-
`Failed to create integrity for element target[${i}].`
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
return target2;
|
|
274
|
-
})
|
|
275
|
-
);
|
|
276
|
-
return Object.assign(obj, { target });
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async function signCa(uca, privateKey, {
|
|
280
|
-
alg = "ES256",
|
|
281
|
-
issuedAt = /* @__PURE__ */ new Date(),
|
|
282
|
-
expiredAt,
|
|
283
|
-
integrityAlg = "sha256",
|
|
284
|
-
documentProvider = async () => document
|
|
285
|
-
}) {
|
|
286
|
-
await fetchAndSetDigestSri(integrityAlg, uca.credentialSubject.image);
|
|
287
|
-
await fetchAndSetTargetIntegrity(integrityAlg, uca, documentProvider);
|
|
288
|
-
return await securingMechanism.signJwtVc(uca, privateKey, { alg, issuedAt, expiredAt });
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
async function signCp(cp, privateKey, options) {
|
|
292
|
-
return securingMechanism.signJwtVc(cp, privateKey, options);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
exports.FetchFailed = FetchFailed;
|
|
296
|
-
exports.IntegrityCalculationError = IntegrityCalculationError;
|
|
297
|
-
exports.createDigestSri = createDigestSri;
|
|
298
|
-
exports.createIntegrity = createIntegrity;
|
|
299
|
-
exports.fetchAndSetDigestSri = fetchAndSetDigestSri;
|
|
300
|
-
exports.fetchAndSetTargetIntegrity = fetchAndSetTargetIntegrity;
|
|
301
|
-
exports.fetchExternalResource = fetchExternalResource;
|
|
302
|
-
exports.fetchHtmlContent = fetchHtmlContent;
|
|
303
|
-
exports.fetchTextContent = fetchTextContent;
|
|
304
|
-
exports.fetchVisibleTextContent = fetchVisibleTextContent;
|
|
305
|
-
exports.selectByCss = selectByCss;
|
|
306
|
-
exports.selectByIntegrity = selectByIntegrity;
|
|
307
|
-
exports.signCa = signCa;
|
|
308
|
-
exports.signCp = signCp;
|
package/dist/index.d.cts
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { RawTarget, Target, UnsignedContentAttestation, Jwk, CoreProfile } from '@originator-profile/model';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Represents the available hash algorithms used for Subresource Integrity.
|
|
5
|
-
* @see {@link https://www.w3.org/TR/CSP2/#hash_algo}
|
|
6
|
-
*/
|
|
7
|
-
type HashAlgorithm = "sha256" | "sha384" | "sha512";
|
|
8
|
-
|
|
9
|
-
type DigestSriSource = {
|
|
10
|
-
id: string;
|
|
11
|
-
content?: string | string[];
|
|
12
|
-
};
|
|
13
|
-
type DigestSriResult = {
|
|
14
|
-
id: string;
|
|
15
|
-
digestSRI: string;
|
|
16
|
-
};
|
|
17
|
-
type DigestSriContent = DigestSriSource | DigestSriResult;
|
|
18
|
-
type ContentFetcher = (elements: ReadonlyArray<HTMLElement>, fetcher?: typeof fetch) => Promise<ReadonlyArray<Response>>;
|
|
19
|
-
type ElementSelector = (params: {
|
|
20
|
-
cssSelector?: string;
|
|
21
|
-
integrity?: string;
|
|
22
|
-
document: Document;
|
|
23
|
-
}) => ReadonlyArray<HTMLElement>;
|
|
24
|
-
/** 文脈に応じて Document を提供する関数 */
|
|
25
|
-
type DocumentProvider = (raw: RawTarget) => Promise<Document>;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* `digestSRI` の作成
|
|
29
|
-
*
|
|
30
|
-
* `content` にアクセスし `digestSRI` を計算します。
|
|
31
|
-
* なお、`content` プロパティは削除されます。
|
|
32
|
-
* `content` プロパティが存在しない場合、`id` にアクセスし `digestSRI` 計算します。
|
|
33
|
-
*
|
|
34
|
-
* 複数のコンテンツが指定された場合、それぞれのハッシュ値がスペース区切りで結合されます。
|
|
35
|
-
*
|
|
36
|
-
* @see {@link https://www.w3.org/TR/SRI/#the-integrity-attribute}
|
|
37
|
-
* @example
|
|
38
|
-
* 単一コンテンツ
|
|
39
|
-
* ```ts
|
|
40
|
-
* const resource = {
|
|
41
|
-
* id: "<URL>",
|
|
42
|
-
* content: "<コンテンツ (URL)>", // 省略可能
|
|
43
|
-
* };
|
|
44
|
-
*
|
|
45
|
-
* const { digestSRI } = await createDigestSri("sha256", resource);
|
|
46
|
-
* console.log(digestSRI); // sha256-...
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* 複数コンテンツ
|
|
51
|
-
* ```ts
|
|
52
|
-
* const resource = {
|
|
53
|
-
* id: "<URL>",
|
|
54
|
-
* content: ["<コンテンツURL1>", "<コンテンツURL2>"],
|
|
55
|
-
* };
|
|
56
|
-
*
|
|
57
|
-
* const { digestSRI } = await createDigestSri("sha256", resource);
|
|
58
|
-
* console.log(digestSRI); // sha256-... sha256-...
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
declare function createDigestSri(alg: HashAlgorithm, resource: DigestSriSource, fetcher?: typeof fetch): Promise<DigestSriContent>;
|
|
62
|
-
|
|
63
|
-
declare class FetchFailed extends Error {
|
|
64
|
-
static get code(): "ERR_FETCH_FAILED";
|
|
65
|
-
readonly code: "ERR_FETCH_FAILED";
|
|
66
|
-
readonly ok = false;
|
|
67
|
-
error: Error;
|
|
68
|
-
constructor(message: string, error: Error);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* オブジェクトへの `digestSRI` の割り当て
|
|
73
|
-
*
|
|
74
|
-
* `digestSRI` を省略した場合、`content` にアクセスし `digestSRI` を計算します。
|
|
75
|
-
* なお、`content` プロパティは削除されます。
|
|
76
|
-
* `content` プロパティが存在しない場合、`id` にアクセスし `digestSRI` 計算します。
|
|
77
|
-
* @see {@link https://www.w3.org/TR/SRI/#the-integrity-attribute}
|
|
78
|
-
* @example
|
|
79
|
-
* ```ts
|
|
80
|
-
* const resource = {
|
|
81
|
-
* id: "<URL>",
|
|
82
|
-
* content: "<コンテンツ (URL)>", // 省略可能
|
|
83
|
-
* };
|
|
84
|
-
*
|
|
85
|
-
* await fetchAndSetDigestSri("sha256", resource);
|
|
86
|
-
*
|
|
87
|
-
* console.log(resource);
|
|
88
|
-
* // {
|
|
89
|
-
* // id: "<URL>",
|
|
90
|
-
* // digestSRI: "sha256-..."
|
|
91
|
-
* // }
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
declare function fetchAndSetDigestSri(alg: HashAlgorithm, content: unknown): Promise<DigestSriResult | undefined>;
|
|
95
|
-
|
|
96
|
-
/** Integrityの計算に失敗 (例: 検証対象が存在しない) エラー */
|
|
97
|
-
declare class IntegrityCalculationError extends Error {
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 未署名 Content Attestation への Target Integrity の割り当て
|
|
101
|
-
* target[].integrity を省略した場合、type に準じて content から integrity を計算します。
|
|
102
|
-
* 一方、target[].integrity が含まれる場合、その値をそのまま使用します。
|
|
103
|
-
* なお、いずれも target[].content プロパティが削除される点にご注意ください。
|
|
104
|
-
* @see {@link https://docs.originator-profile.org/opb/content-integrity-descriptor/}
|
|
105
|
-
* @throws {IntegrityCalculationError} Integrityの計算に失敗 (例: 検証対象が存在しない)
|
|
106
|
-
* @example
|
|
107
|
-
* ```ts
|
|
108
|
-
* const uca = {
|
|
109
|
-
* // ...
|
|
110
|
-
* target: [
|
|
111
|
-
* {
|
|
112
|
-
* type: "<Target Integrityの種別>",
|
|
113
|
-
* cssSelector: "<CSS セレクター>",
|
|
114
|
-
* },
|
|
115
|
-
* ],
|
|
116
|
-
* };
|
|
117
|
-
*
|
|
118
|
-
* await fetchAndSetTargetIntegrity("sha256", uca);
|
|
119
|
-
*
|
|
120
|
-
* console.log(uca.target);
|
|
121
|
-
* // [
|
|
122
|
-
* // {
|
|
123
|
-
* // type: "<Target Integrityの種別>",
|
|
124
|
-
* // cssSelector: "<CSS セレクター>",
|
|
125
|
-
* // integrity: "sha256-..."
|
|
126
|
-
* // }
|
|
127
|
-
* // ]
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
declare function fetchAndSetTargetIntegrity<T extends {
|
|
131
|
-
target: ReadonlyArray<RawTarget>;
|
|
132
|
-
}>(alg: HashAlgorithm, obj: T, documentProvider?: DocumentProvider): Promise<T & {
|
|
133
|
-
target: ReadonlyArray<Target>;
|
|
134
|
-
}>;
|
|
135
|
-
|
|
136
|
-
/** element.outerHTML and join("") */
|
|
137
|
-
declare const fetchHtmlContent: ContentFetcher;
|
|
138
|
-
/** element.textContent and join("") */
|
|
139
|
-
declare const fetchTextContent: ContentFetcher;
|
|
140
|
-
/** element.innerText and join("") */
|
|
141
|
-
declare const fetchVisibleTextContent: ContentFetcher;
|
|
142
|
-
/**
|
|
143
|
-
* Fetches external resources from elements by using their `currentSrc` or `src` property.
|
|
144
|
-
* HTMLImageElement (<img>) and HTMLMediaElement (<video>, <audio>) support the `currentSrc` property,
|
|
145
|
-
* which represents the actual source URL currently in use after source selection (e.g., <img srcset>, <video> with multiple <source>).
|
|
146
|
-
* `currentSrc` is preferred over `src` because it reflects the final selected resource, ensuring integrity checks are performed on the actual loaded content.
|
|
147
|
-
* Falls back to `src` if `currentSrc` is not available.
|
|
148
|
-
*/
|
|
149
|
-
declare const fetchExternalResource: ContentFetcher;
|
|
150
|
-
declare const selectByCss: ElementSelector;
|
|
151
|
-
declare const selectByIntegrity: ElementSelector;
|
|
152
|
-
/**
|
|
153
|
-
* Target Integrity の作成
|
|
154
|
-
*
|
|
155
|
-
* `ExternalResourceTargetIntegrity` で複数のコンテンツが指定された場合、それぞれのハッシュ値がスペース区切りで結合されます。
|
|
156
|
-
*
|
|
157
|
-
* @see {@link https://docs.originator-profile.org/opb/content-integrity-descriptor/}
|
|
158
|
-
* @example
|
|
159
|
-
* 基本的な使用例
|
|
160
|
-
* ```ts
|
|
161
|
-
* const content = {
|
|
162
|
-
* type: "HtmlTargetIntegrity", // or ***TargetIntegrity
|
|
163
|
-
* cssSelector: "<CSS セレクター>",
|
|
164
|
-
* };
|
|
165
|
-
*
|
|
166
|
-
* const { integrity } = await createIntegrity("sha256", content);
|
|
167
|
-
* console.log(integrity); // sha256-...
|
|
168
|
-
* ```
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ExternalResourceTargetIntegrity で複数コンテンツ
|
|
172
|
-
* ```ts
|
|
173
|
-
* const content = {
|
|
174
|
-
* type: "ExternalResourceTargetIntegrity",
|
|
175
|
-
* content: ["<コンテンツURL1>", "<コンテンツURL2>"],
|
|
176
|
-
* };
|
|
177
|
-
*
|
|
178
|
-
* const { integrity } = await createIntegrity("sha256", content);
|
|
179
|
-
* console.log(integrity); // sha256-... sha256-...
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
declare function createIntegrity(alg: HashAlgorithm, { content, ...target }: RawTarget, doc?: Document): Promise<Target | null>;
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Content Attestation への署名
|
|
186
|
-
* @param uca 未署名 Content Attestation オブジェクト
|
|
187
|
-
* @param privateKey プライベート鍵
|
|
188
|
-
* @return JWT でエンコードされた Content Attestation
|
|
189
|
-
*/
|
|
190
|
-
declare function signCa(uca: UnsignedContentAttestation, privateKey: Jwk, { alg, issuedAt, expiredAt, integrityAlg, documentProvider, }: {
|
|
191
|
-
alg?: string;
|
|
192
|
-
issuedAt?: Date;
|
|
193
|
-
expiredAt: Date;
|
|
194
|
-
integrityAlg?: HashAlgorithm;
|
|
195
|
-
documentProvider?: DocumentProvider;
|
|
196
|
-
}): Promise<string>;
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* CP への署名
|
|
200
|
-
* @param cp CoreProfile オブジェクト
|
|
201
|
-
* @param privateKey プライベート鍵
|
|
202
|
-
* @return JWT でエンコードされた CP
|
|
203
|
-
*/
|
|
204
|
-
declare function signCp(cp: CoreProfile, privateKey: Jwk, options: {
|
|
205
|
-
alg?: string;
|
|
206
|
-
issuedAt: Date;
|
|
207
|
-
expiredAt: Date;
|
|
208
|
-
}): Promise<string>;
|
|
209
|
-
|
|
210
|
-
export { FetchFailed, IntegrityCalculationError, createDigestSri, createIntegrity, fetchAndSetDigestSri, fetchAndSetTargetIntegrity, fetchExternalResource, fetchHtmlContent, fetchTextContent, fetchVisibleTextContent, selectByCss, selectByIntegrity, signCa, signCp };
|
|
211
|
-
export type { ContentFetcher, DigestSriContent, DigestSriResult, DigestSriSource, DocumentProvider, ElementSelector };
|