tinfoil 1.1.8 → 1.1.10
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 +201 -0
- package/dist/ai-sdk-provider.d.ts +8 -0
- package/dist/ai-sdk-provider.d.ts.map +1 -1
- package/dist/ai-sdk-provider.js +1 -0
- package/dist/ai-sdk-provider.js.map +1 -1
- package/dist/encrypted-body-fetch.d.ts +1 -1
- package/dist/encrypted-body-fetch.d.ts.map +1 -1
- package/dist/encrypted-body-fetch.js +6 -2
- package/dist/encrypted-body-fetch.js.map +1 -1
- package/dist/secure-client.d.ts +16 -0
- package/dist/secure-client.d.ts.map +1 -1
- package/dist/secure-client.js +9 -2
- package/dist/secure-client.js.map +1 -1
- package/dist/secure-fetch.d.ts +5 -1
- package/dist/secure-fetch.d.ts.map +1 -1
- package/dist/secure-fetch.js +10 -3
- package/dist/secure-fetch.js.map +1 -1
- package/dist/tinfoil-ai.d.ts +11 -0
- package/dist/tinfoil-ai.d.ts.map +1 -1
- package/dist/tinfoil-ai.js +1 -0
- package/dist/tinfoil-ai.js.map +1 -1
- package/dist/user-cache-secret-store.browser.d.ts +7 -0
- package/dist/user-cache-secret-store.browser.d.ts.map +1 -0
- package/dist/user-cache-secret-store.browser.js +9 -0
- package/dist/user-cache-secret-store.browser.js.map +1 -0
- package/dist/user-cache-secret-store.d.ts +19 -0
- package/dist/user-cache-secret-store.d.ts.map +1 -0
- package/dist/user-cache-secret-store.js +160 -0
- package/dist/user-cache-secret-store.js.map +1 -0
- package/dist/user-cache-secret.d.ts +62 -0
- package/dist/user-cache-secret.d.ts.map +1 -0
- package/dist/user-cache-secret.js +399 -0
- package/dist/user-cache-secret.js.map +1 -0
- package/package.json +22 -22
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { closeSync, constants, fstatSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
/**
|
|
6
|
+
* Node.js persistence for the user cache secret. The other Tinfoil SDKs use
|
|
7
|
+
* the same file, so one machine gets one cache namespace across tools. The
|
|
8
|
+
* browser build swaps this module for a stub (see package.json's `browser`
|
|
9
|
+
* field) — browsers have no filesystem, so resolution attempts a
|
|
10
|
+
* process-lifetime in-memory secret.
|
|
11
|
+
*/
|
|
12
|
+
/** Directory under the home directory holding the secret file (mode 0700). */
|
|
13
|
+
export const USER_CACHE_SECRET_DIR_NAME = ".tinfoil";
|
|
14
|
+
/** Secret file name under the directory (mode 0600). */
|
|
15
|
+
export const USER_CACHE_SECRET_FILE_NAME = "user_cache_secret";
|
|
16
|
+
const HAS_POSIX_PERMISSIONS = process.platform !== "win32";
|
|
17
|
+
function posixReadFlags() {
|
|
18
|
+
return constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
|
19
|
+
}
|
|
20
|
+
function trimPersistenceWhitespace(value) {
|
|
21
|
+
return value.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "");
|
|
22
|
+
}
|
|
23
|
+
function isExpectedWindowsPath(path, expectedType) {
|
|
24
|
+
try {
|
|
25
|
+
const stats = lstatSync(path);
|
|
26
|
+
if (stats.isSymbolicLink()) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return expectedType === "directory" ? stats.isDirectory() : stats.isFile();
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
return err?.code === "ENOENT" ? undefined : false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** Reads the secret file, distinguishing a missing file from unusable state. */
|
|
36
|
+
function readSecretFile(path) {
|
|
37
|
+
let fd;
|
|
38
|
+
try {
|
|
39
|
+
if (!HAS_POSIX_PERMISSIONS) {
|
|
40
|
+
const pathState = isExpectedWindowsPath(path, "file");
|
|
41
|
+
if (pathState !== true) {
|
|
42
|
+
return pathState === undefined ? undefined : null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
fd = openSync(path, HAS_POSIX_PERMISSIONS ? posixReadFlags() : "r");
|
|
46
|
+
if (HAS_POSIX_PERMISSIONS) {
|
|
47
|
+
if (!fstatSync(fd).isFile()) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
let secret;
|
|
52
|
+
try {
|
|
53
|
+
secret = trimPersistenceWhitespace(new TextDecoder("utf-8", { fatal: true }).decode(readFileSync(fd)));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return secret === "" ? null : secret;
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
return err?.code === "ENOENT" ? undefined : null;
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
if (fd !== undefined) {
|
|
65
|
+
try {
|
|
66
|
+
closeSync(fd);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// Best-effort cleanup after the read has completed.
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns the secret persisted under the user's home directory, generating
|
|
76
|
+
* (via `generateSecret`) and persisting one on first use. Returns null when
|
|
77
|
+
* the home directory is unavailable or unwritable — the caller attempts a
|
|
78
|
+
* process-lifetime in-memory secret. Never throws.
|
|
79
|
+
*/
|
|
80
|
+
export function loadOrPersistUserCacheSecret(generateSecret) {
|
|
81
|
+
let home;
|
|
82
|
+
try {
|
|
83
|
+
home = homedir();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (!home) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const dir = join(home, USER_CACHE_SECRET_DIR_NAME);
|
|
92
|
+
const path = join(dir, USER_CACHE_SECRET_FILE_NAME);
|
|
93
|
+
try {
|
|
94
|
+
if (!HAS_POSIX_PERMISSIONS && isExpectedWindowsPath(dir, "directory") === false) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
98
|
+
if (HAS_POSIX_PERMISSIONS) {
|
|
99
|
+
let dirFd;
|
|
100
|
+
try {
|
|
101
|
+
dirFd = openSync(dir, posixReadFlags() | constants.O_DIRECTORY);
|
|
102
|
+
if (!fstatSync(dirFd).isDirectory()) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
if (dirFd !== undefined) {
|
|
108
|
+
closeSync(dirFd);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (isExpectedWindowsPath(dir, "directory") !== true) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
const existing = readSecretFile(path);
|
|
120
|
+
if (existing === null) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (existing !== undefined) {
|
|
124
|
+
return existing;
|
|
125
|
+
}
|
|
126
|
+
const secret = generateSecret();
|
|
127
|
+
if (secret === "") {
|
|
128
|
+
// The generator already reported the CSPRNG failure.
|
|
129
|
+
return "";
|
|
130
|
+
}
|
|
131
|
+
const candidatePath = join(dir, `${USER_CACHE_SECRET_FILE_NAME}.${process.pid}.${randomUUID()}.tmp`);
|
|
132
|
+
try {
|
|
133
|
+
// A complete temporary file is linked directly to the destination.
|
|
134
|
+
// Hard-link creation is atomic, so concurrent first users either win or
|
|
135
|
+
// adopt the complete destination created by another process.
|
|
136
|
+
writeFileSync(candidatePath, secret, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
137
|
+
try {
|
|
138
|
+
linkSync(candidatePath, path);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
if (err?.code !== "EEXIST") {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const persisted = readSecretFile(path);
|
|
146
|
+
return persisted ?? null;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
try {
|
|
153
|
+
unlinkSync(candidatePath);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
// Best-effort cleanup: the candidate is never the persisted file.
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=user-cache-secret-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-cache-secret-store.js","sourceRoot":"","sources":["../src/user-cache-secret-store.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,QAAQ,EACR,SAAS,EACT,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,aAAa,GACd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC;;;;;;GAMG;AAEH,8EAA8E;AAC9E,MAAM,CAAC,MAAM,0BAA0B,GAAG,UAAU,CAAC;AAErD,wDAAwD;AACxD,MAAM,CAAC,MAAM,2BAA2B,GAAG,mBAAmB,CAAC;AAE/D,MAAM,qBAAqB,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AAE3D,SAAS,cAAc;IACrB,OAAO,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC,UAAU,GAAG,SAAS,CAAC,UAAU,CAAC;AAC1E,CAAC;AAED,SAAS,yBAAyB,CAAC,KAAa;IAC9C,OAAO,KAAK,CAAC,OAAO,CAAC,uCAAuC,EAAE,EAAE,CAAC,CA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CAAC,CAAC;QACpF,IAAI,CAAC;YACH,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;QAChC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAK,GAAgC,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACzD,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QACD,MAAM,SAAS,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QACvC,OAAO,SAAS,IAAI,IAAI,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,UAAU,CAAC,aAAa,CAAC,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,kEAAkE;QACpE,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `user_cache_secret` provisions the per-user prompt-cache secret defined by
|
|
3
|
+
* the secure prompt caching contract. The router derives the request's
|
|
4
|
+
* prefix-cache namespace from it: requests carrying the same secret (under
|
|
5
|
+
* the same API identity) share cached prompt prefixes, requests carrying
|
|
6
|
+
* different secrets cannot observe each other's cache timing.
|
|
7
|
+
*
|
|
8
|
+
* Resolution order, mirroring the other Tinfoil clients:
|
|
9
|
+
*
|
|
10
|
+
* 1. a non-empty per-request `user_cache_secret` field in the body,
|
|
11
|
+
* 2. the `userCacheSecret` client option,
|
|
12
|
+
* 3. the TINFOIL_USER_CACHE_SECRET environment variable,
|
|
13
|
+
* 4. an attempted generated secret persisted at
|
|
14
|
+
* ~/.tinfoil/user_cache_secret (0600), shared with other Tinfoil SDKs
|
|
15
|
+
* using the same home directory. Runtimes without a filesystem (browsers,
|
|
16
|
+
* edge runtimes) use a process-lifetime in-memory secret when secure
|
|
17
|
+
* random generation succeeds.
|
|
18
|
+
*
|
|
19
|
+
* Injection happens inside the secure transport — before the EHBP transport
|
|
20
|
+
* encrypts the body, or on the way into the TLS connection pinned to the
|
|
21
|
+
* attested key — so the secret is only ever visible to the verified enclave.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* The router-only request-body field. A non-empty string scopes the prompt
|
|
25
|
+
* cache to that secret.
|
|
26
|
+
*/
|
|
27
|
+
export declare const USER_CACHE_SECRET_FIELD = "user_cache_secret";
|
|
28
|
+
/**
|
|
29
|
+
* Provisions the secret via the environment. An empty value is treated as
|
|
30
|
+
* unset.
|
|
31
|
+
*/
|
|
32
|
+
export declare const USER_CACHE_SECRET_ENV = "TINFOIL_USER_CACHE_SECRET";
|
|
33
|
+
/**
|
|
34
|
+
* Resolves the client-level secret: a non-empty explicit option wins, then a
|
|
35
|
+
* non-empty environment value, then an attempted persisted (or generated)
|
|
36
|
+
* secret. Never throws.
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveUserCacheSecret(explicit?: string): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Adds the field to a JSON-object body. Returns null — forward the original
|
|
41
|
+
* body untouched — for non-object bodies, trailing data, or a body that
|
|
42
|
+
* already carries a non-empty or non-string field. An empty string is
|
|
43
|
+
* replaced with the resolved client secret.
|
|
44
|
+
*/
|
|
45
|
+
export declare function injectUserCacheSecret(raw: string, secret: string): string | null;
|
|
46
|
+
/**
|
|
47
|
+
* Injects the client-level secret into an eligible request: a POST with a
|
|
48
|
+
* JSON-object body to one of the supported endpoints. Anything else — other
|
|
49
|
+
* endpoints, non-object bodies, or a body that already carries the field —
|
|
50
|
+
* passes through untouched. Never throws. The caller
|
|
51
|
+
* runs this inside the sealing boundary, so retries and redirects below the
|
|
52
|
+
* transport replay the injected body, not the caller's original.
|
|
53
|
+
*/
|
|
54
|
+
export declare function injectUserCacheSecretIntoRequestInit(init: RequestInit | undefined, pathname: string, secret: string): Promise<RequestInit | undefined>;
|
|
55
|
+
/**
|
|
56
|
+
* Wraps a fetch function so eligible request bodies carry the secret. The
|
|
57
|
+
* wrapper sits directly above the sealing fetch (the TLS connection pinned
|
|
58
|
+
* to the attested key), so the injected field never travels outside the
|
|
59
|
+
* protected channel.
|
|
60
|
+
*/
|
|
61
|
+
export declare function withUserCacheSecret(inner: typeof fetch, baseURL: string, secret: string): typeof fetch;
|
|
62
|
+
//# sourceMappingURL=user-cache-secret.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-cache-secret.d.ts","sourceRoot":"","sources":["../src/user-cache-secret.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH;;;GAGG;AACH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;GAGG;AACH,eAAO,MAAM,qBAAqB,8BAA8B,CAAC;AAejE;;;;GAIG;AACH,wBAAsB,sBAAsB,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAS/E;AA+DD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiChF;AA0JD;;;;;;;GAOG;AACH,wBAAsB,oCAAoC,CACxD,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAuBlC;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,KAAK,EACnB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb,OAAO,KAAK,CA6Cd"}
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { isRealBrowser } from "./env.js";
|
|
2
|
+
/**
|
|
3
|
+
* `user_cache_secret` provisions the per-user prompt-cache secret defined by
|
|
4
|
+
* the secure prompt caching contract. The router derives the request's
|
|
5
|
+
* prefix-cache namespace from it: requests carrying the same secret (under
|
|
6
|
+
* the same API identity) share cached prompt prefixes, requests carrying
|
|
7
|
+
* different secrets cannot observe each other's cache timing.
|
|
8
|
+
*
|
|
9
|
+
* Resolution order, mirroring the other Tinfoil clients:
|
|
10
|
+
*
|
|
11
|
+
* 1. a non-empty per-request `user_cache_secret` field in the body,
|
|
12
|
+
* 2. the `userCacheSecret` client option,
|
|
13
|
+
* 3. the TINFOIL_USER_CACHE_SECRET environment variable,
|
|
14
|
+
* 4. an attempted generated secret persisted at
|
|
15
|
+
* ~/.tinfoil/user_cache_secret (0600), shared with other Tinfoil SDKs
|
|
16
|
+
* using the same home directory. Runtimes without a filesystem (browsers,
|
|
17
|
+
* edge runtimes) use a process-lifetime in-memory secret when secure
|
|
18
|
+
* random generation succeeds.
|
|
19
|
+
*
|
|
20
|
+
* Injection happens inside the secure transport — before the EHBP transport
|
|
21
|
+
* encrypts the body, or on the way into the TLS connection pinned to the
|
|
22
|
+
* attested key — so the secret is only ever visible to the verified enclave.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* The router-only request-body field. A non-empty string scopes the prompt
|
|
26
|
+
* cache to that secret.
|
|
27
|
+
*/
|
|
28
|
+
export const USER_CACHE_SECRET_FIELD = "user_cache_secret";
|
|
29
|
+
/**
|
|
30
|
+
* Provisions the secret via the environment. An empty value is treated as
|
|
31
|
+
* unset.
|
|
32
|
+
*/
|
|
33
|
+
export const USER_CACHE_SECRET_ENV = "TINFOIL_USER_CACHE_SECRET";
|
|
34
|
+
/**
|
|
35
|
+
* The OpenAI-compatible endpoints whose bodies carry the field. Matched by
|
|
36
|
+
* path suffix with no /v1 prefix required, so custom base URLs
|
|
37
|
+
* (path-prefixed proxies or /v1-less roots) still qualify. Other endpoints
|
|
38
|
+
* (embeddings, audio, files) are excluded: their engines do not prefix-cache
|
|
39
|
+
* and may reject unknown fields.
|
|
40
|
+
*/
|
|
41
|
+
const USER_CACHE_SECRET_PATHS = [
|
|
42
|
+
"/chat/completions",
|
|
43
|
+
"/completions",
|
|
44
|
+
"/responses",
|
|
45
|
+
];
|
|
46
|
+
/**
|
|
47
|
+
* Resolves the client-level secret: a non-empty explicit option wins, then a
|
|
48
|
+
* non-empty environment value, then an attempted persisted (or generated)
|
|
49
|
+
* secret. Never throws.
|
|
50
|
+
*/
|
|
51
|
+
export async function resolveUserCacheSecret(explicit) {
|
|
52
|
+
if (explicit !== undefined && explicit !== "") {
|
|
53
|
+
return explicit;
|
|
54
|
+
}
|
|
55
|
+
const env = typeof process !== "undefined" ? process.env?.[USER_CACHE_SECRET_ENV] : undefined;
|
|
56
|
+
if (env !== undefined && env !== "") {
|
|
57
|
+
return env;
|
|
58
|
+
}
|
|
59
|
+
return loadOrGenerateUserCacheSecret();
|
|
60
|
+
}
|
|
61
|
+
/** Returns a fresh 256-bit random secret, hex-encoded. */
|
|
62
|
+
function newUserCacheSecret() {
|
|
63
|
+
try {
|
|
64
|
+
const bytes = new Uint8Array(32);
|
|
65
|
+
crypto.getRandomValues(bytes);
|
|
66
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
console.warn("[tinfoil] could not generate a user cache secret; automatic prompt-cache scoping is unavailable");
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The process-lifetime fallback for when the secret cannot be persisted. When
|
|
75
|
+
* secure random generation succeeds, an unpersisted secret still isolates
|
|
76
|
+
* this process's cache namespace, but continuity is lost on restart — like a
|
|
77
|
+
* session ID, it silently resets the namespace every deploy — so the fallback
|
|
78
|
+
* warns once per process.
|
|
79
|
+
*/
|
|
80
|
+
let ephemeralUserCacheSecret;
|
|
81
|
+
function getEphemeralUserCacheSecret() {
|
|
82
|
+
if (ephemeralUserCacheSecret === undefined) {
|
|
83
|
+
ephemeralUserCacheSecret = newUserCacheSecret();
|
|
84
|
+
if (ephemeralUserCacheSecret !== "") {
|
|
85
|
+
console.warn("[tinfoil] could not persist the user cache secret; using an in-memory secret, " +
|
|
86
|
+
"so prompt-cache continuity resets when this process exits " +
|
|
87
|
+
`(set ${USER_CACHE_SECRET_ENV} or the userCacheSecret option to pin one)`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return ephemeralUserCacheSecret;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns the secret persisted under the user's home directory, generating
|
|
94
|
+
* and persisting one on first use. Without a usable filesystem (browsers,
|
|
95
|
+
* edge runtimes, an unwritable home directory) it uses a process-lifetime
|
|
96
|
+
* in-memory secret when secure random generation succeeds.
|
|
97
|
+
*/
|
|
98
|
+
async function loadOrGenerateUserCacheSecret() {
|
|
99
|
+
if (!isRealBrowser()) {
|
|
100
|
+
try {
|
|
101
|
+
// Dynamic import to keep Node.js fs/os modules out of browser bundles;
|
|
102
|
+
// package.json's browser field maps the store to a stub, the same way
|
|
103
|
+
// pinned-tls-fetch is gated.
|
|
104
|
+
const { loadOrPersistUserCacheSecret } = await import("./user-cache-secret-store.js");
|
|
105
|
+
const secret = loadOrPersistUserCacheSecret(newUserCacheSecret);
|
|
106
|
+
if (secret !== null) {
|
|
107
|
+
return secret;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// No usable filesystem in this runtime: fall through.
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return getEphemeralUserCacheSecret();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Adds the field to a JSON-object body. Returns null — forward the original
|
|
118
|
+
* body untouched — for non-object bodies, trailing data, or a body that
|
|
119
|
+
* already carries a non-empty or non-string field. An empty string is
|
|
120
|
+
* replaced with the resolved client secret.
|
|
121
|
+
*/
|
|
122
|
+
export function injectUserCacheSecret(raw, secret) {
|
|
123
|
+
let parsed;
|
|
124
|
+
try {
|
|
125
|
+
// JSON.parse enforces exactly the framing we need: a single
|
|
126
|
+
// value followed by nothing but whitespace. Trailing data ('{...}}',
|
|
127
|
+
// '{...} garbage') throws, so a body the server would reject is forwarded
|
|
128
|
+
// as-is instead of being quietly rewritten into one it accepts.
|
|
129
|
+
parsed = JSON.parse(raw);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
if (Object.prototype.hasOwnProperty.call(parsed, USER_CACHE_SECRET_FIELD)) {
|
|
138
|
+
const existing = parsed[USER_CACHE_SECRET_FIELD];
|
|
139
|
+
if (existing !== "") {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const range = topLevelValueRange(raw, USER_CACHE_SECRET_FIELD);
|
|
143
|
+
if (range === null) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return raw.slice(0, range[0]) + JSON.stringify(secret) + raw.slice(range[1]);
|
|
147
|
+
}
|
|
148
|
+
// Splice the field into the original text instead of re-serializing the
|
|
149
|
+
// parsed object: parsing round-trips numbers through float64, which would
|
|
150
|
+
// corrupt int64-range values such as `"seed": 9007199254740993`. The last
|
|
151
|
+
// '}' is the object's closing brace — only whitespace may follow it.
|
|
152
|
+
const end = raw.lastIndexOf("}");
|
|
153
|
+
const field = `${JSON.stringify(USER_CACHE_SECRET_FIELD)}:${JSON.stringify(secret)}`;
|
|
154
|
+
const separator = Object.keys(parsed).length > 0 ? "," : "";
|
|
155
|
+
return raw.slice(0, end) + separator + field + raw.slice(end);
|
|
156
|
+
}
|
|
157
|
+
function topLevelValueRange(raw, field) {
|
|
158
|
+
let index = skipWhitespace(raw, 0);
|
|
159
|
+
let matchingRange = null;
|
|
160
|
+
if (raw[index] !== "{") {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
index += 1;
|
|
164
|
+
while (index < raw.length) {
|
|
165
|
+
index = skipWhitespace(raw, index);
|
|
166
|
+
if (raw[index] === "}") {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
const keyEnd = stringEnd(raw, index);
|
|
170
|
+
if (keyEnd === null) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
let key;
|
|
174
|
+
try {
|
|
175
|
+
key = JSON.parse(raw.slice(index, keyEnd));
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
index = skipWhitespace(raw, keyEnd);
|
|
181
|
+
if (raw[index] !== ":") {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
const valueStart = skipWhitespace(raw, index + 1);
|
|
185
|
+
const valueEnd = jsonValueEnd(raw, valueStart);
|
|
186
|
+
if (valueEnd === null) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
if (key === field) {
|
|
190
|
+
matchingRange = [valueStart, valueEnd];
|
|
191
|
+
}
|
|
192
|
+
index = skipWhitespace(raw, valueEnd);
|
|
193
|
+
if (raw[index] === ",") {
|
|
194
|
+
index += 1;
|
|
195
|
+
}
|
|
196
|
+
else if (raw[index] === "}") {
|
|
197
|
+
return matchingRange;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
function skipWhitespace(raw, start) {
|
|
206
|
+
let index = start;
|
|
207
|
+
while (index < raw.length && /\s/.test(raw[index])) {
|
|
208
|
+
index += 1;
|
|
209
|
+
}
|
|
210
|
+
return index;
|
|
211
|
+
}
|
|
212
|
+
function stringEnd(raw, start) {
|
|
213
|
+
if (raw[start] !== "\"") {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
let escaped = false;
|
|
217
|
+
for (let index = start + 1; index < raw.length; index += 1) {
|
|
218
|
+
if (escaped) {
|
|
219
|
+
escaped = false;
|
|
220
|
+
}
|
|
221
|
+
else if (raw[index] === "\\") {
|
|
222
|
+
escaped = true;
|
|
223
|
+
}
|
|
224
|
+
else if (raw[index] === "\"") {
|
|
225
|
+
return index + 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
function jsonValueEnd(raw, start) {
|
|
231
|
+
if (raw[start] === "\"") {
|
|
232
|
+
return stringEnd(raw, start);
|
|
233
|
+
}
|
|
234
|
+
if (raw[start] === "{" || raw[start] === "[") {
|
|
235
|
+
let depth = 0;
|
|
236
|
+
let inString = false;
|
|
237
|
+
let escaped = false;
|
|
238
|
+
for (let index = start; index < raw.length; index += 1) {
|
|
239
|
+
const character = raw[index];
|
|
240
|
+
if (inString) {
|
|
241
|
+
if (escaped) {
|
|
242
|
+
escaped = false;
|
|
243
|
+
}
|
|
244
|
+
else if (character === "\\") {
|
|
245
|
+
escaped = true;
|
|
246
|
+
}
|
|
247
|
+
else if (character === "\"") {
|
|
248
|
+
inString = false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else if (character === "\"") {
|
|
252
|
+
inString = true;
|
|
253
|
+
}
|
|
254
|
+
else if (character === "{" || character === "[") {
|
|
255
|
+
depth += 1;
|
|
256
|
+
}
|
|
257
|
+
else if (character === "}" || character === "]") {
|
|
258
|
+
depth -= 1;
|
|
259
|
+
if (depth === 0) {
|
|
260
|
+
return index + 1;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
let end = start;
|
|
267
|
+
while (end < raw.length && raw[end] !== "," && raw[end] !== "}") {
|
|
268
|
+
end += 1;
|
|
269
|
+
}
|
|
270
|
+
while (end > start && /\s/.test(raw[end - 1])) {
|
|
271
|
+
end -= 1;
|
|
272
|
+
}
|
|
273
|
+
return end > start ? end : null;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Decodes supported replayable in-memory bodies. Other representations are
|
|
277
|
+
* forwarded unchanged, so callers using them must provide the field.
|
|
278
|
+
*/
|
|
279
|
+
function bodyText(body) {
|
|
280
|
+
if (typeof body === "string") {
|
|
281
|
+
return body;
|
|
282
|
+
}
|
|
283
|
+
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
|
|
284
|
+
try {
|
|
285
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(body);
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Rewrites a caller-supplied Content-Length header to describe the injected
|
|
295
|
+
* body. Fetch implementations compute the length themselves when the header
|
|
296
|
+
* is absent, so it is only touched when the caller set one.
|
|
297
|
+
*/
|
|
298
|
+
function headersWithUpdatedBodyMetadata(headers, body) {
|
|
299
|
+
if (headers === undefined) {
|
|
300
|
+
return headers;
|
|
301
|
+
}
|
|
302
|
+
const updated = new Headers(headers);
|
|
303
|
+
if (updated.has("content-length")) {
|
|
304
|
+
updated.set("content-length", String(new TextEncoder().encode(body).byteLength));
|
|
305
|
+
return updated;
|
|
306
|
+
}
|
|
307
|
+
return headers;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Injects the client-level secret into an eligible request: a POST with a
|
|
311
|
+
* JSON-object body to one of the supported endpoints. Anything else — other
|
|
312
|
+
* endpoints, non-object bodies, or a body that already carries the field —
|
|
313
|
+
* passes through untouched. Never throws. The caller
|
|
314
|
+
* runs this inside the sealing boundary, so retries and redirects below the
|
|
315
|
+
* transport replay the injected body, not the caller's original.
|
|
316
|
+
*/
|
|
317
|
+
export async function injectUserCacheSecretIntoRequestInit(init, pathname, secret) {
|
|
318
|
+
if (secret === "" || init === undefined) {
|
|
319
|
+
return init;
|
|
320
|
+
}
|
|
321
|
+
if ((init.method ?? "GET").toUpperCase() !== "POST") {
|
|
322
|
+
return init;
|
|
323
|
+
}
|
|
324
|
+
if (!USER_CACHE_SECRET_PATHS.some((path) => pathname.endsWith(path))) {
|
|
325
|
+
return init;
|
|
326
|
+
}
|
|
327
|
+
const raw = bodyText(init.body);
|
|
328
|
+
if (raw === null || raw === "") {
|
|
329
|
+
return init;
|
|
330
|
+
}
|
|
331
|
+
const injected = injectUserCacheSecret(raw, secret);
|
|
332
|
+
if (injected === null) {
|
|
333
|
+
return init;
|
|
334
|
+
}
|
|
335
|
+
return {
|
|
336
|
+
...init,
|
|
337
|
+
body: injected,
|
|
338
|
+
headers: headersWithUpdatedBodyMetadata(init.headers, injected),
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Wraps a fetch function so eligible request bodies carry the secret. The
|
|
343
|
+
* wrapper sits directly above the sealing fetch (the TLS connection pinned
|
|
344
|
+
* to the attested key), so the injected field never travels outside the
|
|
345
|
+
* protected channel.
|
|
346
|
+
*/
|
|
347
|
+
export function withUserCacheSecret(inner, baseURL, secret) {
|
|
348
|
+
if (secret === "") {
|
|
349
|
+
return inner;
|
|
350
|
+
}
|
|
351
|
+
return (async (input, init) => {
|
|
352
|
+
let pathname;
|
|
353
|
+
try {
|
|
354
|
+
pathname = requestPathname(input, baseURL);
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
// An unresolvable URL is the inner fetch's error to raise.
|
|
358
|
+
return inner(input, init);
|
|
359
|
+
}
|
|
360
|
+
const normalized = input instanceof Request && init?.body !== undefined
|
|
361
|
+
? {
|
|
362
|
+
...init,
|
|
363
|
+
method: init.method ?? input.method,
|
|
364
|
+
headers: init.headers ?? input.headers,
|
|
365
|
+
}
|
|
366
|
+
: init;
|
|
367
|
+
let requestInit = normalized;
|
|
368
|
+
if (input instanceof Request &&
|
|
369
|
+
init?.body === undefined &&
|
|
370
|
+
input.body !== null &&
|
|
371
|
+
(init?.method ?? input.method).toUpperCase() === "POST" &&
|
|
372
|
+
USER_CACHE_SECRET_PATHS.some((path) => pathname.endsWith(path))) {
|
|
373
|
+
try {
|
|
374
|
+
requestInit = {
|
|
375
|
+
...init,
|
|
376
|
+
method: init?.method ?? input.method,
|
|
377
|
+
headers: init?.headers ?? input.headers,
|
|
378
|
+
body: await input.clone().arrayBuffer(),
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
return inner(input, init);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const injected = await injectUserCacheSecretIntoRequestInit(requestInit, pathname, secret);
|
|
386
|
+
return inner(input, injected);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
/** Resolves the request path the same way the pinned TLS fetch does. */
|
|
390
|
+
function requestPathname(input, baseURL) {
|
|
391
|
+
if (typeof input === "string") {
|
|
392
|
+
return new URL(input, baseURL).pathname;
|
|
393
|
+
}
|
|
394
|
+
if (input instanceof URL) {
|
|
395
|
+
return input.pathname;
|
|
396
|
+
}
|
|
397
|
+
return new URL(input.url, baseURL).pathname;
|
|
398
|
+
}
|
|
399
|
+
//# sourceMappingURL=user-cache-secret.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-cache-secret.js","sourceRoot":"","sources":["../src/user-cache-secret.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC;AAE3D;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,2BAA2B,CAAC;AAEjE;;;;;;GAMG;AACH,MAAM,uBAAuB,GAAG;IAC9B,mBAAmB;IACnB,cAAc;IACd,YAAY;CACb,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,QAAiB;IAC5D,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,EAAE,EAAE,CAAC;QAC9C,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,OAAO,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,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