@velora-cms/plugin-package 0.11.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/LICENSE +202 -0
- package/README.md +27 -0
- package/dist/envelope.d.ts +19 -0
- package/dist/envelope.js +104 -0
- package/dist/extract.d.ts +34 -0
- package/dist/extract.js +168 -0
- package/dist/hash.d.ts +4 -0
- package/dist/hash.js +16 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.js +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @velora-cms/plugin-package
|
|
2
|
+
|
|
3
|
+
The plugin packaging and signing toolchain for Velora CMS: builds the
|
|
4
|
+
signed tarball format the marketplace and self-hosted installs both trust,
|
|
5
|
+
and extracts/verifies it safely on the way back in (entry-count, entry-size,
|
|
6
|
+
and total-size limits enforced before anything touches disk).
|
|
7
|
+
|
|
8
|
+
Most authors never import this package directly — the
|
|
9
|
+
[`velora-plugin`](https://www.npmjs.com/package/velora-plugin) CLI wraps it
|
|
10
|
+
for `pack`, `sign`, and `publish`. It's published for tooling that needs
|
|
11
|
+
the primitives directly.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @velora-cms/plugin-package
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { hashTarball } from "@velora-cms/plugin-package";
|
|
23
|
+
|
|
24
|
+
const digest = await hashTarball("./my-plugin-1.0.0.tgz");
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Part of [Velora](https://github.com/velora-cms/velora). Apache-2.0.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SignatureEnvelope, TrustedKey, VerifyResult } from "./types.js";
|
|
2
|
+
export declare function deriveKeyId(publicKeyBase64: string): string;
|
|
3
|
+
export declare function generateSigningKeyPair(): {
|
|
4
|
+
privateKey: string;
|
|
5
|
+
publicKey: string;
|
|
6
|
+
keyId: string;
|
|
7
|
+
};
|
|
8
|
+
/** Deterministic serialization. Deliberately NOT full JCS — we own both
|
|
9
|
+
* ends, and a fixed-order literal is the entire requirement. */
|
|
10
|
+
export declare function canonicalizeEnvelope(envelope: SignatureEnvelope): Buffer;
|
|
11
|
+
export declare function signEnvelope(envelope: SignatureEnvelope, privateKeyBase64: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Check order is deliberate and produces better diagnostics than the
|
|
14
|
+
* reverse: authenticate the envelope FIRST (bad-signature = forged or
|
|
15
|
+
* corrupt), then compare it to the artifact we actually have
|
|
16
|
+
* (hash-mismatch = authentic envelope, tampered bytes — the flipped-byte
|
|
17
|
+
* case). Never throws.
|
|
18
|
+
*/
|
|
19
|
+
export declare function verifyEnvelope(sidecar: unknown, tarballSha256: string, trustedKeys: TrustedKey[]): VerifyResult;
|
package/dist/envelope.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, generateKeyPairSync, sign, verify } from "node:crypto";
|
|
2
|
+
/** Ed25519 SPKI DER is always 44 bytes: a 12-byte header followed by the
|
|
3
|
+
* raw 32-byte key. keyId is derived from the RAW key so it stays stable if
|
|
4
|
+
* the stored encoding ever changes. */
|
|
5
|
+
const SPKI_DER_LENGTH = 44;
|
|
6
|
+
const RAW_PUBLIC_KEY_LENGTH = 32;
|
|
7
|
+
function rawPublicKey(publicKeyBase64) {
|
|
8
|
+
const der = Buffer.from(publicKeyBase64, "base64");
|
|
9
|
+
if (der.length !== SPKI_DER_LENGTH) {
|
|
10
|
+
throw new Error(`expected a ${SPKI_DER_LENGTH}-byte Ed25519 SPKI DER public key, got ${der.length} bytes`);
|
|
11
|
+
}
|
|
12
|
+
return der.subarray(der.length - RAW_PUBLIC_KEY_LENGTH);
|
|
13
|
+
}
|
|
14
|
+
export function deriveKeyId(publicKeyBase64) {
|
|
15
|
+
return createHash("sha256").update(rawPublicKey(publicKeyBase64)).digest("hex").slice(0, 16);
|
|
16
|
+
}
|
|
17
|
+
export function generateSigningKeyPair() {
|
|
18
|
+
const pair = generateKeyPairSync("ed25519");
|
|
19
|
+
const privateKey = pair.privateKey.export({ format: "der", type: "pkcs8" }).toString("base64");
|
|
20
|
+
const publicKey = pair.publicKey.export({ format: "der", type: "spki" }).toString("base64");
|
|
21
|
+
return { privateKey, publicKey, keyId: deriveKeyId(publicKey) };
|
|
22
|
+
}
|
|
23
|
+
/** Deterministic serialization. Deliberately NOT full JCS — we own both
|
|
24
|
+
* ends, and a fixed-order literal is the entire requirement. */
|
|
25
|
+
export function canonicalizeEnvelope(envelope) {
|
|
26
|
+
return Buffer.from(JSON.stringify({
|
|
27
|
+
v: envelope.v,
|
|
28
|
+
alg: envelope.alg,
|
|
29
|
+
keyId: envelope.keyId,
|
|
30
|
+
pluginId: envelope.pluginId,
|
|
31
|
+
version: envelope.version,
|
|
32
|
+
sha256: envelope.sha256,
|
|
33
|
+
}), "utf8");
|
|
34
|
+
}
|
|
35
|
+
export function signEnvelope(envelope, privateKeyBase64) {
|
|
36
|
+
const key = createPrivateKey({
|
|
37
|
+
key: Buffer.from(privateKeyBase64, "base64"),
|
|
38
|
+
format: "der",
|
|
39
|
+
type: "pkcs8",
|
|
40
|
+
});
|
|
41
|
+
return sign(null, canonicalizeEnvelope(envelope), key).toString("base64");
|
|
42
|
+
}
|
|
43
|
+
function isSidecar(value) {
|
|
44
|
+
if (typeof value !== "object" || value === null)
|
|
45
|
+
return false;
|
|
46
|
+
const { envelope, signature } = value;
|
|
47
|
+
if (typeof signature !== "string" || signature.length === 0)
|
|
48
|
+
return false;
|
|
49
|
+
if (typeof envelope !== "object" || envelope === null)
|
|
50
|
+
return false;
|
|
51
|
+
const e = envelope;
|
|
52
|
+
return (e.v === 1 &&
|
|
53
|
+
e.alg === "ed25519" &&
|
|
54
|
+
typeof e.keyId === "string" &&
|
|
55
|
+
typeof e.pluginId === "string" &&
|
|
56
|
+
typeof e.version === "string" &&
|
|
57
|
+
typeof e.sha256 === "string");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Check order is deliberate and produces better diagnostics than the
|
|
61
|
+
* reverse: authenticate the envelope FIRST (bad-signature = forged or
|
|
62
|
+
* corrupt), then compare it to the artifact we actually have
|
|
63
|
+
* (hash-mismatch = authentic envelope, tampered bytes — the flipped-byte
|
|
64
|
+
* case). Never throws.
|
|
65
|
+
*/
|
|
66
|
+
export function verifyEnvelope(sidecar, tarballSha256, trustedKeys) {
|
|
67
|
+
if (!isSidecar(sidecar)) {
|
|
68
|
+
return { status: "failed", reason: "malformed-sidecar", message: "signature sidecar is not a valid envelope" };
|
|
69
|
+
}
|
|
70
|
+
if (trustedKeys.length === 0) {
|
|
71
|
+
return {
|
|
72
|
+
status: "failed",
|
|
73
|
+
reason: "no-trusted-keys",
|
|
74
|
+
message: "no trusted signing keys are configured — nothing can be verified",
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const trusted = trustedKeys.find((key) => key.keyId === sidecar.envelope.keyId);
|
|
78
|
+
if (!trusted) {
|
|
79
|
+
return {
|
|
80
|
+
status: "failed",
|
|
81
|
+
reason: "unknown-key",
|
|
82
|
+
message: `signed by unknown key "${sidecar.envelope.keyId}"`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
let signatureValid = false;
|
|
86
|
+
try {
|
|
87
|
+
const key = createPublicKey({ key: Buffer.from(trusted.publicKey, "base64"), format: "der", type: "spki" });
|
|
88
|
+
signatureValid = verify(null, canonicalizeEnvelope(sidecar.envelope), key, Buffer.from(sidecar.signature, "base64"));
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
signatureValid = false;
|
|
92
|
+
}
|
|
93
|
+
if (!signatureValid) {
|
|
94
|
+
return { status: "failed", reason: "bad-signature", message: "signature does not verify against the envelope" };
|
|
95
|
+
}
|
|
96
|
+
if (sidecar.envelope.sha256 !== tarballSha256) {
|
|
97
|
+
return {
|
|
98
|
+
status: "failed",
|
|
99
|
+
reason: "hash-mismatch",
|
|
100
|
+
message: `envelope describes sha256 ${sidecar.envelope.sha256} but the artifact hashes to ${tarballSha256}`,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return { status: "verified", keyId: trusted.keyId, envelope: sidecar.envelope };
|
|
104
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare const MAX_TOTAL_BYTES: number;
|
|
2
|
+
export declare const MAX_ENTRY_BYTES: number;
|
|
3
|
+
export declare const MAX_ENTRIES = 2000;
|
|
4
|
+
/** Pure gate over one entry's (path, type, declaredSize). Returns the
|
|
5
|
+
* violation message or null. Exported for direct unit testing — tar's
|
|
6
|
+
* own create() can't produce hostile names, but attackers hand-craft
|
|
7
|
+
* archives. Tar fixes each entry's content length in its header, so
|
|
8
|
+
* enforcing header sums equals enforcing actual bytes for well-formed
|
|
9
|
+
* archives; `strict: true` errors malformed ones.
|
|
10
|
+
*
|
|
11
|
+
* "OldFile" and "ContiguousFile" are accepted alongside "File" because
|
|
12
|
+
* tar's own types.js documents both as "same as File" — third-party
|
|
13
|
+
* packers (the whole point of a marketplace) may legitimately produce
|
|
14
|
+
* either. */
|
|
15
|
+
export declare function entryViolation(path: string, type: string, size: number): string | null;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts a single tarball into `targetDir`, admitting only entries that
|
|
18
|
+
* pass `entryViolation`'s allow-list and the MAX_ENTRIES/MAX_TOTAL_BYTES
|
|
19
|
+
* ceilings below. Never throws — every failure mode resolves
|
|
20
|
+
* `{ ok: false, reason }`.
|
|
21
|
+
*
|
|
22
|
+
* IMPORTANT: `{ ok: false }` does not mean nothing was written. Entries are
|
|
23
|
+
* written to disk incrementally as the archive streams in, so anything
|
|
24
|
+
* admitted before the violation was detected is already sitting under
|
|
25
|
+
* `targetDir`. Callers MUST treat `{ ok: false }` as fatal and delete
|
|
26
|
+
* `targetDir` themselves — this function does not roll back a partial
|
|
27
|
+
* extraction.
|
|
28
|
+
*/
|
|
29
|
+
export declare function extractArtifact(tarballPath: string, targetDir: string): Promise<{
|
|
30
|
+
ok: true;
|
|
31
|
+
} | {
|
|
32
|
+
ok: false;
|
|
33
|
+
reason: string;
|
|
34
|
+
}>;
|
package/dist/extract.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { createReadStream, mkdirSync } from "node:fs";
|
|
2
|
+
import * as tar from "tar";
|
|
3
|
+
export const MAX_TOTAL_BYTES = 100 * 1024 * 1024;
|
|
4
|
+
export const MAX_ENTRY_BYTES = 50 * 1024 * 1024;
|
|
5
|
+
export const MAX_ENTRIES = 2000;
|
|
6
|
+
// The per-entry cap tar applies to PAX/GNU meta entries (extended headers,
|
|
7
|
+
// long-path/long-linkpath records) before we ever see them. tar defaults
|
|
8
|
+
// this to 1MB internally if left unset; passed explicitly here so the cap
|
|
9
|
+
// is a documented decision rather than an inherited default. See the
|
|
10
|
+
// `ignoredEntry`/`meta` listeners in extractArtifact for why entries under
|
|
11
|
+
// this cap still need their own ceiling accounting.
|
|
12
|
+
const MAX_META_ENTRY_BYTES = 1024 * 1024;
|
|
13
|
+
const ALLOWED_ENTRY_TYPES = new Set(["File", "OldFile", "ContiguousFile", "Directory"]);
|
|
14
|
+
/** Pure gate over one entry's (path, type, declaredSize). Returns the
|
|
15
|
+
* violation message or null. Exported for direct unit testing — tar's
|
|
16
|
+
* own create() can't produce hostile names, but attackers hand-craft
|
|
17
|
+
* archives. Tar fixes each entry's content length in its header, so
|
|
18
|
+
* enforcing header sums equals enforcing actual bytes for well-formed
|
|
19
|
+
* archives; `strict: true` errors malformed ones.
|
|
20
|
+
*
|
|
21
|
+
* "OldFile" and "ContiguousFile" are accepted alongside "File" because
|
|
22
|
+
* tar's own types.js documents both as "same as File" — third-party
|
|
23
|
+
* packers (the whole point of a marketplace) may legitimately produce
|
|
24
|
+
* either. */
|
|
25
|
+
export function entryViolation(path, type, size) {
|
|
26
|
+
if (path.startsWith("/") || /^[a-zA-Z]:/.test(path) || path.split(/[\\/]/).includes("..")) {
|
|
27
|
+
return `unsafe entry path "${path}"`;
|
|
28
|
+
}
|
|
29
|
+
if (!ALLOWED_ENTRY_TYPES.has(type)) {
|
|
30
|
+
return `unsafe entry type "${type}" for "${path}" — only files and directories are allowed`;
|
|
31
|
+
}
|
|
32
|
+
if (size > MAX_ENTRY_BYTES) {
|
|
33
|
+
return `entry "${path}" exceeds the per-file size limit`;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
// tar's `filter` option is typed `(path: string, entry: Stats | ReadEntry) =>
|
|
38
|
+
// boolean` because the same option shape is shared with archive *creation*
|
|
39
|
+
// (where the second argument is a Stats, not a ReadEntry). During
|
|
40
|
+
// *extraction* — the only mode this module uses — tar always calls filter
|
|
41
|
+
// with a ReadEntry. This guard exists purely to satisfy that shared type;
|
|
42
|
+
// at runtime the `false` branch is unreachable from tar.extract().
|
|
43
|
+
function isReadEntry(entry) {
|
|
44
|
+
return "type" in entry && "path" in entry;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Extracts a single tarball into `targetDir`, admitting only entries that
|
|
48
|
+
* pass `entryViolation`'s allow-list and the MAX_ENTRIES/MAX_TOTAL_BYTES
|
|
49
|
+
* ceilings below. Never throws — every failure mode resolves
|
|
50
|
+
* `{ ok: false, reason }`.
|
|
51
|
+
*
|
|
52
|
+
* IMPORTANT: `{ ok: false }` does not mean nothing was written. Entries are
|
|
53
|
+
* written to disk incrementally as the archive streams in, so anything
|
|
54
|
+
* admitted before the violation was detected is already sitting under
|
|
55
|
+
* `targetDir`. Callers MUST treat `{ ok: false }` as fatal and delete
|
|
56
|
+
* `targetDir` themselves — this function does not roll back a partial
|
|
57
|
+
* extraction.
|
|
58
|
+
*/
|
|
59
|
+
export function extractArtifact(tarballPath, targetDir) {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
// Guards resolve() against firing twice (e.g. both the read stream's
|
|
62
|
+
// 'error' and the extractor's own 'error'/'finish' racing). Harmless —
|
|
63
|
+
// Promise settlement is idempotent regardless — but noted since it's
|
|
64
|
+
// not load-bearing for the contract, just defensive.
|
|
65
|
+
let settled = false;
|
|
66
|
+
const settle = (value) => {
|
|
67
|
+
if (!settled) {
|
|
68
|
+
settled = true;
|
|
69
|
+
resolve(value);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
try {
|
|
73
|
+
mkdirSync(targetDir, { recursive: true });
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
settle({ ok: false, reason: "could not prepare the extraction directory" });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
let entryCount = 0;
|
|
80
|
+
let totalBytes = 0;
|
|
81
|
+
let violation = null;
|
|
82
|
+
// Shared MAX_ENTRIES accounting — called for every entry we consider,
|
|
83
|
+
// real or meta, since both count toward one budget. Only latches
|
|
84
|
+
// `violation`; deliberately does not tear down the stream (see note
|
|
85
|
+
// below), matching how the pre-existing `filter` path already handled
|
|
86
|
+
// rejection — by refusing to admit anything further, not by aborting.
|
|
87
|
+
const countEntry = () => {
|
|
88
|
+
if (violation)
|
|
89
|
+
return false;
|
|
90
|
+
entryCount += 1;
|
|
91
|
+
if (entryCount > MAX_ENTRIES) {
|
|
92
|
+
violation = `archive has more than ${MAX_ENTRIES} entries`;
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
};
|
|
97
|
+
// Shared MAX_TOTAL_BYTES accounting, same reasoning.
|
|
98
|
+
const countBytes = (size) => {
|
|
99
|
+
if (violation)
|
|
100
|
+
return false;
|
|
101
|
+
totalBytes += size;
|
|
102
|
+
if (totalBytes > MAX_TOTAL_BYTES) {
|
|
103
|
+
violation = `archive expands beyond the ${MAX_TOTAL_BYTES / 1024 / 1024} MB limit`;
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
};
|
|
108
|
+
const extractor = tar.extract({
|
|
109
|
+
cwd: targetDir,
|
|
110
|
+
strict: true,
|
|
111
|
+
maxMetaEntrySize: MAX_META_ENTRY_BYTES,
|
|
112
|
+
filter: (path, entry) => {
|
|
113
|
+
if (violation || !isReadEntry(entry))
|
|
114
|
+
return false;
|
|
115
|
+
if (!countEntry())
|
|
116
|
+
return false;
|
|
117
|
+
const size = typeof entry.size === "number" ? entry.size : 0;
|
|
118
|
+
const perEntry = entryViolation(entry.path ?? path, entry.type, size);
|
|
119
|
+
if (perEntry) {
|
|
120
|
+
violation = perEntry;
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
return countBytes(size);
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
// tar's parser routes PAX/GNU meta entries (ExtendedHeader,
|
|
127
|
+
// GlobalExtendedHeader, long-path/long-linkpath records) around
|
|
128
|
+
// `filter` entirely (parse.js CONSUMEHEADER) — they exist to carry a
|
|
129
|
+
// long path/linkpath onto the *next* real entry, and `filter` is never
|
|
130
|
+
// consulted for them. Left unchecked, an archive built mostly or
|
|
131
|
+
// entirely of meta entries sails through with `filter` called once (or
|
|
132
|
+
// never) while hundreds of megabytes of declared content flow past
|
|
133
|
+
// uncounted — a real, demonstrated bypass of both ceilings below.
|
|
134
|
+
// Every meta entry is dispositioned one of two ways, and both are
|
|
135
|
+
// covered here:
|
|
136
|
+
// - `ignoredEntry` fires synchronously, before any body bytes are
|
|
137
|
+
// read, when a meta entry's declared size exceeds
|
|
138
|
+
// `maxMetaEntrySize` (tar force-ignores it itself).
|
|
139
|
+
// - `meta` fires after a meta entry within that cap has been fully
|
|
140
|
+
// read into memory.
|
|
141
|
+
// Both are tallied against the same MAX_ENTRIES/MAX_TOTAL_BYTES
|
|
142
|
+
// ceilings and latch `violation` exactly like a rejected real entry
|
|
143
|
+
// does. This deliberately does NOT call `extractor.abort()`: both
|
|
144
|
+
// listeners run synchronously inside minizlib's own `data` emission for
|
|
145
|
+
// the underlying gunzip stream, and destroying that stream reentrantly
|
|
146
|
+
// from within its own event corrupts/leaks the decompressor (observed
|
|
147
|
+
// as later ENOTEMPTY failures cleaning up temp dirs on Windows).
|
|
148
|
+
// Continuing to let the parser run to `finish` is the same tradeoff the
|
|
149
|
+
// original `filter`-only design already made for hostile real entries.
|
|
150
|
+
extractor.on("ignoredEntry", (entry) => {
|
|
151
|
+
if (!entry.meta)
|
|
152
|
+
return; // non-meta rejects were already counted inside `filter`
|
|
153
|
+
if (!countEntry())
|
|
154
|
+
return;
|
|
155
|
+
countBytes(typeof entry.size === "number" ? entry.size : 0);
|
|
156
|
+
});
|
|
157
|
+
extractor.on("meta", (data) => {
|
|
158
|
+
if (!countEntry())
|
|
159
|
+
return;
|
|
160
|
+
countBytes(Buffer.byteLength(data));
|
|
161
|
+
});
|
|
162
|
+
extractor.on("error", () => settle({ ok: false, reason: violation ?? "not a valid .tar.gz archive" }));
|
|
163
|
+
extractor.on("finish", () => settle(violation ? { ok: false, reason: violation } : { ok: true }));
|
|
164
|
+
createReadStream(tarballPath)
|
|
165
|
+
.on("error", () => settle({ ok: false, reason: "artifact could not be read" }))
|
|
166
|
+
.pipe(extractor);
|
|
167
|
+
});
|
|
168
|
+
}
|
package/dist/hash.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** One definition of "the artifact's hash" for signer and verifier alike —
|
|
2
|
+
* the same value S126 records as PluginVersion.artifactSha256. */
|
|
3
|
+
export declare function hashBuffer(buffer: Buffer): string;
|
|
4
|
+
export declare function hashTarball(path: string): Promise<string>;
|
package/dist/hash.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
/** One definition of "the artifact's hash" for signer and verifier alike —
|
|
4
|
+
* the same value S126 records as PluginVersion.artifactSha256. */
|
|
5
|
+
export function hashBuffer(buffer) {
|
|
6
|
+
return createHash("sha256").update(buffer).digest("hex");
|
|
7
|
+
}
|
|
8
|
+
export function hashTarball(path) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const hash = createHash("sha256");
|
|
11
|
+
createReadStream(path)
|
|
12
|
+
.on("error", reject)
|
|
13
|
+
.on("data", (chunk) => hash.update(chunk))
|
|
14
|
+
.on("end", () => resolve(hash.digest("hex")));
|
|
15
|
+
});
|
|
16
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { PackageSignatureSidecar, SignatureEnvelope, TrustedKey, VerifyFailureReason, VerifyResult, } from "./types.js";
|
|
2
|
+
export { canonicalizeEnvelope, deriveKeyId, generateSigningKeyPair, signEnvelope, verifyEnvelope, } from "./envelope.js";
|
|
3
|
+
export { hashBuffer, hashTarball } from "./hash.js";
|
|
4
|
+
export { entryViolation, extractArtifact, MAX_ENTRIES, MAX_ENTRY_BYTES, MAX_TOTAL_BYTES } from "./extract.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { canonicalizeEnvelope, deriveKeyId, generateSigningKeyPair, signEnvelope, verifyEnvelope, } from "./envelope.js";
|
|
2
|
+
export { hashBuffer, hashTarball } from "./hash.js";
|
|
3
|
+
export { entryViolation, extractArtifact, MAX_ENTRIES, MAX_ENTRY_BYTES, MAX_TOTAL_BYTES } from "./extract.js";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** The signed payload. Field order here is normative — canonicalizeEnvelope
|
|
2
|
+
* serializes in exactly this order, and signer and verifier must agree byte
|
|
3
|
+
* for byte. */
|
|
4
|
+
export interface SignatureEnvelope {
|
|
5
|
+
v: 1;
|
|
6
|
+
alg: "ed25519";
|
|
7
|
+
/** First 16 hex chars of sha256(raw 32-byte public key). Derived, never
|
|
8
|
+
* assigned, so the marketplace's DB row and the CMS's trusted set cannot
|
|
9
|
+
* drift. */
|
|
10
|
+
keyId: string;
|
|
11
|
+
pluginId: string;
|
|
12
|
+
version: string;
|
|
13
|
+
/** sha256 hex of the .tar.gz artifact this envelope describes. */
|
|
14
|
+
sha256: string;
|
|
15
|
+
}
|
|
16
|
+
/** The contents of the detached `<name>.tar.gz.sig` sidecar, and of
|
|
17
|
+
* PluginVersion.signature. */
|
|
18
|
+
export interface PackageSignatureSidecar {
|
|
19
|
+
envelope: SignatureEnvelope;
|
|
20
|
+
/** base64 Ed25519 signature over canonicalizeEnvelope(envelope). */
|
|
21
|
+
signature: string;
|
|
22
|
+
}
|
|
23
|
+
/** A public key the CMS is configured to trust. publicKey is base64 SPKI
|
|
24
|
+
* DER. */
|
|
25
|
+
export interface TrustedKey {
|
|
26
|
+
keyId: string;
|
|
27
|
+
publicKey: string;
|
|
28
|
+
}
|
|
29
|
+
export type VerifyFailureReason = "malformed-sidecar" | "no-trusted-keys" | "unknown-key" | "bad-signature" | "hash-mismatch" | "identity-mismatch"
|
|
30
|
+
/** The artifact itself could not be read (missing, unreadable, permission
|
|
31
|
+
* denied, or a mid-read failure) — nothing could be verified about it,
|
|
32
|
+
* since hashing never completed. Distinct from hash-mismatch, which
|
|
33
|
+
* requires a hash to have actually been computed and compared. */
|
|
34
|
+
| "artifact-unreadable";
|
|
35
|
+
export type VerifyResult = {
|
|
36
|
+
status: "verified";
|
|
37
|
+
keyId: string;
|
|
38
|
+
envelope: SignatureEnvelope;
|
|
39
|
+
} | {
|
|
40
|
+
status: "unsigned";
|
|
41
|
+
} | {
|
|
42
|
+
status: "failed";
|
|
43
|
+
reason: VerifyFailureReason;
|
|
44
|
+
message: string;
|
|
45
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@velora-cms/plugin-package",
|
|
3
|
+
"version": "0.11.0",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22.12"
|
|
6
|
+
},
|
|
7
|
+
"private": false,
|
|
8
|
+
"description": "Plugin package + signing toolchain for Velora CMS",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/velora-cms/velora.git",
|
|
27
|
+
"directory": "packages/plugin-package"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/velora-cms/velora/issues"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"tar": "7.5.22"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "22.20.1",
|
|
40
|
+
"typescript": "6.0.3",
|
|
41
|
+
"vitest": "3.2.6"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p .",
|
|
45
|
+
"test": "vitest run"
|
|
46
|
+
}
|
|
47
|
+
}
|