@secrustapp/shield 0.0.0-stage → 0.1.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 +17 -0
- package/README.md +34 -2
- package/index.js +158 -0
- package/package.json +14 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
secrustapp — Proprietary License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 secrustapp (of-a-tech). All rights reserved.
|
|
4
|
+
|
|
5
|
+
1. GRANT. This repository and its contents (documentation, protocol
|
|
6
|
+
descriptions, and compiled artifacts where distributed) are provided for
|
|
7
|
+
evaluation and integration planning. No rights are granted to modify,
|
|
8
|
+
decompile, disassemble, or reverse engineer any compiled secrustapp
|
|
9
|
+
artifact, except to the extent such restriction is prohibited by law.
|
|
10
|
+
|
|
11
|
+
2. EVALUATION USE. You may read this documentation, run the public demo,
|
|
12
|
+
and link to it. Production use of secrustapp packages requires a
|
|
13
|
+
commercial license — request access via the repository discussions.
|
|
14
|
+
|
|
15
|
+
3. NO WARRANTY. Provided "as is", without warranty of any kind.
|
|
16
|
+
|
|
17
|
+
Questions: open a discussion in this repository.
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @secrustapp/shield
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Wrap your **existing** web app's API behind secrustapp encrypted envelopes —
|
|
4
|
+
one line in the frontend, one container in front of your backend. The app
|
|
5
|
+
itself does not change.
|
|
6
|
+
|
|
7
|
+
The browser's Network tab sees only opaque binary envelopes. Your app sees
|
|
8
|
+
plain HTTP on localhost.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
import { shield } from "@secrustapp/shield";
|
|
12
|
+
await shield();
|
|
13
|
+
// every same-origin fetch() from your SPA is now envelope-encrypted
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Pairs with the `secrustapp/shield` container:
|
|
17
|
+
|
|
18
|
+
```yaml
|
|
19
|
+
services:
|
|
20
|
+
shield:
|
|
21
|
+
image: secrustapp/shield
|
|
22
|
+
environment:
|
|
23
|
+
SHIELD_UPSTREAM: http://myapp:8080 # your app, unchanged
|
|
24
|
+
SHIELD_ORIGIN: https://app.example.com
|
|
25
|
+
ports: ["443:8443"]
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- End-to-end encrypted — nothing readable between browser and server
|
|
29
|
+
- Quantum resilient — hybrid post-quantum envelopes
|
|
30
|
+
- Per-session rate limiting; replay and expiry protection built in
|
|
31
|
+
- Limits (MVP): JSON/text/bytes bodies only (FormData/Blob/streaming fall
|
|
32
|
+
through to plain fetch); no WebSocket/SSE tunnelling yet
|
|
33
|
+
|
|
34
|
+
Demo + docs: https://secrustapp.codeshield.my/library
|
|
35
|
+
Protocol: https://github.com/of-a-tech/secrustapp
|
package/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @secrustapp/shield — wrap an existing web app's API behind encrypted envelopes.
|
|
2
|
+
//
|
|
3
|
+
// One import + one call: every same-origin fetch from your SPA is sealed into
|
|
4
|
+
// a secrustapp envelope; the Shield in front of your app terminates it and
|
|
5
|
+
// replays plain HTTP to your app on localhost. Your backend does not change.
|
|
6
|
+
//
|
|
7
|
+
// Only requests with JSON/text/bytes bodies are shielded; FormData/Blob/
|
|
8
|
+
// streaming requests fall through to plain fetch (documented limit).
|
|
9
|
+
|
|
10
|
+
import init, { SecrustClient } from "@secrustapp/client";
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Minimal CBOR (RFC 8949): null, bool, uint, text, bytes, array, map
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
const te = new TextEncoder();
|
|
17
|
+
const td = new TextDecoder();
|
|
18
|
+
|
|
19
|
+
function cborEncode(v) {
|
|
20
|
+
const parts = [];
|
|
21
|
+
const push = (...bs) => parts.push(Uint8Array.from(bs));
|
|
22
|
+
const head = (mt, n) => {
|
|
23
|
+
if (n < 24) push((mt << 5) | n);
|
|
24
|
+
else if (n < 256) push((mt << 5) | 24, n);
|
|
25
|
+
else if (n < 65536) push((mt << 5) | 25, n >> 8, n & 0xff);
|
|
26
|
+
else push((mt << 5) | 26, (n >>> 24) & 0xff, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff);
|
|
27
|
+
};
|
|
28
|
+
const enc = (v) => {
|
|
29
|
+
if (v === null || v === undefined) push(0xf6);
|
|
30
|
+
else if (v === true) push(0xf5);
|
|
31
|
+
else if (v === false) push(0xf4);
|
|
32
|
+
else if (typeof v === "number") {
|
|
33
|
+
if (!Number.isSafeInteger(v) || v < 0) throw new Error("CBOR: only non-negative safe integers");
|
|
34
|
+
head(0, v);
|
|
35
|
+
} else if (typeof v === "string") {
|
|
36
|
+
const b = te.encode(v);
|
|
37
|
+
head(3, b.length);
|
|
38
|
+
parts.push(b);
|
|
39
|
+
} else if (v instanceof Uint8Array) {
|
|
40
|
+
head(2, v.length);
|
|
41
|
+
parts.push(v);
|
|
42
|
+
} else if (Array.isArray(v)) {
|
|
43
|
+
head(4, v.length);
|
|
44
|
+
v.forEach(enc);
|
|
45
|
+
} else if (typeof v === "object") {
|
|
46
|
+
const keys = Object.keys(v);
|
|
47
|
+
head(5, keys.length);
|
|
48
|
+
keys.forEach((k) => { enc(k); enc(v[k]); });
|
|
49
|
+
} else throw new Error("CBOR: unsupported type");
|
|
50
|
+
};
|
|
51
|
+
enc(v);
|
|
52
|
+
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
53
|
+
const out = new Uint8Array(total);
|
|
54
|
+
let o = 0;
|
|
55
|
+
for (const p of parts) { out.set(p, o); o += p.length; }
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function cborDecode(bytes) {
|
|
60
|
+
let i = 0;
|
|
61
|
+
const read = (n) => { const s = bytes.subarray(i, i + n); i += n; return s; };
|
|
62
|
+
const u8 = () => read(1)[0];
|
|
63
|
+
const u16 = () => { const b = read(2); return (b[0] << 8) | b[1]; };
|
|
64
|
+
const u32 = () => { const b = read(4); return ((b[0] * 16777216) + ((b[1] << 16) | (b[2] << 8) | b[3])) >>> 0; };
|
|
65
|
+
const dec = () => {
|
|
66
|
+
const ib = u8();
|
|
67
|
+
const mt = ib >> 5, ai = ib & 31;
|
|
68
|
+
const arg = () => ai < 24 ? ai : ai === 24 ? u8() : ai === 25 ? u16() : u32();
|
|
69
|
+
switch (mt) {
|
|
70
|
+
case 0: return arg();
|
|
71
|
+
case 2: return new Uint8Array(read(arg()));
|
|
72
|
+
case 3: return td.decode(read(arg()));
|
|
73
|
+
case 4: { const n = arg(); const a = []; for (let k = 0; k < n; k++) a.push(dec()); return a; }
|
|
74
|
+
case 5: { const n = arg(); const o = {}; for (let k = 0; k < n; k++) o[dec()] = dec(); return o; }
|
|
75
|
+
case 7:
|
|
76
|
+
if (ai === 20) return false;
|
|
77
|
+
if (ai === 21) return true;
|
|
78
|
+
if (ai === 22) return null;
|
|
79
|
+
throw new Error("CBOR: unsupported simple value");
|
|
80
|
+
default: throw new Error("CBOR: unsupported type " + mt);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
return dec();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
// Shield
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
const SKIP_REQUEST_HEADERS = new Set([
|
|
91
|
+
"host", "content-length", "connection", "accept-encoding",
|
|
92
|
+
"cookie", // the Shield forwards the browser's own cookies
|
|
93
|
+
"origin", "referer",
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Wrap this page's API traffic in secrustapp envelopes.
|
|
98
|
+
*
|
|
99
|
+
* @param {object} opts
|
|
100
|
+
* - exclude: (path) => boolean — paths to leave unshielded (default: none)
|
|
101
|
+
* - baseUrl: origin override (default: location.origin)
|
|
102
|
+
*/
|
|
103
|
+
export async function shield(opts = {}) {
|
|
104
|
+
await init();
|
|
105
|
+
const baseUrl = (opts.baseUrl || (typeof location !== "undefined" ? location.origin : "")).replace(/\/$/, "");
|
|
106
|
+
let client = await SecrustClient.connect(baseUrl);
|
|
107
|
+
|
|
108
|
+
const shouldShield = (url, init) => {
|
|
109
|
+
const u = new URL(url, baseUrl);
|
|
110
|
+
if (u.origin !== baseUrl) return false;
|
|
111
|
+
if (u.pathname === "/protected" || u.pathname === "/protected/key-discovery") return false;
|
|
112
|
+
if (opts.exclude && opts.exclude(u.pathname)) return false;
|
|
113
|
+
const body = init && init.body;
|
|
114
|
+
if (body && (typeof FormData !== "undefined" && body instanceof FormData ||
|
|
115
|
+
typeof Blob !== "undefined" && body instanceof Blob)) return false;
|
|
116
|
+
return true;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const original = window.fetch.bind(window);
|
|
120
|
+
window.fetch = async (input, init = {}) => {
|
|
121
|
+
const url = typeof input === "string" ? input : input.url;
|
|
122
|
+
if (!shouldShield(url, init)) return original(input, init);
|
|
123
|
+
|
|
124
|
+
const u = new URL(url, baseUrl);
|
|
125
|
+
const method = (init.method || (input && input.method) || "GET").toUpperCase();
|
|
126
|
+
|
|
127
|
+
let body = new Uint8Array(0);
|
|
128
|
+
if (init.body) {
|
|
129
|
+
if (typeof init.body === "string") body = te.encode(init.body);
|
|
130
|
+
else if (init.body instanceof Uint8Array) body = init.body;
|
|
131
|
+
else if (init.body instanceof ArrayBuffer) body = new Uint8Array(init.body);
|
|
132
|
+
else body = new Uint8Array(await new Response(init.body).arrayBuffer());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const headersIn = new Headers(init.headers || (input && input.headers) || {});
|
|
136
|
+
const headers = [];
|
|
137
|
+
headersIn.forEach((v, k) => { if (!SKIP_REQUEST_HEADERS.has(k.toLowerCase())) headers.push([k, v]); });
|
|
138
|
+
|
|
139
|
+
const inner = { m: method, p: u.pathname + u.search, b: body, h: headers };
|
|
140
|
+
let respBytes;
|
|
141
|
+
try {
|
|
142
|
+
respBytes = await client.call(baseUrl, "proxy", "http", cborEncode(inner));
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (e && e.message === "SESSION_EXPIRED") {
|
|
145
|
+
client = await SecrustClient.connect(baseUrl);
|
|
146
|
+
respBytes = await client.call(baseUrl, "proxy", "http", cborEncode(inner));
|
|
147
|
+
} else {
|
|
148
|
+
throw e;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const r = cborDecode(respBytes);
|
|
152
|
+
const responseHeaders = new Headers();
|
|
153
|
+
for (const [k, v] of r.h || []) responseHeaders.append(k, v);
|
|
154
|
+
return new Response(new Uint8Array(r.b || []), { status: r.s, headers: responseHeaders });
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
return { client };
|
|
158
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@secrustapp/shield",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Wrap your existing web app's API behind secrustapp encrypted envelopes — one line, zero backend changes (pairs with the secrustapp/shield container)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": { ".": "./index.js" },
|
|
8
|
+
"files": ["index.js", "README.md", "LICENSE"],
|
|
9
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
10
|
+
"repository": { "type": "git", "url": "https://github.com/of-a-tech/secrustapp" },
|
|
11
|
+
"homepage": "https://secrustapp.codeshield.my/library",
|
|
12
|
+
"keywords": ["encryption", "security", "post-quantum", "api-protection", "anti-reverse-engineering", "wasm", "reverse-proxy"],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@secrustapp/client": "^0.1.1"
|
|
15
|
+
}
|
|
16
|
+
}
|