@xlinklight/publishing 3.2.0 → 3.3.0
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/package.json
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {createPublicKey, verify} from 'node:crypto';
|
|
2
|
+
import {protocolRecord, publishingDigest, publishingError, publishingIdentifier} from './protocol.mjs';
|
|
3
|
+
|
|
4
|
+
export const maximumAcceptanceArchiveBytes = 50 * 1024 ** 2;
|
|
5
|
+
export const deliveryBindingFields = Object.freeze([
|
|
6
|
+
'projectId', 'solutionId', 'version', 'origin', 'keyId', 'manifestSha256', 'archiveSha256', 'artifactSha256', 'byteLength',
|
|
7
|
+
]);
|
|
8
|
+
const fail = code => publishingError('ACCEPTANCE_' + code);
|
|
9
|
+
const versionPattern = /^\d{1,8}\.\d{1,8}\.\d{1,8}$/;
|
|
10
|
+
|
|
11
|
+
export function validateDeliveryOrigin(value) {
|
|
12
|
+
const url = typeof value === 'string' ? URL.parse(value) : null;
|
|
13
|
+
if (url?.protocol !== 'https:' || url.origin !== value) throw fail('ORIGIN_INVALID');
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function deliveryBindingBytes(value) {
|
|
18
|
+
if (!protocolRecord(value,deliveryBindingFields) || ![value.projectId,value.solutionId,value.keyId].every(publishingIdentifier) ||
|
|
19
|
+
!versionPattern.test(value.version) || ![value.manifestSha256,value.archiveSha256,value.artifactSha256].every(publishingDigest) ||
|
|
20
|
+
!Number.isSafeInteger(value.byteLength) || value.byteLength < 1 || value.byteLength > maximumAcceptanceArchiveBytes) throw fail('BINDING_INVALID');
|
|
21
|
+
validateDeliveryOrigin(value.origin);
|
|
22
|
+
return Buffer.from(JSON.stringify(['link-light/owner-acceptance-installation/v1', ...deliveryBindingFields.map(key => value[key])]));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function verifyAcceptanceDelivery(value, signature, publicKey) {
|
|
26
|
+
if (typeof signature !== 'string' || !/^[A-Za-z0-9_-]{86}$/.test(signature)) throw fail('SIGNATURE_INVALID');
|
|
27
|
+
const key = typeof publicKey === 'string' ? createPublicKey(publicKey) : publicKey;
|
|
28
|
+
if (key?.asymmetricKeyType !== 'ed25519' || !verify(null,deliveryBindingBytes(value),key,Buffer.from(signature,'base64url'))) throw fail('SIGNATURE_INVALID');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function readDeliveryBody(message, limit) {
|
|
32
|
+
const length = message.headers.get('content-length');
|
|
33
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > maximumAcceptanceArchiveBytes || !message.body ||
|
|
34
|
+
length !== null && (!/^\d+$/.test(length) || Number(length) > limit)) throw fail('BODY_LIMIT');
|
|
35
|
+
const reader = message.body.getReader(), chunks = [];
|
|
36
|
+
let total = 0;
|
|
37
|
+
try {
|
|
38
|
+
for (;;) {
|
|
39
|
+
const {done,value} = await reader.read();
|
|
40
|
+
if (done) break;
|
|
41
|
+
total += value.byteLength;
|
|
42
|
+
if (total > limit) throw fail('BODY_LIMIT');
|
|
43
|
+
chunks.push(value);
|
|
44
|
+
}
|
|
45
|
+
} finally {
|
|
46
|
+
try { await reader.cancel(); } finally { reader.releaseLock(); }
|
|
47
|
+
}
|
|
48
|
+
return Buffer.concat(chunks,total);
|
|
49
|
+
}
|
package/src/acceptance.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type {KeyObject} from 'node:crypto';
|
|
2
2
|
import type {SolutionReleaseManifest} from '@xlinklight/contracts';
|
|
3
|
+
export {maximumAcceptanceArchiveBytes, readDeliveryBody, verifyAcceptanceDelivery} from './node/acceptance-delivery.mjs';
|
|
4
|
+
export type {AcceptanceDeliveryBinding} from './node/acceptance-delivery.mjs';
|
|
3
5
|
|
|
4
6
|
export interface AcceptanceIdentity {
|
|
5
7
|
projectId:string;requestId:string;intentSha256:string;policyRevision:number;
|
package/src/acceptance.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {createHash,createPrivateKey,createPublicKey,sign,verify} from 'node:crypto';
|
|
2
2
|
import {canonicalMetadata} from '@xlinklight/platform/worker';
|
|
3
3
|
import {protocolRecord,publishingDigest,publishingError,publishingIdentifier,publishingRequestId} from './protocol.mjs';
|
|
4
|
+
export {maximumAcceptanceArchiveBytes, readDeliveryBody, verifyAcceptanceDelivery} from './acceptance-delivery-binding.mjs';
|
|
4
5
|
|
|
5
6
|
const positive=value=>Number.isSafeInteger(value)&&value>0;
|
|
6
7
|
const identityKeys=['projectId','requestId','intentSha256','policyRevision','releaseDigest','acceptanceArtifactSha256','generation'];
|
|
@@ -1,22 +1,15 @@
|
|
|
1
|
-
import {createHash, createPrivateKey, createPublicKey, sign
|
|
2
|
-
import {protocolRecord,
|
|
1
|
+
import {createHash, createPrivateKey, createPublicKey, sign} from 'node:crypto';
|
|
2
|
+
import {protocolRecord, publishingError, publishingIdentifier} from '../protocol.mjs';
|
|
3
|
+
import {deliveryBindingBytes, deliveryBindingFields, readDeliveryBody, validateDeliveryOrigin} from '../acceptance-delivery-binding.mjs';
|
|
4
|
+
export {maximumAcceptanceArchiveBytes, readDeliveryBody, verifyAcceptanceDelivery} from '../acceptance-delivery-binding.mjs';
|
|
3
5
|
|
|
4
|
-
export const maximumAcceptanceArchiveBytes = 50 * 1024 ** 2;
|
|
5
6
|
const fail = code => publishingError('ACCEPTANCE_' + code);
|
|
6
|
-
const versionPattern = /^\d{1,8}\.\d{1,8}\.\d{1,8}$/;
|
|
7
7
|
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
8
|
-
const fields = ['projectId','solutionId','version','origin','keyId','manifestSha256','archiveSha256','artifactSha256','byteLength'];
|
|
9
|
-
|
|
10
|
-
function validateOrigin(value) {
|
|
11
|
-
const url = typeof value === 'string' ? URL.parse(value) : null;
|
|
12
|
-
if (url?.protocol !== 'https:' || url.origin !== value) throw fail('ORIGIN_INVALID');
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
8
|
|
|
16
9
|
export function parseAcceptanceDeliveryCredential(value) {
|
|
17
10
|
if (!protocolRecord(value,['origin','keyId','privateKey']) || !publishingIdentifier(value.keyId) ||
|
|
18
11
|
typeof value.privateKey !== 'string' || value.privateKey.length > 4096) throw fail('CREDENTIAL_INVALID');
|
|
19
|
-
|
|
12
|
+
validateDeliveryOrigin(value.origin);
|
|
20
13
|
const key = createPrivateKey(value.privateKey);
|
|
21
14
|
if (key.asymmetricKeyType !== 'ed25519') throw fail('CREDENTIAL_INVALID');
|
|
22
15
|
return {origin:value.origin,keyId:value.keyId,privateKey:key.export({type:'pkcs8',format:'pem'}).toString()};
|
|
@@ -27,22 +20,8 @@ export function acceptanceDeliveryTarget(credential) {
|
|
|
27
20
|
return {origin,keyId,publicKeySha256:hash(createPublicKey(privateKey).export({type:'spki',format:'der'}))};
|
|
28
21
|
}
|
|
29
22
|
|
|
30
|
-
function binding(value) {
|
|
31
|
-
if (!protocolRecord(value,fields) || ![value.projectId,value.solutionId,value.keyId].every(publishingIdentifier) ||
|
|
32
|
-
!versionPattern.test(value.version) || ![value.manifestSha256,value.archiveSha256,value.artifactSha256].every(publishingDigest) ||
|
|
33
|
-
!Number.isSafeInteger(value.byteLength) || value.byteLength < 1 || value.byteLength > maximumAcceptanceArchiveBytes) throw fail('BINDING_INVALID');
|
|
34
|
-
validateOrigin(value.origin);
|
|
35
|
-
return Buffer.from(JSON.stringify(['link-light/owner-acceptance-installation/v1', ...fields.map(key => value[key])]));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
23
|
export function signAcceptanceDelivery(value, privateKey) {
|
|
39
|
-
return sign(null,
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function verifyAcceptanceDelivery(value, signature, publicKey) {
|
|
43
|
-
if (typeof signature !== 'string' || !/^[A-Za-z0-9_-]{86}$/.test(signature)) throw fail('SIGNATURE_INVALID');
|
|
44
|
-
const key = typeof publicKey === 'string' ? createPublicKey(publicKey) : publicKey;
|
|
45
|
-
if (key?.asymmetricKeyType !== 'ed25519' || !verify(null,binding(value),key,Buffer.from(signature,'base64url'))) throw fail('SIGNATURE_INVALID');
|
|
24
|
+
return sign(null,deliveryBindingBytes(value),privateKey).toString('base64url');
|
|
46
25
|
}
|
|
47
26
|
|
|
48
27
|
/** Only the dedicated owner's HTTPS origin receives this archive. A read after
|
|
@@ -61,8 +40,8 @@ function deliveryTarget({intent,archive,credential,request=fetch,signal}) {
|
|
|
61
40
|
if(response.status!==200){await response.body?.cancel();throw fail('DELIVERY_UNKNOWN');}
|
|
62
41
|
const receipt=await readDeliveryBody(response,8192);
|
|
63
42
|
let value;try{value=JSON.parse(receipt.toString('utf8'));}catch{throw fail('DELIVERY_UNKNOWN');}
|
|
64
|
-
if (!protocolRecord(value,
|
|
65
|
-
if (!
|
|
43
|
+
if (!protocolRecord(value,deliveryBindingFields))throw fail('DELIVERY_UNKNOWN');
|
|
44
|
+
if (!deliveryBindingBytes(value).equals(deliveryBindingBytes(expected))) throw fail('VERSION_CONFLICT');
|
|
66
45
|
return true;
|
|
67
46
|
}
|
|
68
47
|
return {observed,expected,privateKey,options,url,request};
|
|
@@ -89,21 +68,3 @@ export async function deliverAcceptanceArchive(input) {
|
|
|
89
68
|
} catch(error){if(error?.code?.startsWith('PUBLISHING_ACCEPTANCE_'))throw error;}
|
|
90
69
|
if(!await observed())throw fail(rejection??'DELIVERY_UNKNOWN');
|
|
91
70
|
}
|
|
92
|
-
|
|
93
|
-
export async function readDeliveryBody(message,limit) {
|
|
94
|
-
const length=message.headers.get('content-length');
|
|
95
|
-
if(!Number.isSafeInteger(limit)||limit<1||limit>maximumAcceptanceArchiveBytes||!message.body||
|
|
96
|
-
length!==null&&(!/^\d+$/.test(length)||Number(length)>limit))throw fail('BODY_LIMIT');
|
|
97
|
-
const reader=message.body.getReader(),chunks=[];
|
|
98
|
-
let total=0;
|
|
99
|
-
try {
|
|
100
|
-
for(;;){
|
|
101
|
-
const {done,value}=await reader.read();
|
|
102
|
-
if(done)break;
|
|
103
|
-
total+=value.byteLength;
|
|
104
|
-
if(total>limit)throw fail('BODY_LIMIT');
|
|
105
|
-
chunks.push(value);
|
|
106
|
-
}
|
|
107
|
-
} finally {try{await reader.cancel();}finally{reader.releaseLock();}}
|
|
108
|
-
return Buffer.concat(chunks,total);
|
|
109
|
-
}
|