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
package/watermark.js ADDED
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ // Watermarking for jso-protector.
4
+ //
5
+ // What it does: before source goes to the obfuscation API, the CLI
6
+ // prepends a header-comment block carrying an HMAC-SHA256 over a
7
+ // caller-supplied tag. The obfuscator's existing "Keep Header Comment"
8
+ // option preserves the block through every transform (string array
9
+ // moves, encryption, control-flow flattening, dead code, etc.), so the
10
+ // marker survives intact in the protected output.
11
+ //
12
+ // Use cases:
13
+ // - Anti-piracy: "this build came from our official pipeline; here is
14
+ // the cryptographic proof"
15
+ // - License-enforcement: bind a per-customer tag to the obfuscated
16
+ // artifact so a leak can be traced back
17
+ // - Forensics: dispute that an attacker claims to have produced the
18
+ // code themselves — the watermark verifies otherwise
19
+ //
20
+ // Why a header comment and not a string literal inside the program?
21
+ // The obfuscator preserves header comments verbatim when KeepComment
22
+ // is true. String literals get encoded / encrypted / moved into the
23
+ // string array, so a verifier would need to reverse the obfuscation
24
+ // to find them. The header-comment path is robust and zero-friction.
25
+ //
26
+ // Format (versioned so we can evolve without breaking old artifacts):
27
+ //
28
+ // /*! __jso_watermark_v1
29
+ // * tag: <base64url>
30
+ // * sig: <base64url HMAC-SHA256(tag, key)>
31
+ // */
32
+ //
33
+ // The `/*!` opening is the convention preserved by most minifiers /
34
+ // transforms (it's a "bang comment"); we use it so the marker survives
35
+ // even if a customer pipes the protected output through a second tool.
36
+
37
+ const crypto = require("node:crypto");
38
+
39
+ const MARKER = "__jso_watermark_v1";
40
+ const MARKER_RE = /\/\*!?\s*__jso_watermark_v1\s+\*\s*tag:\s*([A-Za-z0-9_-]+)\s*\*\s*sig:\s*([A-Za-z0-9_-]+)\s*\*\//;
41
+
42
+ function b64url(buf) {
43
+ return Buffer.from(buf).toString("base64")
44
+ .replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
45
+ }
46
+
47
+ function b64urlDecode(str) {
48
+ str = String(str || "").replace(/-/g, "+").replace(/_/g, "/");
49
+ while (str.length % 4) str += "=";
50
+ return Buffer.from(str, "base64");
51
+ }
52
+
53
+ // Compute the canonical HMAC-SHA256 over a watermark tag. Returns the
54
+ // signature as base64url-encoded bytes. The tag itself is the plaintext
55
+ // caller-supplied identifier (e.g. customer-id, release-id, license
56
+ // number) — we sign it so a leaked artifact can be authenticated even
57
+ // when the tag is publicly visible inside the protected file.
58
+ function signTag(tag, key) {
59
+ if (!tag) throw new Error("watermark: tag is required");
60
+ if (!key) throw new Error("watermark: key is required");
61
+ const h = crypto.createHmac("sha256", key);
62
+ h.update(Buffer.from(String(tag), "utf8"));
63
+ return b64url(h.digest());
64
+ }
65
+
66
+ // Build the header-comment block. Slot it at the very top of the file
67
+ // so the obfuscator treats it as the file's leading comment.
68
+ function buildHeader(tag, key) {
69
+ const tagB = b64url(Buffer.from(String(tag), "utf8"));
70
+ const sigB = signTag(tag, key);
71
+ return "/*! " + MARKER + "\n"
72
+ + " * tag: " + tagB + "\n"
73
+ + " * sig: " + sigB + "\n"
74
+ + " */\n";
75
+ }
76
+
77
+ // Inject the header at the start of source. If the file begins with
78
+ // `"use strict"` directive it stays at line 1 (V8 only honors it when
79
+ // it's the literal first statement); we put the comment ABOVE it,
80
+ // which is still semantically valid because comments don't break the
81
+ // directive prologue.
82
+ function injectInto(source, tag, key) {
83
+ return buildHeader(tag, key) + (source || "");
84
+ }
85
+
86
+ // Scan protected output for the watermark marker. Returns
87
+ // { present, tag, sig, valid } where:
88
+ // present: true iff the marker block was found
89
+ // tag: plaintext tag (already base64url-decoded back to a string)
90
+ // sig: the embedded base64url signature
91
+ // valid: true iff HMAC(tag, key) matches sig (constant-time)
92
+ // error: set when the file lacks a marker or the parse fails
93
+ function verify(protectedSource, key) {
94
+ const src = String(protectedSource || "");
95
+ const m = MARKER_RE.exec(src);
96
+ if (!m) {
97
+ return { present: false, valid: false, error: "watermark: marker not found in input" };
98
+ }
99
+ const tagB64 = m[1];
100
+ const sigB64 = m[2];
101
+ let tag;
102
+ try { tag = b64urlDecode(tagB64).toString("utf8"); }
103
+ catch (e) { return { present: true, valid: false, error: "watermark: tag is not valid base64url" }; }
104
+
105
+ if (!key) {
106
+ // Lookup-only mode: caller just wants to read the embedded tag.
107
+ return { present: true, tag: tag, sig: sigB64, valid: false };
108
+ }
109
+ const expected = signTag(tag, key);
110
+ // Constant-time compare to defeat timing attacks against a verifier
111
+ // that's exposed publicly (e.g. webhook that checks if an uploaded
112
+ // artifact is "ours").
113
+ const a = Buffer.from(expected, "utf8");
114
+ const b = Buffer.from(sigB64, "utf8");
115
+ const valid = a.length === b.length && crypto.timingSafeEqual(a, b);
116
+ return { present: true, tag: tag, sig: sigB64, valid: valid };
117
+ }
118
+
119
+ module.exports = {
120
+ MARKER,
121
+ MARKER_RE,
122
+ signTag,
123
+ buildHeader,
124
+ injectInto,
125
+ verify,
126
+ };
@@ -0,0 +1,15 @@
1
+ import type { ProtectionOptions } from "./index";
2
+
3
+ export interface JsoProtectorWebpackLoaderOptions extends ProtectionOptions {
4
+ fileName?: string;
5
+ }
6
+
7
+ declare function jsoProtectorLoader(
8
+ this: unknown,
9
+ source: string | Buffer,
10
+ sourceMap?: unknown,
11
+ meta?: unknown
12
+ ): void;
13
+
14
+ export { jsoProtectorLoader };
15
+ export default jsoProtectorLoader;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+ const { assertSizeBudgets, buildItemsManifest, buildProtectionItemsFromInputItems, composeProtectionItemOutput, createProtectionConfig, isExcluded, protectItems, writeManifest } = require("./index.js");
5
+
6
+ function jsoProtectorLoader(source, sourceMap, meta) {
7
+ const callback = typeof this.async === "function" ? this.async() : null;
8
+ if (!callback) {
9
+ throw new Error("jso-protector webpack loader requires an async loader context.");
10
+ }
11
+ if (typeof this.cacheable === "function") {
12
+ this.cacheable(false);
13
+ }
14
+
15
+ const options = readLoaderOptions(this);
16
+ const config = createProtectionConfig(options);
17
+ const fileName = normalizeName(options.fileName || resourceNameForContext(this));
18
+ if (!shouldProtect(fileName, config)) {
19
+ callback(null, source, sourceMap, meta);
20
+ return;
21
+ }
22
+
23
+ const code = Buffer.isBuffer(source) ? source.toString("utf8") : String(source || "");
24
+ const inputItems = [{
25
+ FileName: fileName,
26
+ FileCode: code,
27
+ SourcePath: this.resourcePath || fileName,
28
+ OutputPath: this.resourcePath || fileName
29
+ }];
30
+
31
+ const protection = buildProtectionItemsFromInputItems(config, inputItems);
32
+
33
+ protectItems(config, protection.items)
34
+ .then((result) => {
35
+ const manifest = buildItemsManifest(config, inputItems, result, () => this.resourcePath || fileName, protection.transforms);
36
+ assertSizeBudgets(manifest, config);
37
+ if (config.manifest) writeManifest(config.manifest, manifest);
38
+
39
+ callback(null, composeProtectionItemOutput(fileName, result, protection.transforms, config), null, meta);
40
+ })
41
+ .catch((error) => callback(error));
42
+ }
43
+
44
+ function readLoaderOptions(context) {
45
+ if (typeof context.getOptions === "function") {
46
+ return context.getOptions() || {};
47
+ }
48
+ const query = context.query;
49
+ if (!query) return {};
50
+ if (typeof query === "object") return query;
51
+ if (typeof query !== "string") return {};
52
+ const text = query.startsWith("?") ? query.slice(1) : query;
53
+ if (!text) return {};
54
+ const options = {};
55
+ for (const [key, value] of new URLSearchParams(text).entries()) {
56
+ options[key] = parseQueryValue(value);
57
+ }
58
+ return options;
59
+ }
60
+
61
+ function parseQueryValue(value) {
62
+ if (/^(true|false)$/i.test(value)) return value.toLowerCase() === "true";
63
+ if (/^-?\d+(\.\d+)?$/.test(value)) return Number(value);
64
+ return value;
65
+ }
66
+
67
+ function resourceNameForContext(context) {
68
+ const resourcePath = context.resourcePath || "";
69
+ if (!resourcePath) return "module.js";
70
+ const root = context.rootContext || context.context || process.cwd();
71
+ const relative = path.relative(root, resourcePath);
72
+ return normalizeName(relative && !relative.startsWith("..") ? relative : path.basename(resourcePath));
73
+ }
74
+
75
+ function shouldProtect(fileName, config) {
76
+ if (!matchesProtectedScriptExtension(fileName, config)) return false;
77
+ if (Array.isArray(config.include) && config.include.length && !isExcluded(fileName, config.include)) {
78
+ return false;
79
+ }
80
+ return !isExcluded(fileName, config.exclude || []);
81
+ }
82
+
83
+ function matchesProtectedScriptExtension(fileName, config) {
84
+ const normalizedName = normalizeName(fileName).toLowerCase();
85
+ const extensions = Array.isArray(config.extensions) && config.extensions.length
86
+ ? config.extensions
87
+ : [".js"];
88
+ return extensions.some((extension) => normalizedName.endsWith(String(extension || "").toLowerCase()));
89
+ }
90
+
91
+ function normalizeName(name) {
92
+ return String(name || "").replace(/\\/g, "/");
93
+ }
94
+
95
+ module.exports = jsoProtectorLoader;
96
+ module.exports.jsoProtectorLoader = jsoProtectorLoader;
97
+ module.exports.default = jsoProtectorLoader;
package/webpack.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { ProtectionOptions } from "./index";
2
+
3
+ export interface JsoProtectorWebpackPluginOptions extends ProtectionOptions {
4
+ removeSourceMaps?: boolean;
5
+ }
6
+
7
+ declare class JsoProtectorWebpackPlugin {
8
+ constructor(options?: JsoProtectorWebpackPluginOptions);
9
+ apply(compiler: unknown): void;
10
+ }
11
+
12
+ export { JsoProtectorWebpackPlugin };
13
+ export default JsoProtectorWebpackPlugin;
package/webpack.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+
3
+ const { assertSizeBudgets, buildItemsManifest, buildProtectionItemsFromInputItems, composeProtectionItemOutput, createProtectionConfig, isExcluded, protectItems, writeManifest } = require("./index.js");
4
+
5
+ class JsoProtectorWebpackPlugin {
6
+ constructor(options = {}) {
7
+ this.options = options;
8
+ this.removeSourceMaps = options.removeSourceMaps !== false;
9
+ }
10
+
11
+ apply(compiler) {
12
+ const pluginName = "JsoProtectorWebpackPlugin";
13
+ const processAssetsHook = compiler.hooks && compiler.hooks.thisCompilation;
14
+ if (processAssetsHook && compiler.webpack && compiler.webpack.sources && compiler.webpack.Compilation) {
15
+ const RawSource = compiler.webpack.sources.RawSource;
16
+ const stage = compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE;
17
+
18
+ processAssetsHook.tap(pluginName, (compilation) => {
19
+ compilation.hooks.processAssets.tapPromise({ name: pluginName, stage }, async (assets) => {
20
+ await this.protectCompilationAssets(compiler, compilation, assets, (value) => new RawSource(value));
21
+ });
22
+ });
23
+ return;
24
+ }
25
+
26
+ if (compiler.hooks && compiler.hooks.emit) {
27
+ compiler.hooks.emit.tapPromise(pluginName, async (compilation) => {
28
+ await this.protectCompilationAssets(compiler, compilation, compilation.assets, createRawSourceAsset);
29
+ });
30
+ }
31
+ }
32
+
33
+ async protectCompilationAssets(compiler, compilation, assets, createAsset) {
34
+ const outputPath = compiler.options && compiler.options.output && compiler.options.output.path
35
+ ? compiler.options.output.path
36
+ : "dist";
37
+ const config = createProtectionConfig({
38
+ input: outputPath,
39
+ output: outputPath,
40
+ ...this.options
41
+ });
42
+
43
+ const names = Object.keys(assets || {}).filter((name) => (
44
+ matchesProtectedScriptExtension(name, config) &&
45
+ shouldProtectAsset(name, config, this.options)
46
+ ));
47
+ if (!names.length) return;
48
+
49
+ const inputItems = names.map((name) => ({
50
+ FileName: name,
51
+ FileCode: assetToString(getAssetSource(compilation, assets, name))
52
+ }));
53
+ const protection = buildProtectionItemsFromInputItems(config, inputItems);
54
+ const result = await protectItems(config, protection.items);
55
+ const manifest = buildItemsManifest(config, inputItems, result, (fileName) => fileName, protection.transforms);
56
+ assertSizeBudgets(manifest, config);
57
+
58
+ for (const name of names) {
59
+ const output = composeProtectionItemOutput(name, result, protection.transforms, config);
60
+ if (typeof compilation.updateAsset === "function") {
61
+ compilation.updateAsset(name, createAsset(output));
62
+ } else if (compilation.assets) {
63
+ compilation.assets[name] = createAsset(output);
64
+ }
65
+
66
+ if (this.removeSourceMaps) {
67
+ removeSourceMapAsset(compilation, `${name}.map`);
68
+ }
69
+ }
70
+
71
+ if (config.manifest) {
72
+ writeManifest(config.manifest, manifest);
73
+ }
74
+ }
75
+ }
76
+
77
+ function getAssetSource(compilation, assets, name) {
78
+ if (compilation && typeof compilation.getAsset === "function") {
79
+ const asset = compilation.getAsset(name);
80
+ if (asset && asset.source) return asset.source;
81
+ }
82
+ return assets ? assets[name] : null;
83
+ }
84
+
85
+ function removeSourceMapAsset(compilation, name) {
86
+ if (!compilation) return;
87
+ if (typeof compilation.deleteAsset === "function") {
88
+ compilation.deleteAsset(name);
89
+ return;
90
+ }
91
+ if (compilation.assets && Object.prototype.hasOwnProperty.call(compilation.assets, name)) {
92
+ delete compilation.assets[name];
93
+ }
94
+ }
95
+
96
+ function createRawSourceAsset(value) {
97
+ return {
98
+ source() {
99
+ return value;
100
+ },
101
+ size() {
102
+ return Buffer.byteLength(value, "utf8");
103
+ }
104
+ };
105
+ }
106
+
107
+ function assetToString(assetOrSource) {
108
+ if (!assetOrSource) return "";
109
+ if (typeof assetOrSource.source === "function") {
110
+ const value = assetOrSource.source();
111
+ return Buffer.isBuffer(value) ? value.toString("utf8") : String(value || "");
112
+ }
113
+ if (assetOrSource.source) {
114
+ return assetToString(assetOrSource.source);
115
+ }
116
+ const value = assetOrSource;
117
+ return Buffer.isBuffer(value) ? value.toString("utf8") : String(value || "");
118
+ }
119
+
120
+ function normalizeName(name) {
121
+ return String(name || "").replace(/\\/g, "/");
122
+ }
123
+
124
+ function shouldProtectAsset(fileName, config, options) {
125
+ const normalized = normalizeName(fileName);
126
+ const include = Array.isArray(config.include) && config.include.length ? config.include : options.include;
127
+ if (Array.isArray(include) && include.length && !isExcluded(normalized, include)) {
128
+ return false;
129
+ }
130
+ return !isExcluded(normalized, config.exclude || []);
131
+ }
132
+
133
+ function matchesProtectedScriptExtension(fileName, config) {
134
+ const normalizedName = normalizeName(fileName).toLowerCase();
135
+ const extensions = Array.isArray(config.extensions) && config.extensions.length
136
+ ? config.extensions
137
+ : [".js"];
138
+ return extensions.some((extension) => normalizedName.endsWith(String(extension || "").toLowerCase()));
139
+ }
140
+
141
+ module.exports = JsoProtectorWebpackPlugin;
142
+ module.exports.JsoProtectorWebpackPlugin = JsoProtectorWebpackPlugin;
143
+ module.exports.default = JsoProtectorWebpackPlugin;