tinfoil 0.8.2 → 0.8.3
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/README.md +3 -0
- package/dist/ai-sdk-provider.browser.d.ts +1 -0
- package/dist/ai-sdk-provider.browser.js +2 -8
- package/dist/ai-sdk-provider.d.ts +2 -0
- package/dist/ai-sdk-provider.js +2 -8
- package/dist/client.browser.d.ts +2 -0
- package/dist/client.browser.js +2 -8
- package/dist/client.d.ts +3 -0
- package/dist/client.js +2 -8
- package/dist/config.d.ts +6 -0
- package/dist/config.js +6 -0
- package/dist/encrypted-body-fetch.d.ts +2 -2
- package/dist/encrypted-body-fetch.js +54 -16
- package/dist/esm/ai-sdk-provider.browser.d.ts +1 -0
- package/dist/esm/ai-sdk-provider.browser.js +2 -8
- package/dist/esm/ai-sdk-provider.d.ts +2 -0
- package/dist/esm/ai-sdk-provider.js +2 -8
- package/dist/esm/client.browser.d.ts +2 -0
- package/dist/esm/client.browser.js +2 -8
- package/dist/esm/client.d.ts +3 -0
- package/dist/esm/client.js +2 -8
- package/dist/esm/config.d.ts +6 -0
- package/dist/esm/config.js +6 -0
- package/dist/esm/encrypted-body-fetch.d.ts +2 -2
- package/dist/esm/encrypted-body-fetch.js +54 -16
- package/dist/esm/verification-runner.d.ts +0 -9
- package/dist/esm/verification-runner.js +1 -43
- package/dist/esm/verifier.d.ts +0 -2
- package/dist/esm/verifier.js +21 -52
- package/dist/verification-runner.d.ts +0 -9
- package/dist/verification-runner.js +1 -44
- package/dist/verifier.d.ts +0 -2
- package/dist/verifier.js +21 -52
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,6 +65,9 @@ const completion = await client.chat.completions.create({
|
|
|
65
65
|
- WebAssembly support for enclave verification
|
|
66
66
|
- Secure context (HTTPS or localhost) with WebCrypto SubtleCrypto (required by EHBP)
|
|
67
67
|
|
|
68
|
+
> **Note**
|
|
69
|
+
> The EHBP transport now accepts `http://` origins (for example when developing against a `localhost` proxy). Payloads remain HPKE-encrypted end to end, while TLS-only fallback logic continues to require HTTPS because it relies on certificate pinning.
|
|
70
|
+
|
|
68
71
|
|
|
69
72
|
## Verification helpers
|
|
70
73
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
interface CreateTinfoilAIOptions {
|
|
2
2
|
baseURL?: string;
|
|
3
|
+
hpkeKeyURL?: string;
|
|
3
4
|
configRepo?: string;
|
|
4
5
|
}
|
|
5
6
|
export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
|
|
@@ -8,8 +8,8 @@ const verifier_1 = require("./verifier");
|
|
|
8
8
|
const env_1 = require("./env");
|
|
9
9
|
async function createTinfoilAI(apiKey, options = {}) {
|
|
10
10
|
const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
11
|
+
const hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
11
12
|
const configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
12
|
-
assertHttpsUrl(baseURL, "Inference baseURL");
|
|
13
13
|
const verifier = new verifier_1.Verifier({ serverURL: baseURL, configRepo });
|
|
14
14
|
const attestationResponse = await verifier.verify();
|
|
15
15
|
const hpkePublicKey = attestationResponse.hpkePublicKey;
|
|
@@ -20,7 +20,7 @@ async function createTinfoilAI(apiKey, options = {}) {
|
|
|
20
20
|
}
|
|
21
21
|
throw new Error("HPKE public key is required in browser environments");
|
|
22
22
|
}
|
|
23
|
-
const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey);
|
|
23
|
+
const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey, hpkeKeyURL);
|
|
24
24
|
return (0, openai_compatible_1.createOpenAICompatible)({
|
|
25
25
|
name: "tinfoil",
|
|
26
26
|
baseURL: baseURL.replace(/\/$/, ""),
|
|
@@ -28,9 +28,3 @@ async function createTinfoilAI(apiKey, options = {}) {
|
|
|
28
28
|
fetch: fetchFunction,
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
-
function assertHttpsUrl(url, context) {
|
|
32
|
-
const parsed = new URL(url);
|
|
33
|
-
if (parsed.protocol !== "https:") {
|
|
34
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
interface CreateTinfoilAIOptions {
|
|
2
2
|
/** Override the inference API base URL */
|
|
3
3
|
baseURL?: string;
|
|
4
|
+
/** Override the URL used to fetch the HPKE (defaults to baseURL) */
|
|
5
|
+
hpkeKeyURL?: string;
|
|
4
6
|
/** Override the config GitHub repository */
|
|
5
7
|
configRepo?: string;
|
|
6
8
|
}
|
package/dist/ai-sdk-provider.js
CHANGED
|
@@ -22,8 +22,8 @@ const stream_1 = require("stream");
|
|
|
22
22
|
*/
|
|
23
23
|
async function createTinfoilAI(apiKey, options = {}) {
|
|
24
24
|
const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
25
|
+
const hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
25
26
|
const configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
26
|
-
assertHttpsUrl(baseURL, "Inference baseURL");
|
|
27
27
|
// step 1: verify the enclave and extract the public keys
|
|
28
28
|
// from the attestation response
|
|
29
29
|
const verifier = new verifier_1.Verifier({ serverURL: baseURL, configRepo });
|
|
@@ -34,7 +34,7 @@ async function createTinfoilAI(apiKey, options = {}) {
|
|
|
34
34
|
let fetchFunction;
|
|
35
35
|
if (hpkePublicKey) {
|
|
36
36
|
// HPKE available: use encrypted body fetch
|
|
37
|
-
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey);
|
|
37
|
+
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey, hpkeKeyURL);
|
|
38
38
|
}
|
|
39
39
|
else {
|
|
40
40
|
// HPKE not available: check if we're in a browser
|
|
@@ -57,12 +57,6 @@ async function createTinfoilAI(apiKey, options = {}) {
|
|
|
57
57
|
fetch: fetchFunction,
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
|
-
function assertHttpsUrl(url, context) {
|
|
61
|
-
const parsed = new URL(url);
|
|
62
|
-
if (parsed.protocol !== "https:") {
|
|
63
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
60
|
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
67
61
|
return (async (input, init) => {
|
|
68
62
|
// Normalize URL
|
package/dist/client.browser.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { VerificationDocument } from "./verifier";
|
|
|
4
4
|
interface TinfoilAIOptions {
|
|
5
5
|
apiKey?: string;
|
|
6
6
|
baseURL?: string;
|
|
7
|
+
hpkeKeyURL?: string;
|
|
7
8
|
configRepo?: string;
|
|
8
9
|
[key: string]: any;
|
|
9
10
|
}
|
|
@@ -15,6 +16,7 @@ export declare class TinfoilAI {
|
|
|
15
16
|
private verificationDocument?;
|
|
16
17
|
apiKey?: string;
|
|
17
18
|
baseURL?: string;
|
|
19
|
+
hpkeKeyURL?: string;
|
|
18
20
|
constructor(options?: TinfoilAIOptions);
|
|
19
21
|
ready(): Promise<void>;
|
|
20
22
|
private initClient;
|
package/dist/client.browser.js
CHANGED
|
@@ -41,7 +41,7 @@ class TinfoilAI {
|
|
|
41
41
|
}
|
|
42
42
|
this.apiKey = openAIOptions.apiKey;
|
|
43
43
|
this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
44
|
-
|
|
44
|
+
this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
45
45
|
this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
46
46
|
this.clientPromise = this.initClient(openAIOptions);
|
|
47
47
|
}
|
|
@@ -80,7 +80,7 @@ class TinfoilAI {
|
|
|
80
80
|
}
|
|
81
81
|
throw new Error("HPKE public key is required in browser environments");
|
|
82
82
|
}
|
|
83
|
-
const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey);
|
|
83
|
+
const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
84
84
|
const clientOptions = {
|
|
85
85
|
...options,
|
|
86
86
|
baseURL: this.baseURL,
|
|
@@ -149,9 +149,3 @@ exports.TinfoilAI = TinfoilAI;
|
|
|
149
149
|
TinfoilAI.Uploads = openai_1.default.Uploads;
|
|
150
150
|
TinfoilAI.VectorStores = openai_1.default.VectorStores;
|
|
151
151
|
})(TinfoilAI || (exports.TinfoilAI = TinfoilAI = {}));
|
|
152
|
-
function assertHttpsUrl(url, context) {
|
|
153
|
-
const parsed = new URL(url);
|
|
154
|
-
if (parsed.protocol !== "https:") {
|
|
155
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
156
|
-
}
|
|
157
|
-
}
|
package/dist/client.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ interface TinfoilAIOptions {
|
|
|
14
14
|
apiKey?: string;
|
|
15
15
|
/** Override the inference API base URL */
|
|
16
16
|
baseURL?: string;
|
|
17
|
+
/** Override the URL used to fetch the HPKE key (defaults to baseURL) */
|
|
18
|
+
hpkeKeyURL?: string;
|
|
17
19
|
/** Override the config GitHub repository */
|
|
18
20
|
configRepo?: string;
|
|
19
21
|
[key: string]: any;
|
|
@@ -26,6 +28,7 @@ export declare class TinfoilAI {
|
|
|
26
28
|
private verificationDocument?;
|
|
27
29
|
apiKey?: string;
|
|
28
30
|
baseURL?: string;
|
|
31
|
+
hpkeKeyURL?: string;
|
|
29
32
|
/**
|
|
30
33
|
* Creates a new TinfoilAI instance.
|
|
31
34
|
* @param options - Configuration options including apiKey and other OpenAI client options
|
package/dist/client.js
CHANGED
|
@@ -72,7 +72,7 @@ class TinfoilAI {
|
|
|
72
72
|
// Store properties for compatibility
|
|
73
73
|
this.apiKey = openAIOptions.apiKey;
|
|
74
74
|
this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
75
|
-
|
|
75
|
+
this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
76
76
|
this.configRepo =
|
|
77
77
|
options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
78
78
|
this.clientPromise = this.initClient(openAIOptions);
|
|
@@ -113,7 +113,7 @@ class TinfoilAI {
|
|
|
113
113
|
let fetchFunction;
|
|
114
114
|
if (hpkePublicKey) {
|
|
115
115
|
// HPKE available: use encrypted body fetch
|
|
116
|
-
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey);
|
|
116
|
+
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
117
117
|
}
|
|
118
118
|
else {
|
|
119
119
|
// HPKE not available: check if we're in a browser
|
|
@@ -250,12 +250,6 @@ exports.TinfoilAI = TinfoilAI;
|
|
|
250
250
|
TinfoilAI.Uploads = openai_1.default.Uploads;
|
|
251
251
|
TinfoilAI.VectorStores = openai_1.default.VectorStores;
|
|
252
252
|
})(TinfoilAI || (exports.TinfoilAI = TinfoilAI = {}));
|
|
253
|
-
function assertHttpsUrl(url, context) {
|
|
254
|
-
const parsed = new URL(url);
|
|
255
|
-
if (parsed.protocol !== "https:") {
|
|
256
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
253
|
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
260
254
|
return (async (input, init) => {
|
|
261
255
|
// Normalize URL
|
package/dist/config.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ export declare const TINFOIL_CONFIG: {
|
|
|
6
6
|
* The full base URL for the inference API
|
|
7
7
|
*/
|
|
8
8
|
readonly INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/";
|
|
9
|
+
/**
|
|
10
|
+
* The URL used to fetch the HPKE server public key (via EHBP .well-known endpoint).
|
|
11
|
+
* Defaults to the same value as `INFERENCE_BASE_URL` so hpkeKeyURL and baseURL share
|
|
12
|
+
* the same default. Callers may override independently if key discovery is hosted elsewhere.
|
|
13
|
+
*/
|
|
14
|
+
readonly HPKE_KEY_URL: "https://inference.tinfoil.sh/v1/";
|
|
9
15
|
/**
|
|
10
16
|
* The GitHub repository for the confidential inference proxy
|
|
11
17
|
*/
|
package/dist/config.js
CHANGED
|
@@ -9,6 +9,12 @@ exports.TINFOIL_CONFIG = {
|
|
|
9
9
|
* The full base URL for the inference API
|
|
10
10
|
*/
|
|
11
11
|
INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/",
|
|
12
|
+
/**
|
|
13
|
+
* The URL used to fetch the HPKE server public key (via EHBP .well-known endpoint).
|
|
14
|
+
* Defaults to the same value as `INFERENCE_BASE_URL` so hpkeKeyURL and baseURL share
|
|
15
|
+
* the same default. Callers may override independently if key discovery is hosted elsewhere.
|
|
16
|
+
*/
|
|
17
|
+
HPKE_KEY_URL: "https://inference.tinfoil.sh/v1/",
|
|
12
18
|
/**
|
|
13
19
|
* The GitHub repository for the confidential inference proxy
|
|
14
20
|
*/
|
|
@@ -3,8 +3,8 @@ export declare function normalizeEncryptedBodyRequestArgs(input: RequestInfo | U
|
|
|
3
3
|
url: string;
|
|
4
4
|
init?: RequestInit;
|
|
5
5
|
};
|
|
6
|
-
export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit): Promise<Response>;
|
|
7
|
-
export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string): typeof fetch;
|
|
6
|
+
export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit, hpkeKeyURL?: string): Promise<Response>;
|
|
7
|
+
export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string, hpkeKeyURL?: string): typeof fetch;
|
|
8
8
|
export declare function __setEhbpModuleForTests(module: EhbpModule | undefined): void;
|
|
9
9
|
export declare function __resetEhbpModuleStateForTests(): void;
|
|
10
10
|
export {};
|
|
@@ -40,6 +40,7 @@ exports.__setEhbpModuleForTests = __setEhbpModuleForTests;
|
|
|
40
40
|
exports.__resetEhbpModuleStateForTests = __resetEhbpModuleStateForTests;
|
|
41
41
|
const env_1 = require("./env");
|
|
42
42
|
const transportCache = new Map();
|
|
43
|
+
const compositeTransportCache = new Map();
|
|
43
44
|
let ehbpModulePromise = null;
|
|
44
45
|
let ehbpModuleOverride;
|
|
45
46
|
// Public API
|
|
@@ -63,30 +64,38 @@ function normalizeEncryptedBodyRequestArgs(input, init) {
|
|
|
63
64
|
init: { ...derivedInit, ...init },
|
|
64
65
|
};
|
|
65
66
|
}
|
|
66
|
-
async function encryptedBodyRequest(input, hpkePublicKey, init) {
|
|
67
|
+
async function encryptedBodyRequest(input, hpkePublicKey, init, hpkeKeyURL) {
|
|
67
68
|
const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
|
|
68
69
|
const u = new URL(requestUrl);
|
|
69
|
-
if (u.protocol !== 'https:') {
|
|
70
|
-
throw new Error(`HTTP connections are not allowed for EHBP. Use HTTPS. URL: ${requestUrl}`);
|
|
71
|
-
}
|
|
72
70
|
const { origin } = u;
|
|
73
|
-
|
|
71
|
+
// Determine which origin to fetch the HPKE key from (may be different from request origin)
|
|
72
|
+
const keyOrigin = hpkeKeyURL ? new URL(hpkeKeyURL).origin : origin;
|
|
73
|
+
// If key origin matches request origin, use the standard transport (fetches keys from origin)
|
|
74
|
+
// Otherwise, build a composite transport that routes to `origin` but uses the key fetched from `keyOrigin`.
|
|
75
|
+
const transport = keyOrigin === origin
|
|
76
|
+
? await getTransportForOrigin(origin)
|
|
77
|
+
: await getTransportForRequestOriginWithKeyFrom(origin, keyOrigin);
|
|
74
78
|
const serverPublicKey = await transport.getServerPublicKeyHex();
|
|
75
79
|
if (serverPublicKey !== hpkePublicKey) {
|
|
76
|
-
|
|
80
|
+
// Clear caches so the next attempt re-handshakes
|
|
81
|
+
if (keyOrigin === origin) {
|
|
82
|
+
transportCache.delete(origin);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const cacheKey = `${origin}::${keyOrigin}`;
|
|
86
|
+
compositeTransportCache.delete(cacheKey);
|
|
87
|
+
// Also clear the underlying key-origin cache to force re-fetch
|
|
88
|
+
transportCache.delete(keyOrigin);
|
|
89
|
+
}
|
|
77
90
|
throw new Error(`HPKE public key mismatch: expected ${hpkePublicKey}, got ${serverPublicKey}`);
|
|
78
91
|
}
|
|
79
92
|
return transport.request(requestUrl, requestInit);
|
|
80
93
|
}
|
|
81
|
-
function createEncryptedBodyFetch(baseURL, hpkePublicKey) {
|
|
94
|
+
function createEncryptedBodyFetch(baseURL, hpkePublicKey, hpkeKeyURL) {
|
|
82
95
|
return (async (input, init) => {
|
|
83
|
-
const base = new URL(baseURL);
|
|
84
|
-
if (base.protocol !== 'https:') {
|
|
85
|
-
throw new Error(`EHBP baseURL must use HTTPS. Got: ${baseURL}`);
|
|
86
|
-
}
|
|
87
96
|
const normalized = normalizeEncryptedBodyRequestArgs(input, init);
|
|
88
97
|
const targetUrl = new URL(normalized.url, baseURL);
|
|
89
|
-
return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init);
|
|
98
|
+
return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init, hpkeKeyURL);
|
|
90
99
|
});
|
|
91
100
|
}
|
|
92
101
|
// Private helper functions
|
|
@@ -122,10 +131,6 @@ async function getTransportForOrigin(origin) {
|
|
|
122
131
|
}
|
|
123
132
|
const transportPromise = (async () => {
|
|
124
133
|
const { Identity, createTransport } = await getEhbpModule();
|
|
125
|
-
const proto = new URL(origin).protocol;
|
|
126
|
-
if (proto !== 'https:') {
|
|
127
|
-
throw new Error(`EHBP requires HTTPS origin. Got: ${origin}`);
|
|
128
|
-
}
|
|
129
134
|
// Ensure we're in a secure context with WebCrypto Subtle available (required by EHBP)
|
|
130
135
|
if (typeof globalThis !== 'undefined') {
|
|
131
136
|
const isSecure = globalThis.isSecureContext !== false;
|
|
@@ -146,6 +151,38 @@ async function getTransportForOrigin(origin) {
|
|
|
146
151
|
transportCache.set(origin, transportPromise);
|
|
147
152
|
return transportPromise;
|
|
148
153
|
}
|
|
154
|
+
async function getTransportForRequestOriginWithKeyFrom(origin, keyOrigin) {
|
|
155
|
+
const cacheKey = `${origin}::${keyOrigin}`;
|
|
156
|
+
const cached = compositeTransportCache.get(cacheKey);
|
|
157
|
+
if (cached) {
|
|
158
|
+
return cached;
|
|
159
|
+
}
|
|
160
|
+
const transportPromise = (async () => {
|
|
161
|
+
const { Identity, createTransport, Transport } = await getEhbpModule();
|
|
162
|
+
// Ensure secure browser context
|
|
163
|
+
if (typeof globalThis !== 'undefined') {
|
|
164
|
+
const isSecure = globalThis.isSecureContext !== false;
|
|
165
|
+
const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
|
|
166
|
+
if (!isSecure || !hasSubtle) {
|
|
167
|
+
const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
|
|
168
|
+
throw new Error(`EHBP requires a secure browser context: ${reason}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Create a single client identity to use for both key discovery and requests
|
|
172
|
+
const clientIdentity = await Identity.generate();
|
|
173
|
+
// Fetch the server's HPKE public key from the dedicated key origin
|
|
174
|
+
const keyTransport = await createTransport(keyOrigin, clientIdentity);
|
|
175
|
+
// Reuse the discovered server public key but route to the request origin
|
|
176
|
+
const serverPublicKey = keyTransport.getServerPublicKey();
|
|
177
|
+
const requestHost = new URL(origin).host;
|
|
178
|
+
return new Transport(clientIdentity, requestHost, serverPublicKey);
|
|
179
|
+
})().catch((error) => {
|
|
180
|
+
compositeTransportCache.delete(cacheKey);
|
|
181
|
+
throw error;
|
|
182
|
+
});
|
|
183
|
+
compositeTransportCache.set(cacheKey, transportPromise);
|
|
184
|
+
return transportPromise;
|
|
185
|
+
}
|
|
149
186
|
// Test utilities
|
|
150
187
|
function __setEhbpModuleForTests(module) {
|
|
151
188
|
ehbpModuleOverride = module;
|
|
@@ -155,4 +192,5 @@ function __resetEhbpModuleStateForTests() {
|
|
|
155
192
|
ehbpModuleOverride = undefined;
|
|
156
193
|
ehbpModulePromise = null;
|
|
157
194
|
transportCache.clear();
|
|
195
|
+
compositeTransportCache.clear();
|
|
158
196
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
interface CreateTinfoilAIOptions {
|
|
2
2
|
baseURL?: string;
|
|
3
|
+
hpkeKeyURL?: string;
|
|
3
4
|
configRepo?: string;
|
|
4
5
|
}
|
|
5
6
|
export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
|
|
@@ -5,8 +5,8 @@ import { Verifier } from "./verifier";
|
|
|
5
5
|
import { isRealBrowser } from "./env";
|
|
6
6
|
export async function createTinfoilAI(apiKey, options = {}) {
|
|
7
7
|
const baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
8
|
+
const hpkeKeyURL = options.hpkeKeyURL || TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
8
9
|
const configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
9
|
-
assertHttpsUrl(baseURL, "Inference baseURL");
|
|
10
10
|
const verifier = new Verifier({ serverURL: baseURL, configRepo });
|
|
11
11
|
const attestationResponse = await verifier.verify();
|
|
12
12
|
const hpkePublicKey = attestationResponse.hpkePublicKey;
|
|
@@ -17,7 +17,7 @@ export async function createTinfoilAI(apiKey, options = {}) {
|
|
|
17
17
|
}
|
|
18
18
|
throw new Error("HPKE public key is required in browser environments");
|
|
19
19
|
}
|
|
20
|
-
const fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey);
|
|
20
|
+
const fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey, hpkeKeyURL);
|
|
21
21
|
return createOpenAICompatible({
|
|
22
22
|
name: "tinfoil",
|
|
23
23
|
baseURL: baseURL.replace(/\/$/, ""),
|
|
@@ -25,9 +25,3 @@ export async function createTinfoilAI(apiKey, options = {}) {
|
|
|
25
25
|
fetch: fetchFunction,
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
-
function assertHttpsUrl(url, context) {
|
|
29
|
-
const parsed = new URL(url);
|
|
30
|
-
if (parsed.protocol !== "https:") {
|
|
31
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
interface CreateTinfoilAIOptions {
|
|
2
2
|
/** Override the inference API base URL */
|
|
3
3
|
baseURL?: string;
|
|
4
|
+
/** Override the URL used to fetch the HPKE (defaults to baseURL) */
|
|
5
|
+
hpkeKeyURL?: string;
|
|
4
6
|
/** Override the config GitHub repository */
|
|
5
7
|
configRepo?: string;
|
|
6
8
|
}
|
|
@@ -16,8 +16,8 @@ import { Readable } from "stream";
|
|
|
16
16
|
*/
|
|
17
17
|
export async function createTinfoilAI(apiKey, options = {}) {
|
|
18
18
|
const baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
19
|
+
const hpkeKeyURL = options.hpkeKeyURL || TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
19
20
|
const configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
20
|
-
assertHttpsUrl(baseURL, "Inference baseURL");
|
|
21
21
|
// step 1: verify the enclave and extract the public keys
|
|
22
22
|
// from the attestation response
|
|
23
23
|
const verifier = new Verifier({ serverURL: baseURL, configRepo });
|
|
@@ -28,7 +28,7 @@ export async function createTinfoilAI(apiKey, options = {}) {
|
|
|
28
28
|
let fetchFunction;
|
|
29
29
|
if (hpkePublicKey) {
|
|
30
30
|
// HPKE available: use encrypted body fetch
|
|
31
|
-
fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey);
|
|
31
|
+
fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey, hpkeKeyURL);
|
|
32
32
|
}
|
|
33
33
|
else {
|
|
34
34
|
// HPKE not available: check if we're in a browser
|
|
@@ -51,12 +51,6 @@ export async function createTinfoilAI(apiKey, options = {}) {
|
|
|
51
51
|
fetch: fetchFunction,
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
|
-
function assertHttpsUrl(url, context) {
|
|
55
|
-
const parsed = new URL(url);
|
|
56
|
-
if (parsed.protocol !== "https:") {
|
|
57
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
54
|
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
61
55
|
return (async (input, init) => {
|
|
62
56
|
// Normalize URL
|
|
@@ -4,6 +4,7 @@ import type { VerificationDocument } from "./verifier";
|
|
|
4
4
|
interface TinfoilAIOptions {
|
|
5
5
|
apiKey?: string;
|
|
6
6
|
baseURL?: string;
|
|
7
|
+
hpkeKeyURL?: string;
|
|
7
8
|
configRepo?: string;
|
|
8
9
|
[key: string]: any;
|
|
9
10
|
}
|
|
@@ -15,6 +16,7 @@ export declare class TinfoilAI {
|
|
|
15
16
|
private verificationDocument?;
|
|
16
17
|
apiKey?: string;
|
|
17
18
|
baseURL?: string;
|
|
19
|
+
hpkeKeyURL?: string;
|
|
18
20
|
constructor(options?: TinfoilAIOptions);
|
|
19
21
|
ready(): Promise<void>;
|
|
20
22
|
private initClient;
|
|
@@ -35,7 +35,7 @@ export class TinfoilAI {
|
|
|
35
35
|
}
|
|
36
36
|
this.apiKey = openAIOptions.apiKey;
|
|
37
37
|
this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
38
|
-
|
|
38
|
+
this.hpkeKeyURL = options.hpkeKeyURL || TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
39
39
|
this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
40
40
|
this.clientPromise = this.initClient(openAIOptions);
|
|
41
41
|
}
|
|
@@ -74,7 +74,7 @@ export class TinfoilAI {
|
|
|
74
74
|
}
|
|
75
75
|
throw new Error("HPKE public key is required in browser environments");
|
|
76
76
|
}
|
|
77
|
-
const fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey);
|
|
77
|
+
const fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
78
78
|
const clientOptions = {
|
|
79
79
|
...options,
|
|
80
80
|
baseURL: this.baseURL,
|
|
@@ -142,9 +142,3 @@ export class TinfoilAI {
|
|
|
142
142
|
TinfoilAI.Uploads = OpenAI.Uploads;
|
|
143
143
|
TinfoilAI.VectorStores = OpenAI.VectorStores;
|
|
144
144
|
})(TinfoilAI || (TinfoilAI = {}));
|
|
145
|
-
function assertHttpsUrl(url, context) {
|
|
146
|
-
const parsed = new URL(url);
|
|
147
|
-
if (parsed.protocol !== "https:") {
|
|
148
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
149
|
-
}
|
|
150
|
-
}
|
package/dist/esm/client.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ interface TinfoilAIOptions {
|
|
|
14
14
|
apiKey?: string;
|
|
15
15
|
/** Override the inference API base URL */
|
|
16
16
|
baseURL?: string;
|
|
17
|
+
/** Override the URL used to fetch the HPKE key (defaults to baseURL) */
|
|
18
|
+
hpkeKeyURL?: string;
|
|
17
19
|
/** Override the config GitHub repository */
|
|
18
20
|
configRepo?: string;
|
|
19
21
|
[key: string]: any;
|
|
@@ -26,6 +28,7 @@ export declare class TinfoilAI {
|
|
|
26
28
|
private verificationDocument?;
|
|
27
29
|
apiKey?: string;
|
|
28
30
|
baseURL?: string;
|
|
31
|
+
hpkeKeyURL?: string;
|
|
29
32
|
/**
|
|
30
33
|
* Creates a new TinfoilAI instance.
|
|
31
34
|
* @param options - Configuration options including apiKey and other OpenAI client options
|
package/dist/esm/client.js
CHANGED
|
@@ -66,7 +66,7 @@ export class TinfoilAI {
|
|
|
66
66
|
// Store properties for compatibility
|
|
67
67
|
this.apiKey = openAIOptions.apiKey;
|
|
68
68
|
this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
69
|
-
|
|
69
|
+
this.hpkeKeyURL = options.hpkeKeyURL || TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
70
70
|
this.configRepo =
|
|
71
71
|
options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
72
72
|
this.clientPromise = this.initClient(openAIOptions);
|
|
@@ -107,7 +107,7 @@ export class TinfoilAI {
|
|
|
107
107
|
let fetchFunction;
|
|
108
108
|
if (hpkePublicKey) {
|
|
109
109
|
// HPKE available: use encrypted body fetch
|
|
110
|
-
fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey);
|
|
110
|
+
fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
111
111
|
}
|
|
112
112
|
else {
|
|
113
113
|
// HPKE not available: check if we're in a browser
|
|
@@ -243,12 +243,6 @@ export class TinfoilAI {
|
|
|
243
243
|
TinfoilAI.Uploads = OpenAI.Uploads;
|
|
244
244
|
TinfoilAI.VectorStores = OpenAI.VectorStores;
|
|
245
245
|
})(TinfoilAI || (TinfoilAI = {}));
|
|
246
|
-
function assertHttpsUrl(url, context) {
|
|
247
|
-
const parsed = new URL(url);
|
|
248
|
-
if (parsed.protocol !== "https:") {
|
|
249
|
-
throw new Error(`${context} must use HTTPS. Got: ${url}`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
246
|
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
253
247
|
return (async (input, init) => {
|
|
254
248
|
// Normalize URL
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ export declare const TINFOIL_CONFIG: {
|
|
|
6
6
|
* The full base URL for the inference API
|
|
7
7
|
*/
|
|
8
8
|
readonly INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/";
|
|
9
|
+
/**
|
|
10
|
+
* The URL used to fetch the HPKE server public key (via EHBP .well-known endpoint).
|
|
11
|
+
* Defaults to the same value as `INFERENCE_BASE_URL` so hpkeKeyURL and baseURL share
|
|
12
|
+
* the same default. Callers may override independently if key discovery is hosted elsewhere.
|
|
13
|
+
*/
|
|
14
|
+
readonly HPKE_KEY_URL: "https://inference.tinfoil.sh/v1/";
|
|
9
15
|
/**
|
|
10
16
|
* The GitHub repository for the confidential inference proxy
|
|
11
17
|
*/
|
package/dist/esm/config.js
CHANGED
|
@@ -6,6 +6,12 @@ export const TINFOIL_CONFIG = {
|
|
|
6
6
|
* The full base URL for the inference API
|
|
7
7
|
*/
|
|
8
8
|
INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/",
|
|
9
|
+
/**
|
|
10
|
+
* The URL used to fetch the HPKE server public key (via EHBP .well-known endpoint).
|
|
11
|
+
* Defaults to the same value as `INFERENCE_BASE_URL` so hpkeKeyURL and baseURL share
|
|
12
|
+
* the same default. Callers may override independently if key discovery is hosted elsewhere.
|
|
13
|
+
*/
|
|
14
|
+
HPKE_KEY_URL: "https://inference.tinfoil.sh/v1/",
|
|
9
15
|
/**
|
|
10
16
|
* The GitHub repository for the confidential inference proxy
|
|
11
17
|
*/
|
|
@@ -3,8 +3,8 @@ export declare function normalizeEncryptedBodyRequestArgs(input: RequestInfo | U
|
|
|
3
3
|
url: string;
|
|
4
4
|
init?: RequestInit;
|
|
5
5
|
};
|
|
6
|
-
export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit): Promise<Response>;
|
|
7
|
-
export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string): typeof fetch;
|
|
6
|
+
export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit, hpkeKeyURL?: string): Promise<Response>;
|
|
7
|
+
export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string, hpkeKeyURL?: string): typeof fetch;
|
|
8
8
|
export declare function __setEhbpModuleForTests(module: EhbpModule | undefined): void;
|
|
9
9
|
export declare function __resetEhbpModuleStateForTests(): void;
|
|
10
10
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isRealBrowser } from "./env";
|
|
2
2
|
const transportCache = new Map();
|
|
3
|
+
const compositeTransportCache = new Map();
|
|
3
4
|
let ehbpModulePromise = null;
|
|
4
5
|
let ehbpModuleOverride;
|
|
5
6
|
// Public API
|
|
@@ -23,30 +24,38 @@ export function normalizeEncryptedBodyRequestArgs(input, init) {
|
|
|
23
24
|
init: { ...derivedInit, ...init },
|
|
24
25
|
};
|
|
25
26
|
}
|
|
26
|
-
export async function encryptedBodyRequest(input, hpkePublicKey, init) {
|
|
27
|
+
export async function encryptedBodyRequest(input, hpkePublicKey, init, hpkeKeyURL) {
|
|
27
28
|
const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
|
|
28
29
|
const u = new URL(requestUrl);
|
|
29
|
-
if (u.protocol !== 'https:') {
|
|
30
|
-
throw new Error(`HTTP connections are not allowed for EHBP. Use HTTPS. URL: ${requestUrl}`);
|
|
31
|
-
}
|
|
32
30
|
const { origin } = u;
|
|
33
|
-
|
|
31
|
+
// Determine which origin to fetch the HPKE key from (may be different from request origin)
|
|
32
|
+
const keyOrigin = hpkeKeyURL ? new URL(hpkeKeyURL).origin : origin;
|
|
33
|
+
// If key origin matches request origin, use the standard transport (fetches keys from origin)
|
|
34
|
+
// Otherwise, build a composite transport that routes to `origin` but uses the key fetched from `keyOrigin`.
|
|
35
|
+
const transport = keyOrigin === origin
|
|
36
|
+
? await getTransportForOrigin(origin)
|
|
37
|
+
: await getTransportForRequestOriginWithKeyFrom(origin, keyOrigin);
|
|
34
38
|
const serverPublicKey = await transport.getServerPublicKeyHex();
|
|
35
39
|
if (serverPublicKey !== hpkePublicKey) {
|
|
36
|
-
|
|
40
|
+
// Clear caches so the next attempt re-handshakes
|
|
41
|
+
if (keyOrigin === origin) {
|
|
42
|
+
transportCache.delete(origin);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const cacheKey = `${origin}::${keyOrigin}`;
|
|
46
|
+
compositeTransportCache.delete(cacheKey);
|
|
47
|
+
// Also clear the underlying key-origin cache to force re-fetch
|
|
48
|
+
transportCache.delete(keyOrigin);
|
|
49
|
+
}
|
|
37
50
|
throw new Error(`HPKE public key mismatch: expected ${hpkePublicKey}, got ${serverPublicKey}`);
|
|
38
51
|
}
|
|
39
52
|
return transport.request(requestUrl, requestInit);
|
|
40
53
|
}
|
|
41
|
-
export function createEncryptedBodyFetch(baseURL, hpkePublicKey) {
|
|
54
|
+
export function createEncryptedBodyFetch(baseURL, hpkePublicKey, hpkeKeyURL) {
|
|
42
55
|
return (async (input, init) => {
|
|
43
|
-
const base = new URL(baseURL);
|
|
44
|
-
if (base.protocol !== 'https:') {
|
|
45
|
-
throw new Error(`EHBP baseURL must use HTTPS. Got: ${baseURL}`);
|
|
46
|
-
}
|
|
47
56
|
const normalized = normalizeEncryptedBodyRequestArgs(input, init);
|
|
48
57
|
const targetUrl = new URL(normalized.url, baseURL);
|
|
49
|
-
return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init);
|
|
58
|
+
return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init, hpkeKeyURL);
|
|
50
59
|
});
|
|
51
60
|
}
|
|
52
61
|
// Private helper functions
|
|
@@ -82,10 +91,6 @@ async function getTransportForOrigin(origin) {
|
|
|
82
91
|
}
|
|
83
92
|
const transportPromise = (async () => {
|
|
84
93
|
const { Identity, createTransport } = await getEhbpModule();
|
|
85
|
-
const proto = new URL(origin).protocol;
|
|
86
|
-
if (proto !== 'https:') {
|
|
87
|
-
throw new Error(`EHBP requires HTTPS origin. Got: ${origin}`);
|
|
88
|
-
}
|
|
89
94
|
// Ensure we're in a secure context with WebCrypto Subtle available (required by EHBP)
|
|
90
95
|
if (typeof globalThis !== 'undefined') {
|
|
91
96
|
const isSecure = globalThis.isSecureContext !== false;
|
|
@@ -106,6 +111,38 @@ async function getTransportForOrigin(origin) {
|
|
|
106
111
|
transportCache.set(origin, transportPromise);
|
|
107
112
|
return transportPromise;
|
|
108
113
|
}
|
|
114
|
+
async function getTransportForRequestOriginWithKeyFrom(origin, keyOrigin) {
|
|
115
|
+
const cacheKey = `${origin}::${keyOrigin}`;
|
|
116
|
+
const cached = compositeTransportCache.get(cacheKey);
|
|
117
|
+
if (cached) {
|
|
118
|
+
return cached;
|
|
119
|
+
}
|
|
120
|
+
const transportPromise = (async () => {
|
|
121
|
+
const { Identity, createTransport, Transport } = await getEhbpModule();
|
|
122
|
+
// Ensure secure browser context
|
|
123
|
+
if (typeof globalThis !== 'undefined') {
|
|
124
|
+
const isSecure = globalThis.isSecureContext !== false;
|
|
125
|
+
const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
|
|
126
|
+
if (!isSecure || !hasSubtle) {
|
|
127
|
+
const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
|
|
128
|
+
throw new Error(`EHBP requires a secure browser context: ${reason}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Create a single client identity to use for both key discovery and requests
|
|
132
|
+
const clientIdentity = await Identity.generate();
|
|
133
|
+
// Fetch the server's HPKE public key from the dedicated key origin
|
|
134
|
+
const keyTransport = await createTransport(keyOrigin, clientIdentity);
|
|
135
|
+
// Reuse the discovered server public key but route to the request origin
|
|
136
|
+
const serverPublicKey = keyTransport.getServerPublicKey();
|
|
137
|
+
const requestHost = new URL(origin).host;
|
|
138
|
+
return new Transport(clientIdentity, requestHost, serverPublicKey);
|
|
139
|
+
})().catch((error) => {
|
|
140
|
+
compositeTransportCache.delete(cacheKey);
|
|
141
|
+
throw error;
|
|
142
|
+
});
|
|
143
|
+
compositeTransportCache.set(cacheKey, transportPromise);
|
|
144
|
+
return transportPromise;
|
|
145
|
+
}
|
|
109
146
|
// Test utilities
|
|
110
147
|
export function __setEhbpModuleForTests(module) {
|
|
111
148
|
ehbpModuleOverride = module;
|
|
@@ -115,4 +152,5 @@ export function __resetEhbpModuleStateForTests() {
|
|
|
115
152
|
ehbpModuleOverride = undefined;
|
|
116
153
|
ehbpModulePromise = null;
|
|
117
154
|
transportCache.clear();
|
|
155
|
+
compositeTransportCache.clear();
|
|
118
156
|
}
|
|
@@ -61,11 +61,6 @@ export interface RunVerificationOptions {
|
|
|
61
61
|
export declare class VerifierWithState extends Verifier {
|
|
62
62
|
private state;
|
|
63
63
|
private subscribers;
|
|
64
|
-
private static runnerVerificationCache;
|
|
65
|
-
/**
|
|
66
|
-
* Clear the verification cache
|
|
67
|
-
*/
|
|
68
|
-
static clearVerificationCache(): void;
|
|
69
64
|
/**
|
|
70
65
|
* Subscribe to verification state updates
|
|
71
66
|
* @param subscriber - Callback function to receive state updates
|
|
@@ -94,7 +89,3 @@ export declare class VerifierWithState extends Verifier {
|
|
|
94
89
|
*/
|
|
95
90
|
export declare function loadVerifier(): Promise<VerifierWithState>;
|
|
96
91
|
export { suppressWasmLogs, TINFOIL_CONFIG };
|
|
97
|
-
/**
|
|
98
|
-
* Clear the verification cache
|
|
99
|
-
*/
|
|
100
|
-
export declare function clearVerificationCache(): void;
|
|
@@ -16,12 +16,6 @@ export class VerifierWithState extends Verifier {
|
|
|
16
16
|
// Subscribers
|
|
17
17
|
this.subscribers = new Set();
|
|
18
18
|
}
|
|
19
|
-
/**
|
|
20
|
-
* Clear the verification cache
|
|
21
|
-
*/
|
|
22
|
-
static clearVerificationCache() {
|
|
23
|
-
VerifierWithState.runnerVerificationCache.clear();
|
|
24
|
-
}
|
|
25
19
|
/**
|
|
26
20
|
* Subscribe to verification state updates
|
|
27
21
|
* @param subscriber - Callback function to receive state updates
|
|
@@ -92,35 +86,7 @@ export class VerifierWithState extends Verifier {
|
|
|
92
86
|
return this.state;
|
|
93
87
|
}
|
|
94
88
|
}
|
|
95
|
-
|
|
96
|
-
const cacheKey = `${configRepo}::${enclaveURL}::${releaseDigest}`;
|
|
97
|
-
const cachedPromise = VerifierWithState.runnerVerificationCache.get(cacheKey);
|
|
98
|
-
if (cachedPromise) {
|
|
99
|
-
// Reuse cached promise, but publish state updates for this instance
|
|
100
|
-
if (options?.onUpdate) {
|
|
101
|
-
const unsubscribe = this.subscribe(options.onUpdate);
|
|
102
|
-
// Get the cached result and update this instance's state to match
|
|
103
|
-
cachedPromise.then(cachedResult => {
|
|
104
|
-
// Update this instance's state to match the cached result
|
|
105
|
-
this.updateState(cachedResult);
|
|
106
|
-
}).finally(() => unsubscribe());
|
|
107
|
-
}
|
|
108
|
-
return cachedPromise;
|
|
109
|
-
}
|
|
110
|
-
// No cached result, create new verification promise
|
|
111
|
-
const verificationPromise = this._runVerificationInternal(configRepo, enclaveURL, releaseDigest)
|
|
112
|
-
.then(result => {
|
|
113
|
-
// Successful verification stays in cache
|
|
114
|
-
return result;
|
|
115
|
-
})
|
|
116
|
-
.catch(error => {
|
|
117
|
-
// On error, evict from cache
|
|
118
|
-
VerifierWithState.runnerVerificationCache.delete(cacheKey);
|
|
119
|
-
throw error;
|
|
120
|
-
});
|
|
121
|
-
// Store in cache
|
|
122
|
-
VerifierWithState.runnerVerificationCache.set(cacheKey, verificationPromise);
|
|
123
|
-
// Handle updates if requested
|
|
89
|
+
const verificationPromise = this._runVerificationInternal(configRepo, enclaveURL, releaseDigest);
|
|
124
90
|
if (options?.onUpdate) {
|
|
125
91
|
const unsubscribe = this.subscribe(options.onUpdate);
|
|
126
92
|
verificationPromise.finally(() => unsubscribe());
|
|
@@ -202,8 +168,6 @@ export class VerifierWithState extends Verifier {
|
|
|
202
168
|
}
|
|
203
169
|
}
|
|
204
170
|
}
|
|
205
|
-
// Static cache for deduplicating verifications
|
|
206
|
-
VerifierWithState.runnerVerificationCache = new Map();
|
|
207
171
|
/**
|
|
208
172
|
* Load and initialize a verifier instance with state management
|
|
209
173
|
* @returns Initialized verifier instance with state management
|
|
@@ -216,9 +180,3 @@ export async function loadVerifier() {
|
|
|
216
180
|
}
|
|
217
181
|
// Re-export utilities
|
|
218
182
|
export { suppressWasmLogs, TINFOIL_CONFIG };
|
|
219
|
-
/**
|
|
220
|
-
* Clear the verification cache
|
|
221
|
-
*/
|
|
222
|
-
export function clearVerificationCache() {
|
|
223
|
-
VerifierWithState.clearVerificationCache();
|
|
224
|
-
}
|
package/dist/esm/verifier.d.ts
CHANGED
|
@@ -52,13 +52,11 @@ export declare function compareMeasurements(codeMeasurement: AttestationMeasurem
|
|
|
52
52
|
export declare class Verifier {
|
|
53
53
|
private static goInstance;
|
|
54
54
|
private static initializationPromise;
|
|
55
|
-
private static verificationCache;
|
|
56
55
|
private static readonly defaultWasmUrl;
|
|
57
56
|
static originalFsWriteSync: ((fd: number, buf: Uint8Array) => number) | null;
|
|
58
57
|
static wasmLogsSuppressed: boolean;
|
|
59
58
|
static globalsInitialized: boolean;
|
|
60
59
|
private lastVerificationDocument?;
|
|
61
|
-
static clearVerificationCache(): void;
|
|
62
60
|
protected readonly serverURL: string;
|
|
63
61
|
protected readonly configRepo: string;
|
|
64
62
|
constructor(options?: {
|
package/dist/esm/verifier.js
CHANGED
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
* - Works in Node 20+ and modern browsers with lightweight polyfills for
|
|
29
29
|
* `performance`, `TextEncoder`/`TextDecoder`, and `crypto.getRandomValues`
|
|
30
30
|
* - Go stdout/stderr is suppressed by default; toggle via `suppressWasmLogs()`
|
|
31
|
-
* - Successful end-to-end results are cached per `configRepo::enclave::digest` for the process lifetime
|
|
32
31
|
* - Module auto-initializes the WASM runtime on import
|
|
33
32
|
*
|
|
34
33
|
* PROXIES AND TRUST
|
|
@@ -194,9 +193,6 @@ export function compareMeasurements(codeMeasurement, runtimeMeasurement) {
|
|
|
194
193
|
* 4. Compares measurements using platform-specific logic
|
|
195
194
|
*/
|
|
196
195
|
export class Verifier {
|
|
197
|
-
static clearVerificationCache() {
|
|
198
|
-
Verifier.verificationCache.clear();
|
|
199
|
-
}
|
|
200
196
|
constructor(options) {
|
|
201
197
|
const serverURL = options?.serverURL ?? TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
202
198
|
this.serverURL = new URL(serverURL).hostname;
|
|
@@ -411,55 +407,29 @@ export class Verifier {
|
|
|
411
407
|
* @throws Error if measurements don't match or verification fails
|
|
412
408
|
*/
|
|
413
409
|
async verify() {
|
|
414
|
-
// Get latest release digest first
|
|
410
|
+
// Get latest release digest first
|
|
415
411
|
const releaseDigest = await this.fetchLatestDigest(this.configRepo);
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
enclaveHost: this.serverURL,
|
|
427
|
-
releaseDigest: releaseDigest,
|
|
428
|
-
codeMeasurement,
|
|
429
|
-
enclaveMeasurement: attestation,
|
|
430
|
-
match: true, // Must be true since verification succeeded
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
return attestation;
|
|
412
|
+
// Perform code attestation and runtime attestation in parallel
|
|
413
|
+
const [{ measurement: codeMeasurement }, attestation] = await Promise.all([
|
|
414
|
+
this.verifyCode(this.configRepo, releaseDigest),
|
|
415
|
+
this.verifyEnclave(this.serverURL),
|
|
416
|
+
]);
|
|
417
|
+
// Compare measurements using platform-specific logic
|
|
418
|
+
const measurementsMatchError = compareMeasurementsError(codeMeasurement, attestation.measurement);
|
|
419
|
+
if (measurementsMatchError) {
|
|
420
|
+
throw new Error(`Measurement verification failed: Code measurement (${codeMeasurement.type}) ` +
|
|
421
|
+
`does not match runtime measurement (${attestation.measurement.type}): ${measurementsMatchError.message}`);
|
|
434
422
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
`does not match runtime measurement (${attestation.measurement.type}): ${measurementsMatchError.message}`);
|
|
446
|
-
}
|
|
447
|
-
// Persist a full verification document for later retrieval by clients
|
|
448
|
-
this.lastVerificationDocument = {
|
|
449
|
-
configRepo: this.configRepo,
|
|
450
|
-
enclaveHost: this.serverURL,
|
|
451
|
-
releaseDigest: releaseDigest,
|
|
452
|
-
codeMeasurement,
|
|
453
|
-
enclaveMeasurement: attestation,
|
|
454
|
-
match: true,
|
|
455
|
-
};
|
|
456
|
-
return attestation;
|
|
457
|
-
})();
|
|
458
|
-
Verifier.verificationCache.set(cacheKey, verificationPromise);
|
|
459
|
-
verificationPromise.catch(() => {
|
|
460
|
-
Verifier.verificationCache.delete(cacheKey);
|
|
461
|
-
});
|
|
462
|
-
return verificationPromise;
|
|
423
|
+
// Persist a full verification document for later retrieval by clients
|
|
424
|
+
this.lastVerificationDocument = {
|
|
425
|
+
configRepo: this.configRepo,
|
|
426
|
+
enclaveHost: this.serverURL,
|
|
427
|
+
releaseDigest: releaseDigest,
|
|
428
|
+
codeMeasurement,
|
|
429
|
+
enclaveMeasurement: attestation,
|
|
430
|
+
match: true,
|
|
431
|
+
};
|
|
432
|
+
return attestation;
|
|
463
433
|
}
|
|
464
434
|
/**
|
|
465
435
|
* Returns the full verification document from the last successful verify() call
|
|
@@ -470,7 +440,6 @@ export class Verifier {
|
|
|
470
440
|
}
|
|
471
441
|
Verifier.goInstance = null;
|
|
472
442
|
Verifier.initializationPromise = null;
|
|
473
|
-
Verifier.verificationCache = new Map();
|
|
474
443
|
Verifier.defaultWasmUrl = "https://tinfoilsh.github.io/verifier-js/tinfoil-verifier.wasm";
|
|
475
444
|
Verifier.originalFsWriteSync = null;
|
|
476
445
|
Verifier.wasmLogsSuppressed = true;
|
|
@@ -61,11 +61,6 @@ export interface RunVerificationOptions {
|
|
|
61
61
|
export declare class VerifierWithState extends Verifier {
|
|
62
62
|
private state;
|
|
63
63
|
private subscribers;
|
|
64
|
-
private static runnerVerificationCache;
|
|
65
|
-
/**
|
|
66
|
-
* Clear the verification cache
|
|
67
|
-
*/
|
|
68
|
-
static clearVerificationCache(): void;
|
|
69
64
|
/**
|
|
70
65
|
* Subscribe to verification state updates
|
|
71
66
|
* @param subscriber - Callback function to receive state updates
|
|
@@ -94,7 +89,3 @@ export declare class VerifierWithState extends Verifier {
|
|
|
94
89
|
*/
|
|
95
90
|
export declare function loadVerifier(): Promise<VerifierWithState>;
|
|
96
91
|
export { suppressWasmLogs, TINFOIL_CONFIG };
|
|
97
|
-
/**
|
|
98
|
-
* Clear the verification cache
|
|
99
|
-
*/
|
|
100
|
-
export declare function clearVerificationCache(): void;
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TINFOIL_CONFIG = exports.suppressWasmLogs = exports.VerifierWithState = void 0;
|
|
4
4
|
exports.loadVerifier = loadVerifier;
|
|
5
|
-
exports.clearVerificationCache = clearVerificationCache;
|
|
6
5
|
const verifier_1 = require("./verifier");
|
|
7
6
|
Object.defineProperty(exports, "suppressWasmLogs", { enumerable: true, get: function () { return verifier_1.suppressWasmLogs; } });
|
|
8
7
|
const config_1 = require("./config");
|
|
@@ -23,12 +22,6 @@ class VerifierWithState extends verifier_1.Verifier {
|
|
|
23
22
|
// Subscribers
|
|
24
23
|
this.subscribers = new Set();
|
|
25
24
|
}
|
|
26
|
-
/**
|
|
27
|
-
* Clear the verification cache
|
|
28
|
-
*/
|
|
29
|
-
static clearVerificationCache() {
|
|
30
|
-
VerifierWithState.runnerVerificationCache.clear();
|
|
31
|
-
}
|
|
32
25
|
/**
|
|
33
26
|
* Subscribe to verification state updates
|
|
34
27
|
* @param subscriber - Callback function to receive state updates
|
|
@@ -99,35 +92,7 @@ class VerifierWithState extends verifier_1.Verifier {
|
|
|
99
92
|
return this.state;
|
|
100
93
|
}
|
|
101
94
|
}
|
|
102
|
-
|
|
103
|
-
const cacheKey = `${configRepo}::${enclaveURL}::${releaseDigest}`;
|
|
104
|
-
const cachedPromise = VerifierWithState.runnerVerificationCache.get(cacheKey);
|
|
105
|
-
if (cachedPromise) {
|
|
106
|
-
// Reuse cached promise, but publish state updates for this instance
|
|
107
|
-
if (options?.onUpdate) {
|
|
108
|
-
const unsubscribe = this.subscribe(options.onUpdate);
|
|
109
|
-
// Get the cached result and update this instance's state to match
|
|
110
|
-
cachedPromise.then(cachedResult => {
|
|
111
|
-
// Update this instance's state to match the cached result
|
|
112
|
-
this.updateState(cachedResult);
|
|
113
|
-
}).finally(() => unsubscribe());
|
|
114
|
-
}
|
|
115
|
-
return cachedPromise;
|
|
116
|
-
}
|
|
117
|
-
// No cached result, create new verification promise
|
|
118
|
-
const verificationPromise = this._runVerificationInternal(configRepo, enclaveURL, releaseDigest)
|
|
119
|
-
.then(result => {
|
|
120
|
-
// Successful verification stays in cache
|
|
121
|
-
return result;
|
|
122
|
-
})
|
|
123
|
-
.catch(error => {
|
|
124
|
-
// On error, evict from cache
|
|
125
|
-
VerifierWithState.runnerVerificationCache.delete(cacheKey);
|
|
126
|
-
throw error;
|
|
127
|
-
});
|
|
128
|
-
// Store in cache
|
|
129
|
-
VerifierWithState.runnerVerificationCache.set(cacheKey, verificationPromise);
|
|
130
|
-
// Handle updates if requested
|
|
95
|
+
const verificationPromise = this._runVerificationInternal(configRepo, enclaveURL, releaseDigest);
|
|
131
96
|
if (options?.onUpdate) {
|
|
132
97
|
const unsubscribe = this.subscribe(options.onUpdate);
|
|
133
98
|
verificationPromise.finally(() => unsubscribe());
|
|
@@ -210,8 +175,6 @@ class VerifierWithState extends verifier_1.Verifier {
|
|
|
210
175
|
}
|
|
211
176
|
}
|
|
212
177
|
exports.VerifierWithState = VerifierWithState;
|
|
213
|
-
// Static cache for deduplicating verifications
|
|
214
|
-
VerifierWithState.runnerVerificationCache = new Map();
|
|
215
178
|
/**
|
|
216
179
|
* Load and initialize a verifier instance with state management
|
|
217
180
|
* @returns Initialized verifier instance with state management
|
|
@@ -222,9 +185,3 @@ async function loadVerifier() {
|
|
|
222
185
|
// Return a new verifier instance with state management
|
|
223
186
|
return new VerifierWithState();
|
|
224
187
|
}
|
|
225
|
-
/**
|
|
226
|
-
* Clear the verification cache
|
|
227
|
-
*/
|
|
228
|
-
function clearVerificationCache() {
|
|
229
|
-
VerifierWithState.clearVerificationCache();
|
|
230
|
-
}
|
package/dist/verifier.d.ts
CHANGED
|
@@ -52,13 +52,11 @@ export declare function compareMeasurements(codeMeasurement: AttestationMeasurem
|
|
|
52
52
|
export declare class Verifier {
|
|
53
53
|
private static goInstance;
|
|
54
54
|
private static initializationPromise;
|
|
55
|
-
private static verificationCache;
|
|
56
55
|
private static readonly defaultWasmUrl;
|
|
57
56
|
static originalFsWriteSync: ((fd: number, buf: Uint8Array) => number) | null;
|
|
58
57
|
static wasmLogsSuppressed: boolean;
|
|
59
58
|
static globalsInitialized: boolean;
|
|
60
59
|
private lastVerificationDocument?;
|
|
61
|
-
static clearVerificationCache(): void;
|
|
62
60
|
protected readonly serverURL: string;
|
|
63
61
|
protected readonly configRepo: string;
|
|
64
62
|
constructor(options?: {
|
package/dist/verifier.js
CHANGED
|
@@ -67,7 +67,6 @@ exports.suppressWasmLogs = suppressWasmLogs;
|
|
|
67
67
|
* - Works in Node 20+ and modern browsers with lightweight polyfills for
|
|
68
68
|
* `performance`, `TextEncoder`/`TextDecoder`, and `crypto.getRandomValues`
|
|
69
69
|
* - Go stdout/stderr is suppressed by default; toggle via `suppressWasmLogs()`
|
|
70
|
-
* - Successful end-to-end results are cached per `configRepo::enclave::digest` for the process lifetime
|
|
71
70
|
* - Module auto-initializes the WASM runtime on import
|
|
72
71
|
*
|
|
73
72
|
* PROXIES AND TRUST
|
|
@@ -233,9 +232,6 @@ function compareMeasurements(codeMeasurement, runtimeMeasurement) {
|
|
|
233
232
|
* 4. Compares measurements using platform-specific logic
|
|
234
233
|
*/
|
|
235
234
|
class Verifier {
|
|
236
|
-
static clearVerificationCache() {
|
|
237
|
-
Verifier.verificationCache.clear();
|
|
238
|
-
}
|
|
239
235
|
constructor(options) {
|
|
240
236
|
const serverURL = options?.serverURL ?? config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
241
237
|
this.serverURL = new URL(serverURL).hostname;
|
|
@@ -450,55 +446,29 @@ class Verifier {
|
|
|
450
446
|
* @throws Error if measurements don't match or verification fails
|
|
451
447
|
*/
|
|
452
448
|
async verify() {
|
|
453
|
-
// Get latest release digest first
|
|
449
|
+
// Get latest release digest first
|
|
454
450
|
const releaseDigest = await this.fetchLatestDigest(this.configRepo);
|
|
455
|
-
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
enclaveHost: this.serverURL,
|
|
466
|
-
releaseDigest: releaseDigest,
|
|
467
|
-
codeMeasurement,
|
|
468
|
-
enclaveMeasurement: attestation,
|
|
469
|
-
match: true, // Must be true since verification succeeded
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
return attestation;
|
|
451
|
+
// Perform code attestation and runtime attestation in parallel
|
|
452
|
+
const [{ measurement: codeMeasurement }, attestation] = await Promise.all([
|
|
453
|
+
this.verifyCode(this.configRepo, releaseDigest),
|
|
454
|
+
this.verifyEnclave(this.serverURL),
|
|
455
|
+
]);
|
|
456
|
+
// Compare measurements using platform-specific logic
|
|
457
|
+
const measurementsMatchError = compareMeasurementsError(codeMeasurement, attestation.measurement);
|
|
458
|
+
if (measurementsMatchError) {
|
|
459
|
+
throw new Error(`Measurement verification failed: Code measurement (${codeMeasurement.type}) ` +
|
|
460
|
+
`does not match runtime measurement (${attestation.measurement.type}): ${measurementsMatchError.message}`);
|
|
473
461
|
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
`does not match runtime measurement (${attestation.measurement.type}): ${measurementsMatchError.message}`);
|
|
485
|
-
}
|
|
486
|
-
// Persist a full verification document for later retrieval by clients
|
|
487
|
-
this.lastVerificationDocument = {
|
|
488
|
-
configRepo: this.configRepo,
|
|
489
|
-
enclaveHost: this.serverURL,
|
|
490
|
-
releaseDigest: releaseDigest,
|
|
491
|
-
codeMeasurement,
|
|
492
|
-
enclaveMeasurement: attestation,
|
|
493
|
-
match: true,
|
|
494
|
-
};
|
|
495
|
-
return attestation;
|
|
496
|
-
})();
|
|
497
|
-
Verifier.verificationCache.set(cacheKey, verificationPromise);
|
|
498
|
-
verificationPromise.catch(() => {
|
|
499
|
-
Verifier.verificationCache.delete(cacheKey);
|
|
500
|
-
});
|
|
501
|
-
return verificationPromise;
|
|
462
|
+
// Persist a full verification document for later retrieval by clients
|
|
463
|
+
this.lastVerificationDocument = {
|
|
464
|
+
configRepo: this.configRepo,
|
|
465
|
+
enclaveHost: this.serverURL,
|
|
466
|
+
releaseDigest: releaseDigest,
|
|
467
|
+
codeMeasurement,
|
|
468
|
+
enclaveMeasurement: attestation,
|
|
469
|
+
match: true,
|
|
470
|
+
};
|
|
471
|
+
return attestation;
|
|
502
472
|
}
|
|
503
473
|
/**
|
|
504
474
|
* Returns the full verification document from the last successful verify() call
|
|
@@ -510,7 +480,6 @@ class Verifier {
|
|
|
510
480
|
exports.Verifier = Verifier;
|
|
511
481
|
Verifier.goInstance = null;
|
|
512
482
|
Verifier.initializationPromise = null;
|
|
513
|
-
Verifier.verificationCache = new Map();
|
|
514
483
|
Verifier.defaultWasmUrl = "https://tinfoilsh.github.io/verifier-js/tinfoil-verifier.wasm";
|
|
515
484
|
Verifier.originalFsWriteSync = null;
|
|
516
485
|
Verifier.wasmLogsSuppressed = true;
|