jso-protector 0.2.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.
Files changed (119) hide show
  1. package/CHANGELOG.md +960 -0
  2. package/LICENSE +90 -0
  3. package/MIGRATION.md +207 -0
  4. package/README.md +1477 -0
  5. package/RELEASE.md +82 -0
  6. package/SECURITY.md +71 -0
  7. package/ai-cli.js +392 -0
  8. package/ai.js +176 -0
  9. package/bin/jso-protector.js +12066 -0
  10. package/browserify.d.ts +10 -0
  11. package/browserify.js +88 -0
  12. package/bun.d.ts +10 -0
  13. package/bun.js +41 -0
  14. package/ci/Jenkinsfile +82 -0
  15. package/ci/argo-workflows.yaml +121 -0
  16. package/ci/azure-pipelines.yml +56 -0
  17. package/ci/bitbucket-pipelines.yml +43 -0
  18. package/ci/buildkite.yml +50 -0
  19. package/ci/circleci.yml +60 -0
  20. package/ci/drone.yml +67 -0
  21. package/ci/github-actions.yml +51 -0
  22. package/ci/gitlab-ci.yml +39 -0
  23. package/ci/gocd.yaml +79 -0
  24. package/ci/teamcity.kts +79 -0
  25. package/ci/tekton.yaml +106 -0
  26. package/ci/verify-package.js +748 -0
  27. package/ci/verify-publish-metadata.js +147 -0
  28. package/ci/verify-templates.js +195 -0
  29. package/ci/woodpecker.yml +53 -0
  30. package/compliance/cli.js +204 -0
  31. package/compliance/evidence-forwarder.js +209 -0
  32. package/compliance/pci-dss-v4/controls.json +75 -0
  33. package/compliance/pci-dss-v4/index.js +2613 -0
  34. package/config/named-sets.js +185 -0
  35. package/esbuild.d.ts +16 -0
  36. package/esbuild.js +190 -0
  37. package/examples/README.md +78 -0
  38. package/examples/browserify/build.js +20 -0
  39. package/examples/bun/package.json +13 -0
  40. package/examples/bun/scripts/protect-release.js +28 -0
  41. package/examples/cli-basic/jso.config.cjs +23 -0
  42. package/examples/cli-basic/jso.config.json +15 -0
  43. package/examples/cli-basic/package.json +14 -0
  44. package/examples/cli-basic/scripts/build.js +10 -0
  45. package/examples/curl/README.md +55 -0
  46. package/examples/curl/protect.sh +106 -0
  47. package/examples/esbuild/build.js +19 -0
  48. package/examples/grunt/Gruntfile.js +29 -0
  49. package/examples/gulp/gulpfile.js +19 -0
  50. package/examples/helm/Chart.yaml +27 -0
  51. package/examples/helm/README.md +69 -0
  52. package/examples/helm/templates/_helpers.tpl +23 -0
  53. package/examples/helm/templates/_podtemplate.tpl +64 -0
  54. package/examples/helm/templates/job.yaml +39 -0
  55. package/examples/helm/templates/secret.yaml +17 -0
  56. package/examples/helm/values.yaml +54 -0
  57. package/examples/kubernetes/job.yaml +123 -0
  58. package/examples/metro.config.js +11 -0
  59. package/examples/nextjs/next.config.js +11 -0
  60. package/examples/node-api/protect-release.js +28 -0
  61. package/examples/node-api/release-summary.js +59 -0
  62. package/examples/parcel/package.json +13 -0
  63. package/examples/parcel/scripts/protect-release.js +28 -0
  64. package/examples/payment-page-script-inventory.json +62 -0
  65. package/examples/payment-page-security-headers.json +46 -0
  66. package/examples/pre-commit/.pre-commit-config.yaml +33 -0
  67. package/examples/pre-commit/README.md +45 -0
  68. package/examples/react-native/README.md +208 -0
  69. package/examples/react-native/metro.config.js +11 -0
  70. package/examples/rollup/rollup.config.js +20 -0
  71. package/examples/rspack/rspack.config.js +28 -0
  72. package/examples/turbopack/package.json +13 -0
  73. package/examples/turbopack/scripts/protect-release.js +28 -0
  74. package/examples/vite/vite.config.js +18 -0
  75. package/examples/webpack/webpack.config.js +21 -0
  76. package/examples/webpack-loader/webpack.config.js +22 -0
  77. package/governance/index.d.ts +18 -0
  78. package/governance/index.js +309 -0
  79. package/grunt.d.ts +4 -0
  80. package/grunt.js +111 -0
  81. package/gulp.d.ts +10 -0
  82. package/gulp.js +106 -0
  83. package/index.d.ts +879 -0
  84. package/index.js +607 -0
  85. package/jso.config.example.json +29 -0
  86. package/jso.config.schema.json +548 -0
  87. package/metro.d.ts +43 -0
  88. package/metro.js +117 -0
  89. package/next.d.ts +24 -0
  90. package/next.js +64 -0
  91. package/package.json +241 -0
  92. package/parcel.d.ts +10 -0
  93. package/parcel.js +40 -0
  94. package/react-native.d.ts +2 -0
  95. package/react-native.js +3 -0
  96. package/release-signer.js +245 -0
  97. package/rollup.d.ts +13 -0
  98. package/rollup.js +71 -0
  99. package/rspack-loader.d.ts +13 -0
  100. package/rspack-loader.js +11 -0
  101. package/rspack.d.ts +11 -0
  102. package/rspack.js +9 -0
  103. package/runtime/ai-script-governance.js +379 -0
  104. package/runtime/countermeasures.js +0 -0
  105. package/runtime/data-exfiltration-guard.d.ts +7 -0
  106. package/runtime/data-exfiltration-guard.js +161 -0
  107. package/runtime/environment-integrity.js +389 -0
  108. package/runtime/managed-integrity.d.ts +4 -0
  109. package/runtime/managed-integrity.js +108 -0
  110. package/runtime/third-party-inventory.js +437 -0
  111. package/turbopack.d.ts +10 -0
  112. package/turbopack.js +41 -0
  113. package/vite.d.ts +16 -0
  114. package/vite.js +78 -0
  115. package/watermark.js +126 -0
  116. package/webpack-loader.d.ts +15 -0
  117. package/webpack-loader.js +97 -0
  118. package/webpack.d.ts +13 -0
  119. package/webpack.js +143 -0
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+
3
+ // Ed25519-signed release manifests for jso-protector.
4
+ //
5
+ // After a successful obfuscation run, this module produces a small
6
+ // signed attestation file (".manifest.sig") that proves:
7
+ //
8
+ // - the artifact came from a build with the holder of the private key
9
+ // - which BuildId / PolymorphismFingerprint was used
10
+ // - the exact SHA-256 of each protected output file
11
+ // - the ReleaseLabel and a UTC timestamp
12
+ //
13
+ // Verification is the inverse: given a .manifest.sig and the matching
14
+ // public key, confirm the signature, then re-hash each referenced file
15
+ // on disk and confirm the hashes still match. A failure on either step
16
+ // means the artifact was tampered with after signing.
17
+ //
18
+ // This is the SLSA-style attestation pattern adapted for an obfuscation
19
+ // pipeline. Enterprises typically want to publish their public key in
20
+ // their security.txt or repo, then anyone can verify "this protected
21
+ // JS came from our official CI" without trusting our service.
22
+ //
23
+ // Why Ed25519:
24
+ // - 64-byte signatures, 32-byte keys, fast verify
25
+ // - Node has built-in support (crypto.generateKeyPairSync('ed25519')
26
+ // since Node 12.0); no third-party dependency
27
+ // - Recommended over RSA for new designs in 2024+
28
+ //
29
+ // File formats:
30
+ //
31
+ // <output>/<name>.manifest.json existing protection manifest
32
+ // <output>/<name>.manifest.json.sig the new signed envelope:
33
+ //
34
+ // {
35
+ // "v": 1,
36
+ // "signedAt": "2026-05-27T00:00:00Z",
37
+ // "manifest": { // canonical subset we sign
38
+ // "buildId": "...",
39
+ // "polymorphismFingerprint": "...",
40
+ // "label": "...",
41
+ // "files": [
42
+ // { "name": "app.js", "sha256": "<hex>" },
43
+ // { "name": "lib/dep.js", "sha256": "<hex>" }
44
+ // ]
45
+ // },
46
+ // "publicKey": "<base64 SPKI DER>",
47
+ // "signature": "<base64 64-byte Ed25519 sig over canonical(manifest)>"
48
+ // }
49
+ //
50
+ // The signature is over a CANONICAL JSON encoding of the `manifest`
51
+ // subobject. Canonical = sorted keys, no whitespace, no trailing
52
+ // newline. This way the same logical manifest always produces the same
53
+ // bytes to sign and the same bytes to verify, regardless of which JSON
54
+ // library the caller used.
55
+
56
+ const crypto = require("node:crypto");
57
+ const fs = require("node:fs");
58
+ const path = require("node:path");
59
+
60
+ const VERSION = 1;
61
+
62
+ // ---- canonical JSON --------------------------------------------------------
63
+ //
64
+ // JSON has no canonical form by default — key ordering, whitespace, and
65
+ // number formatting all vary. We pick a deterministic shape so the bytes
66
+ // you sign now match the bytes a verifier produces later. Sort keys
67
+ // alphabetically, no whitespace, JSON.stringify number formatting.
68
+ function canonicalize(value) {
69
+ if (Array.isArray(value)) {
70
+ return "[" + value.map(canonicalize).join(",") + "]";
71
+ }
72
+ if (value && typeof value === "object") {
73
+ const keys = Object.keys(value).sort();
74
+ return "{" + keys.map(k =>
75
+ JSON.stringify(k) + ":" + canonicalize(value[k])
76
+ ).join(",") + "}";
77
+ }
78
+ return JSON.stringify(value);
79
+ }
80
+
81
+ // ---- key generation --------------------------------------------------------
82
+ //
83
+ // generateKeyPair() produces a fresh Ed25519 key pair as two PEM files.
84
+ // We write PEM (not DER) because most users already have PEM tooling
85
+ // (ssh-keygen, openssl, hashicorp Vault) and pasting a PEM into a CI
86
+ // secret store is the path of least surprise.
87
+ function generateKeyPair() {
88
+ const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
89
+ return {
90
+ publicKeyPem: publicKey.export({ type: "spki", format: "pem" }),
91
+ privateKeyPem: privateKey.export({ type: "pkcs8", format: "pem" }),
92
+ };
93
+ }
94
+
95
+ // ---- sign ------------------------------------------------------------------
96
+ //
97
+ // signRelease takes the parts of a build and a path to a PEM private
98
+ // key, returns the JSON envelope object (caller writes it to disk).
99
+ function signRelease(parts, privateKeyPem) {
100
+ if (!parts || !Array.isArray(parts.files)) {
101
+ throw new Error("signRelease: parts.files is required (array of { name, sha256 })");
102
+ }
103
+ const keyObj = crypto.createPrivateKey({ key: privateKeyPem, format: "pem" });
104
+ if (keyObj.asymmetricKeyType !== "ed25519") {
105
+ throw new Error("signRelease: private key is not Ed25519 (got " + keyObj.asymmetricKeyType + ")");
106
+ }
107
+ const manifest = {
108
+ buildId: parts.buildId || null,
109
+ polymorphismFingerprint: parts.polymorphismFingerprint || null,
110
+ label: parts.label || null,
111
+ files: parts.files.map(f => ({ name: f.name, sha256: f.sha256 })),
112
+ };
113
+ const canonical = canonicalize(manifest);
114
+ // Ed25519 in Node: pass key + Buffer to crypto.sign(null, ...) — the
115
+ // algorithm name MUST be null for EdDSA (the algorithm is baked into
116
+ // the key type).
117
+ const sig = crypto.sign(null, Buffer.from(canonical, "utf8"), keyObj);
118
+
119
+ const pubObj = crypto.createPublicKey(keyObj);
120
+ return {
121
+ v: VERSION,
122
+ signedAt: new Date().toISOString(),
123
+ manifest: manifest,
124
+ publicKey: pubObj.export({ type: "spki", format: "der" }).toString("base64"),
125
+ signature: sig.toString("base64"),
126
+ };
127
+ }
128
+
129
+ // ---- verify ----------------------------------------------------------------
130
+ //
131
+ // Two-stage verification. Stage 1 confirms the signature is valid for
132
+ // the embedded manifest under the supplied (or embedded) public key.
133
+ // Stage 2 (optional, on by default) re-hashes the referenced files on
134
+ // disk and confirms the hashes still match — this is what catches
135
+ // post-signing tampering of the protected artifacts themselves.
136
+ //
137
+ // Returns: { valid, stage1, stage2, mismatches: [...], error: string? }
138
+ //
139
+ // valid: overall pass/fail
140
+ // stage1: signature verified against (manifest, publicKey)
141
+ // stage2: every referenced file's on-disk sha256 matched the manifest
142
+ // mismatches: list of { name, expected, actual } when stage2 fails
143
+ //
144
+ // `expectedPublicKeyPem` is optional. When provided, we verify the
145
+ // embedded public key matches the one the caller trusts (so a sneaky
146
+ // envelope can't substitute its own key with a self-signed manifest).
147
+ function verifyRelease(envelope, opts) {
148
+ opts = opts || {};
149
+ const result = {
150
+ valid: false, stage1: false, stage2: false,
151
+ mismatches: [], error: null,
152
+ };
153
+ if (!envelope || typeof envelope !== "object") {
154
+ result.error = "envelope is not an object";
155
+ return result;
156
+ }
157
+ if (envelope.v !== VERSION) {
158
+ result.error = "unsupported envelope version: " + envelope.v;
159
+ return result;
160
+ }
161
+ if (!envelope.signature || !envelope.publicKey || !envelope.manifest) {
162
+ result.error = "envelope missing signature / publicKey / manifest";
163
+ return result;
164
+ }
165
+
166
+ // Pin to a trusted public key when the caller supplied one.
167
+ let pubKey;
168
+ if (opts.expectedPublicKeyPem) {
169
+ pubKey = crypto.createPublicKey({ key: opts.expectedPublicKeyPem, format: "pem" });
170
+ const embeddedDerB64 = envelope.publicKey;
171
+ const trustedDerB64 = pubKey.export({ type: "spki", format: "der" }).toString("base64");
172
+ if (embeddedDerB64 !== trustedDerB64) {
173
+ result.error = "embedded public key does not match --public-key";
174
+ return result;
175
+ }
176
+ } else {
177
+ pubKey = crypto.createPublicKey({
178
+ key: Buffer.from(envelope.publicKey, "base64"),
179
+ format: "der", type: "spki",
180
+ });
181
+ }
182
+
183
+ // Stage 1: signature verification over canonical manifest.
184
+ const canonical = canonicalize(envelope.manifest);
185
+ let stage1ok = false;
186
+ try {
187
+ stage1ok = crypto.verify(null,
188
+ Buffer.from(canonical, "utf8"),
189
+ pubKey,
190
+ Buffer.from(envelope.signature, "base64"));
191
+ } catch (e) {
192
+ result.error = "verify failed: " + e.message;
193
+ return result;
194
+ }
195
+ result.stage1 = stage1ok;
196
+ if (!stage1ok) {
197
+ result.error = "signature does not match manifest";
198
+ return result;
199
+ }
200
+
201
+ // Stage 2 (optional): re-hash files on disk.
202
+ if (opts.fileRoot && Array.isArray(envelope.manifest.files)) {
203
+ for (const f of envelope.manifest.files) {
204
+ const p = path.join(opts.fileRoot, f.name);
205
+ if (!fs.existsSync(p)) {
206
+ result.mismatches.push({ name: f.name, expected: f.sha256, actual: null, missing: true });
207
+ continue;
208
+ }
209
+ const actual = sha256OfFile(p);
210
+ if (actual !== f.sha256) {
211
+ result.mismatches.push({ name: f.name, expected: f.sha256, actual: actual });
212
+ }
213
+ }
214
+ result.stage2 = result.mismatches.length === 0;
215
+ result.valid = result.stage1 && result.stage2;
216
+ if (!result.valid && !result.error) {
217
+ result.error = "one or more files were modified after signing";
218
+ }
219
+ } else {
220
+ // No file root means signature-only verification.
221
+ result.stage2 = true; // n/a
222
+ result.valid = result.stage1;
223
+ }
224
+ return result;
225
+ }
226
+
227
+ function sha256OfFile(p) {
228
+ const h = crypto.createHash("sha256");
229
+ h.update(fs.readFileSync(p));
230
+ return h.digest("hex");
231
+ }
232
+
233
+ function sha256OfBuffer(buf) {
234
+ return crypto.createHash("sha256").update(buf).digest("hex");
235
+ }
236
+
237
+ module.exports = {
238
+ VERSION,
239
+ canonicalize,
240
+ generateKeyPair,
241
+ signRelease,
242
+ verifyRelease,
243
+ sha256OfFile,
244
+ sha256OfBuffer,
245
+ };
package/rollup.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { ProtectionOptions } from "./index";
2
+
3
+ export interface JsoProtectorPluginOptions extends ProtectionOptions {
4
+ removeSourceMaps?: boolean;
5
+ }
6
+
7
+ declare function jsoProtector(options?: JsoProtectorPluginOptions): {
8
+ name: "jso-protector";
9
+ generateBundle(outputOptions: unknown, bundle: Record<string, unknown>): Promise<void>;
10
+ };
11
+
12
+ export { jsoProtector };
13
+ export default jsoProtector;
package/rollup.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+
3
+ const { assertSizeBudgets, buildItemsManifest, buildProtectionItemsFromInputItems, composeProtectionItemOutput, createProtectionConfig, isExcluded, protectItems, writeManifest } = require("./index.js");
4
+
5
+ function jsoProtector(options = {}) {
6
+ let config = null;
7
+ const removeSourceMaps = options.removeSourceMaps !== false;
8
+
9
+ return {
10
+ name: "jso-protector",
11
+ async generateBundle(outputOptions, bundle) {
12
+ if (!config) {
13
+ const outDir = outputOptions && outputOptions.dir ? outputOptions.dir : "dist";
14
+ config = createProtectionConfig({
15
+ input: outDir,
16
+ output: outDir,
17
+ ...options
18
+ });
19
+ }
20
+
21
+ const chunks = Object.values(bundle).filter((entry) => (
22
+ entry &&
23
+ entry.type === "chunk" &&
24
+ typeof entry.code === "string" &&
25
+ entry.fileName &&
26
+ entry.fileName.endsWith(".js") &&
27
+ shouldProtectAsset(entry.fileName, config, options)
28
+ ));
29
+
30
+ if (!chunks.length) return;
31
+
32
+ const inputItems = chunks.map((chunk) => ({
33
+ FileName: chunk.fileName,
34
+ FileCode: chunk.code
35
+ }));
36
+ const protection = buildProtectionItemsFromInputItems(config, inputItems);
37
+ const result = await protectItems(config, protection.items);
38
+ const manifest = buildItemsManifest(config, inputItems, result, (fileName) => fileName, protection.transforms);
39
+ assertSizeBudgets(manifest, config);
40
+
41
+ for (const chunk of chunks) {
42
+ chunk.code = composeProtectionItemOutput(chunk.fileName, result, protection.transforms, config);
43
+ if (removeSourceMaps) {
44
+ chunk.map = null;
45
+ delete bundle[`${chunk.fileName}.map`];
46
+ }
47
+ }
48
+
49
+ if (config.manifest) {
50
+ writeManifest(config.manifest, manifest);
51
+ }
52
+ }
53
+ };
54
+ }
55
+
56
+ function normalizeName(name) {
57
+ return String(name || "").replace(/\\/g, "/");
58
+ }
59
+
60
+ function shouldProtectAsset(fileName, config, options) {
61
+ const normalized = normalizeName(fileName);
62
+ const include = Array.isArray(config.include) && config.include.length ? config.include : options.include;
63
+ if (Array.isArray(include) && include.length && !isExcluded(normalized, include)) {
64
+ return false;
65
+ }
66
+ return !isExcluded(normalized, config.exclude || []);
67
+ }
68
+
69
+ module.exports = jsoProtector;
70
+ module.exports.jsoProtector = jsoProtector;
71
+ module.exports.default = jsoProtector;
@@ -0,0 +1,13 @@
1
+ import type { JsoProtectorWebpackLoaderOptions } from "./webpack-loader";
2
+
3
+ export interface JsoProtectorRspackLoaderOptions extends JsoProtectorWebpackLoaderOptions {}
4
+
5
+ declare function jsoProtectorRspackLoader(
6
+ this: unknown,
7
+ source: string | Buffer,
8
+ sourceMap?: unknown,
9
+ meta?: unknown
10
+ ): void;
11
+
12
+ export { jsoProtectorRspackLoader };
13
+ export default jsoProtectorRspackLoader;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ const webpackLoader = require("./webpack-loader.js");
4
+
5
+ function jsoProtectorRspackLoader(source, sourceMap, meta) {
6
+ return webpackLoader.call(this, source, sourceMap, meta);
7
+ }
8
+
9
+ module.exports = jsoProtectorRspackLoader;
10
+ module.exports.jsoProtectorRspackLoader = jsoProtectorRspackLoader;
11
+ module.exports.default = jsoProtectorRspackLoader;
package/rspack.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { JsoProtectorWebpackPluginOptions } from "./webpack";
2
+
3
+ export interface JsoProtectorRspackPluginOptions extends JsoProtectorWebpackPluginOptions {}
4
+
5
+ declare class JsoProtectorRspackPlugin {
6
+ constructor(options?: JsoProtectorRspackPluginOptions);
7
+ apply(compiler: unknown): void;
8
+ }
9
+
10
+ export { JsoProtectorRspackPlugin };
11
+ export default JsoProtectorRspackPlugin;
package/rspack.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ const WebpackProtector = require("./webpack.js");
4
+
5
+ class JsoProtectorRspackPlugin extends WebpackProtector {}
6
+
7
+ module.exports = JsoProtectorRspackPlugin;
8
+ module.exports.JsoProtectorRspackPlugin = JsoProtectorRspackPlugin;
9
+ module.exports.default = JsoProtectorRspackPlugin;