xoonya 3.1.13 → 3.1.15

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.
@@ -1,86 +1,86 @@
1
- "use strict";
2
-
3
- // Verifies the optional paywall flag (XOONYA_REQUIRE_PAID_VERIFICATION) for the FREE
4
- // direct verification routes. Default OFF => routes stay free (non-breaking). ON => the
5
- // five crypto-verify routes that have a metered paid equivalent return 402 pointing to
6
- // the paid x402 lane, while /execute, /health and descriptor GETs stay free.
7
-
8
- const assert = require("assert");
9
- const crypto = require("crypto");
10
- const http = require("http");
11
-
12
- const b64url = (buf) => Buffer.from(buf).toString("base64url");
13
- function makeJws(payloadObj, privateKey) {
14
- const protectedB64 = b64url(Buffer.from(JSON.stringify({ alg: "EdDSA" }), "utf8"));
15
- const payloadB64 = b64url(Buffer.from(JSON.stringify(payloadObj), "utf8"));
16
- const signingInput = `${protectedB64}.${payloadB64}`;
17
- return `${signingInput}.${b64url(crypto.sign(null, Buffer.from(signingInput, "utf8"), privateKey))}`;
18
- }
19
-
20
- async function post(baseUrl, path, body) {
21
- const res = await fetch(`${baseUrl}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
22
- let json = null; try { json = await res.json(); } catch (_) { json = null; }
23
- return { status: res.status, json };
24
- }
25
-
26
- const GATED = [
27
- ["/v1/proof-center/import-adapter/verify-signature", "proof.verify-signature"],
28
- ["/v1/proof-bundles/verify", "proof.verify-bundle"],
29
- ["/v1/proofs/verify", "proof.verify-envelope"],
30
- ["/validate", "proof.verify-receipt"],
31
- ["/v1/receipts/verify", "proof.verify-receipt"]
32
- ];
33
-
34
- async function main() {
35
- const prevFlag = process.env.XOONYA_REQUIRE_PAID_VERIFICATION;
36
- const prevBearer = process.env.XOONYA_EXECUTOR_BEARER_TOKEN;
37
- delete process.env.XOONYA_REQUIRE_PAID_VERIFICATION;
38
- process.env.XOONYA_EXECUTOR_BEARER_TOKEN = "paywall-test-bearer";
39
-
40
- delete require.cache[require.resolve("../server.js")];
41
- const { createServer } = require("../server.js");
42
- const server = createServer();
43
- await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
44
- const baseUrl = `http://127.0.0.1:${server.address().port}`;
45
-
46
- const ed = crypto.generateKeyPairSync("ed25519");
47
- const edPubPem = ed.publicKey.export({ type: "spki", format: "pem" });
48
- const jws = makeJws({ sub: "a" }, ed.privateKey);
49
-
50
- try {
51
- // Flag OFF (default): free direct verification works unchanged (non-breaking).
52
- let r = await post(baseUrl, "/v1/proof-center/import-adapter/verify-signature", { signature_verification: { jws, trusted_public_key: edPubPem } });
53
- assert.strictEqual(r.status, 200, "flag off: verify-signature free (200)");
54
- assert.strictEqual(r.json.signature_valid, true, "flag off: real verdict returned free");
55
-
56
- // Flag ON: every gated route returns 402 pointing to the paid lane.
57
- process.env.XOONYA_REQUIRE_PAID_VERIFICATION = "true";
58
- for (const [path, tool] of GATED) {
59
- r = await post(baseUrl, path, {});
60
- assert.strictEqual(r.status, 402, `flag on: ${path} must 402`);
61
- assert.strictEqual(r.json.reason, "direct_verification_paywalled", `flag on: ${path} paywall reason`);
62
- assert.ok(r.json.paid_lane && r.json.paid_lane.task_ref === "adapter.xoonya.invoke", `flag on: ${path} points to paid lane`);
63
- assert.strictEqual(r.json.paid_lane.tool_name, tool, `flag on: ${path} names the paid tool`);
64
- }
65
- // And the paywalled route no longer performs the free verification (402, not 200).
66
- r = await post(baseUrl, "/v1/proof-center/import-adapter/verify-signature", { signature_verification: { jws, trusted_public_key: edPubPem } });
67
- assert.strictEqual(r.status, 402, "flag on: a valid request is still paywalled (no free bypass)");
68
-
69
- // Flag ON must NOT paywall /execute (the paid backend) or /health (free).
70
- const exec = await fetch(`${baseUrl}/execute`, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
71
- assert.notStrictEqual(exec.status, 402, "/execute must not be paywalled (it IS the paid path; 401 without bearer here)");
72
- const health = await fetch(`${baseUrl}/health`);
73
- assert.strictEqual(health.status, 200, "/health stays free");
74
-
75
- console.log("xoonya verify:paywall-flag passed");
76
- } finally {
77
- await new Promise((resolve) => server.close(resolve));
78
- if (prevFlag === undefined) delete process.env.XOONYA_REQUIRE_PAID_VERIFICATION; else process.env.XOONYA_REQUIRE_PAID_VERIFICATION = prevFlag;
79
- if (prevBearer === undefined) delete process.env.XOONYA_EXECUTOR_BEARER_TOKEN; else process.env.XOONYA_EXECUTOR_BEARER_TOKEN = prevBearer;
80
- }
81
- }
82
-
83
- main().catch((error) => {
84
- console.error(error && error.message ? error.message : error);
85
- process.exitCode = 1;
86
- });
1
+ "use strict";
2
+
3
+ // Verifies the optional paywall flag (XOONYA_REQUIRE_PAID_VERIFICATION) for the FREE
4
+ // direct verification routes. Default OFF => routes stay free (non-breaking). ON => the
5
+ // five crypto-verify routes that have a metered paid equivalent return 402 pointing to
6
+ // the paid x402 lane, while /execute, /health and descriptor GETs stay free.
7
+
8
+ const assert = require("assert");
9
+ const crypto = require("crypto");
10
+ const http = require("http");
11
+
12
+ const b64url = (buf) => Buffer.from(buf).toString("base64url");
13
+ function makeJws(payloadObj, privateKey) {
14
+ const protectedB64 = b64url(Buffer.from(JSON.stringify({ alg: "EdDSA" }), "utf8"));
15
+ const payloadB64 = b64url(Buffer.from(JSON.stringify(payloadObj), "utf8"));
16
+ const signingInput = `${protectedB64}.${payloadB64}`;
17
+ return `${signingInput}.${b64url(crypto.sign(null, Buffer.from(signingInput, "utf8"), privateKey))}`;
18
+ }
19
+
20
+ async function post(baseUrl, path, body) {
21
+ const res = await fetch(`${baseUrl}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
22
+ let json = null; try { json = await res.json(); } catch (_) { json = null; }
23
+ return { status: res.status, json };
24
+ }
25
+
26
+ const GATED = [
27
+ ["/v1/proof-center/import-adapter/verify-signature", "proof.verify-signature"],
28
+ ["/v1/proof-bundles/verify", "proof.verify-bundle"],
29
+ ["/v1/proofs/verify", "proof.verify-envelope"],
30
+ ["/validate", "proof.verify-receipt"],
31
+ ["/v1/receipts/verify", "proof.verify-receipt"]
32
+ ];
33
+
34
+ async function main() {
35
+ const prevFlag = process.env.XOONYA_REQUIRE_PAID_VERIFICATION;
36
+ const prevBearer = process.env.XOONYA_EXECUTOR_BEARER_TOKEN;
37
+ delete process.env.XOONYA_REQUIRE_PAID_VERIFICATION;
38
+ process.env.XOONYA_EXECUTOR_BEARER_TOKEN = "paywall-test-bearer";
39
+
40
+ delete require.cache[require.resolve("../server.js")];
41
+ const { createServer } = require("../server.js");
42
+ const server = createServer();
43
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
44
+ const baseUrl = `http://127.0.0.1:${server.address().port}`;
45
+
46
+ const ed = crypto.generateKeyPairSync("ed25519");
47
+ const edPubPem = ed.publicKey.export({ type: "spki", format: "pem" });
48
+ const jws = makeJws({ sub: "a" }, ed.privateKey);
49
+
50
+ try {
51
+ // Flag OFF (default): free direct verification works unchanged (non-breaking).
52
+ let r = await post(baseUrl, "/v1/proof-center/import-adapter/verify-signature", { signature_verification: { jws, trusted_public_key: edPubPem } });
53
+ assert.strictEqual(r.status, 200, "flag off: verify-signature free (200)");
54
+ assert.strictEqual(r.json.signature_valid, true, "flag off: real verdict returned free");
55
+
56
+ // Flag ON: every gated route returns 402 pointing to the paid lane.
57
+ process.env.XOONYA_REQUIRE_PAID_VERIFICATION = "true";
58
+ for (const [path, tool] of GATED) {
59
+ r = await post(baseUrl, path, {});
60
+ assert.strictEqual(r.status, 402, `flag on: ${path} must 402`);
61
+ assert.strictEqual(r.json.reason, "direct_verification_paywalled", `flag on: ${path} paywall reason`);
62
+ assert.ok(r.json.paid_lane && r.json.paid_lane.task_ref === "adapter.xoonya.invoke", `flag on: ${path} points to paid lane`);
63
+ assert.strictEqual(r.json.paid_lane.tool_name, tool, `flag on: ${path} names the paid tool`);
64
+ }
65
+ // And the paywalled route no longer performs the free verification (402, not 200).
66
+ r = await post(baseUrl, "/v1/proof-center/import-adapter/verify-signature", { signature_verification: { jws, trusted_public_key: edPubPem } });
67
+ assert.strictEqual(r.status, 402, "flag on: a valid request is still paywalled (no free bypass)");
68
+
69
+ // Flag ON must NOT paywall /execute (the paid backend) or /health (free).
70
+ const exec = await fetch(`${baseUrl}/execute`, { method: "POST", headers: { "content-type": "application/json" }, body: "{}" });
71
+ assert.notStrictEqual(exec.status, 402, "/execute must not be paywalled (it IS the paid path; 401 without bearer here)");
72
+ const health = await fetch(`${baseUrl}/health`);
73
+ assert.strictEqual(health.status, 200, "/health stays free");
74
+
75
+ console.log("xoonya verify:paywall-flag passed");
76
+ } finally {
77
+ await new Promise((resolve) => server.close(resolve));
78
+ if (prevFlag === undefined) delete process.env.XOONYA_REQUIRE_PAID_VERIFICATION; else process.env.XOONYA_REQUIRE_PAID_VERIFICATION = prevFlag;
79
+ if (prevBearer === undefined) delete process.env.XOONYA_EXECUTOR_BEARER_TOKEN; else process.env.XOONYA_EXECUTOR_BEARER_TOKEN = prevBearer;
80
+ }
81
+ }
82
+
83
+ main().catch((error) => {
84
+ console.error(error && error.message ? error.message : error);
85
+ process.exitCode = 1;
86
+ });