tinfoil 0.8.2 → 0.8.4
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.d.ts +1 -9
- package/dist/ai-sdk-provider.js +9 -157
- package/dist/client.browser.d.ts +3 -0
- package/dist/client.browser.js +13 -34
- package/dist/client.d.ts +4 -0
- package/dist/client.js +13 -159
- 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.d.ts +1 -9
- package/dist/esm/ai-sdk-provider.js +9 -154
- package/dist/esm/client.browser.d.ts +3 -0
- package/dist/esm/client.browser.js +13 -34
- package/dist/esm/client.d.ts +4 -0
- package/dist/esm/client.js +13 -159
- 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/index.browser.d.ts +2 -1
- package/dist/esm/index.browser.js +2 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/pinned-tls-fetch.d.ts +1 -0
- package/dist/esm/pinned-tls-fetch.js +110 -0
- package/dist/esm/secure-client.browser.d.ts +20 -0
- package/dist/esm/secure-client.browser.js +57 -0
- package/dist/esm/secure-client.d.ts +20 -0
- package/dist/esm/secure-client.js +68 -0
- 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/index.browser.d.ts +2 -1
- package/dist/index.browser.js +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/pinned-tls-fetch.d.ts +1 -0
- package/dist/pinned-tls-fetch.js +116 -0
- package/dist/secure-client.browser.d.ts +20 -0
- package/dist/secure-client.browser.js +61 -0
- package/dist/secure-client.d.ts +20 -0
- package/dist/secure-client.js +72 -0
- 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 +7 -2
- package/dist/ai-sdk-provider.browser.d.ts +0 -6
- package/dist/ai-sdk-provider.browser.js +0 -36
- package/dist/esm/ai-sdk-provider.browser.d.ts +0 -6
- package/dist/esm/ai-sdk-provider.browser.js +0 -33
package/dist/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __exportStar(require("./verifier"), exports);
|
|
|
25
25
|
__exportStar(require("./verification-runner"), exports);
|
|
26
26
|
__exportStar(require("./ai-sdk-provider"), exports);
|
|
27
27
|
__exportStar(require("./config"), exports);
|
|
28
|
+
__exportStar(require("./secure-client"), exports);
|
|
28
29
|
// Re-export OpenAI utility types and classes that users might need
|
|
29
30
|
// Using public exports from the main OpenAI package instead of deep imports
|
|
30
31
|
var openai_1 = require("openai");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createPinnedTlsFetch(expectedFingerprintHex: string): typeof fetch;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createPinnedTlsFetch = createPinnedTlsFetch;
|
|
7
|
+
const https_1 = __importDefault(require("https"));
|
|
8
|
+
const tls_1 = require("tls");
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
const stream_1 = require("stream");
|
|
11
|
+
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
12
|
+
return (async (input, init) => {
|
|
13
|
+
// Normalize URL
|
|
14
|
+
const makeURL = (value) => {
|
|
15
|
+
if (typeof value === "string")
|
|
16
|
+
return new URL(value);
|
|
17
|
+
if (value instanceof URL)
|
|
18
|
+
return value;
|
|
19
|
+
return new URL(value.url);
|
|
20
|
+
};
|
|
21
|
+
const url = makeURL(input);
|
|
22
|
+
if (url.protocol !== "https:") {
|
|
23
|
+
throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
|
|
24
|
+
}
|
|
25
|
+
// Gather method and headers
|
|
26
|
+
const method = (init?.method || input.method || "GET").toUpperCase();
|
|
27
|
+
const headers = new Headers(init?.headers || input?.headers || {});
|
|
28
|
+
const headerObj = {};
|
|
29
|
+
headers.forEach((v, k) => {
|
|
30
|
+
headerObj[k] = v;
|
|
31
|
+
});
|
|
32
|
+
// Resolve body
|
|
33
|
+
let body = init?.body;
|
|
34
|
+
if (!body && input instanceof Request) {
|
|
35
|
+
// If the original was a Request with a body, read it
|
|
36
|
+
try {
|
|
37
|
+
const buf = await input.arrayBuffer();
|
|
38
|
+
if (buf && buf.byteLength)
|
|
39
|
+
body = Buffer.from(buf);
|
|
40
|
+
}
|
|
41
|
+
catch { }
|
|
42
|
+
}
|
|
43
|
+
// Convert web streams to Node streams if needed
|
|
44
|
+
if (body && typeof body.getReader === "function") {
|
|
45
|
+
body = stream_1.Readable.fromWeb(body);
|
|
46
|
+
}
|
|
47
|
+
if (body instanceof ArrayBuffer) {
|
|
48
|
+
body = Buffer.from(body);
|
|
49
|
+
}
|
|
50
|
+
if (ArrayBuffer.isView(body)) {
|
|
51
|
+
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
52
|
+
}
|
|
53
|
+
const requestOptions = {
|
|
54
|
+
protocol: url.protocol,
|
|
55
|
+
hostname: url.hostname,
|
|
56
|
+
port: url.port ? Number(url.port) : 443,
|
|
57
|
+
path: `${url.pathname}${url.search}`,
|
|
58
|
+
method,
|
|
59
|
+
headers: headerObj,
|
|
60
|
+
checkServerIdentity: (host, cert) => {
|
|
61
|
+
const raw = cert.raw;
|
|
62
|
+
if (!raw) {
|
|
63
|
+
return new Error("Certificate raw bytes are unavailable for pinning");
|
|
64
|
+
}
|
|
65
|
+
const x509 = new crypto_1.X509Certificate(raw);
|
|
66
|
+
const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
|
|
67
|
+
const fp = (0, crypto_1.createHash)("sha256").update(publicKeyDer).digest("hex");
|
|
68
|
+
if (fp !== expectedFingerprintHex) {
|
|
69
|
+
return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
|
|
70
|
+
}
|
|
71
|
+
return (0, tls_1.checkServerIdentity)(host, cert);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
const { signal } = init || {};
|
|
75
|
+
const res = await new Promise((resolve, reject) => {
|
|
76
|
+
const req = https_1.default.request(requestOptions, resolve);
|
|
77
|
+
req.on("error", reject);
|
|
78
|
+
if (signal) {
|
|
79
|
+
if (signal.aborted) {
|
|
80
|
+
req.destroy(new Error("Request aborted"));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
|
|
84
|
+
}
|
|
85
|
+
if (body === undefined || body === null) {
|
|
86
|
+
req.end();
|
|
87
|
+
}
|
|
88
|
+
else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
|
|
89
|
+
req.end(body);
|
|
90
|
+
}
|
|
91
|
+
else if (typeof body.pipe === "function") {
|
|
92
|
+
body.pipe(req);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
// Fallback: try to serialize objects
|
|
96
|
+
req.end(String(body));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
const responseHeaders = new Headers();
|
|
100
|
+
for (const [k, v] of Object.entries(res.headers)) {
|
|
101
|
+
if (Array.isArray(v)) {
|
|
102
|
+
v.forEach(item => responseHeaders.append(k, item));
|
|
103
|
+
}
|
|
104
|
+
else if (v != null) {
|
|
105
|
+
responseHeaders.set(k, String(v));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Convert Node stream to Web ReadableStream
|
|
109
|
+
const webStream = stream_1.Readable.toWeb(res);
|
|
110
|
+
return new Response(webStream, {
|
|
111
|
+
status: res.statusCode || 0,
|
|
112
|
+
statusText: res.statusMessage || "",
|
|
113
|
+
headers: responseHeaders,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { VerificationDocument } from "./verifier";
|
|
2
|
+
interface SecureClientOptions {
|
|
3
|
+
baseURL?: string;
|
|
4
|
+
hpkeKeyURL?: string;
|
|
5
|
+
configRepo?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class SecureClient {
|
|
8
|
+
private initPromise;
|
|
9
|
+
private verificationDocument;
|
|
10
|
+
private _fetch;
|
|
11
|
+
private readonly baseURL?;
|
|
12
|
+
private readonly hpkeKeyURL?;
|
|
13
|
+
private readonly configRepo?;
|
|
14
|
+
constructor(options?: SecureClientOptions);
|
|
15
|
+
ready(): Promise<void>;
|
|
16
|
+
private initSecureClient;
|
|
17
|
+
getVerificationDocument(): Promise<VerificationDocument>;
|
|
18
|
+
get fetch(): typeof fetch;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SecureClient = void 0;
|
|
4
|
+
const verifier_1 = require("./verifier");
|
|
5
|
+
const config_1 = require("./config");
|
|
6
|
+
const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
|
|
7
|
+
const env_1 = require("./env");
|
|
8
|
+
class SecureClient {
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.initPromise = null;
|
|
11
|
+
this.verificationDocument = null;
|
|
12
|
+
this._fetch = null;
|
|
13
|
+
this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
14
|
+
this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
15
|
+
this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
16
|
+
}
|
|
17
|
+
async ready() {
|
|
18
|
+
if (!this.initPromise) {
|
|
19
|
+
this.initPromise = this.initSecureClient();
|
|
20
|
+
}
|
|
21
|
+
return this.initPromise;
|
|
22
|
+
}
|
|
23
|
+
async initSecureClient() {
|
|
24
|
+
const verifier = new verifier_1.Verifier({
|
|
25
|
+
serverURL: this.baseURL,
|
|
26
|
+
configRepo: this.configRepo,
|
|
27
|
+
});
|
|
28
|
+
await verifier.verify();
|
|
29
|
+
const doc = verifier.getVerificationDocument();
|
|
30
|
+
if (!doc) {
|
|
31
|
+
throw new Error("Verification document not available after successful verification");
|
|
32
|
+
}
|
|
33
|
+
this.verificationDocument = doc;
|
|
34
|
+
const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
|
|
35
|
+
if (!hpkePublicKey) {
|
|
36
|
+
if ((0, env_1.isRealBrowser)()) {
|
|
37
|
+
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
38
|
+
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
39
|
+
}
|
|
40
|
+
throw new Error("HPKE public key is required in browser environments");
|
|
41
|
+
}
|
|
42
|
+
this._fetch = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
43
|
+
}
|
|
44
|
+
async getVerificationDocument() {
|
|
45
|
+
if (!this.initPromise) {
|
|
46
|
+
await this.ready();
|
|
47
|
+
}
|
|
48
|
+
await this.initPromise;
|
|
49
|
+
if (!this.verificationDocument) {
|
|
50
|
+
throw new Error("Verification document unavailable: client not verified yet");
|
|
51
|
+
}
|
|
52
|
+
return this.verificationDocument;
|
|
53
|
+
}
|
|
54
|
+
get fetch() {
|
|
55
|
+
return async (input, init) => {
|
|
56
|
+
await this.ready();
|
|
57
|
+
return this._fetch(input, init);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.SecureClient = SecureClient;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { VerificationDocument } from "./verifier";
|
|
2
|
+
interface SecureClientOptions {
|
|
3
|
+
baseURL?: string;
|
|
4
|
+
hpkeKeyURL?: string;
|
|
5
|
+
configRepo?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class SecureClient {
|
|
8
|
+
private initPromise;
|
|
9
|
+
private verificationDocument;
|
|
10
|
+
private _fetch;
|
|
11
|
+
private readonly baseURL?;
|
|
12
|
+
private readonly hpkeKeyURL?;
|
|
13
|
+
private readonly configRepo?;
|
|
14
|
+
constructor(options?: SecureClientOptions);
|
|
15
|
+
ready(): Promise<void>;
|
|
16
|
+
private initSecureClient;
|
|
17
|
+
getVerificationDocument(): Promise<VerificationDocument>;
|
|
18
|
+
get fetch(): typeof fetch;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SecureClient = void 0;
|
|
4
|
+
const verifier_1 = require("./verifier");
|
|
5
|
+
const config_1 = require("./config");
|
|
6
|
+
const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
|
|
7
|
+
const pinned_tls_fetch_1 = require("./pinned-tls-fetch");
|
|
8
|
+
const env_1 = require("./env");
|
|
9
|
+
class SecureClient {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.initPromise = null;
|
|
12
|
+
this.verificationDocument = null;
|
|
13
|
+
this._fetch = null;
|
|
14
|
+
this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
15
|
+
this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
16
|
+
this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
17
|
+
}
|
|
18
|
+
async ready() {
|
|
19
|
+
if (!this.initPromise) {
|
|
20
|
+
this.initPromise = this.initSecureClient();
|
|
21
|
+
}
|
|
22
|
+
return this.initPromise;
|
|
23
|
+
}
|
|
24
|
+
async initSecureClient() {
|
|
25
|
+
const verifier = new verifier_1.Verifier({
|
|
26
|
+
serverURL: this.baseURL,
|
|
27
|
+
configRepo: this.configRepo,
|
|
28
|
+
});
|
|
29
|
+
await verifier.verify();
|
|
30
|
+
const doc = verifier.getVerificationDocument();
|
|
31
|
+
if (!doc) {
|
|
32
|
+
throw new Error("Verification document not available after successful verification");
|
|
33
|
+
}
|
|
34
|
+
this.verificationDocument = doc;
|
|
35
|
+
const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
|
|
36
|
+
let fetchFunction;
|
|
37
|
+
if (hpkePublicKey) {
|
|
38
|
+
// HPKE available: use encrypted body fetch
|
|
39
|
+
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// HPKE not available: check if we're in a browser
|
|
43
|
+
if ((0, env_1.isRealBrowser)()) {
|
|
44
|
+
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
45
|
+
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
46
|
+
}
|
|
47
|
+
// Node.js environment: fall back to TLS-only verification using pinned TLS fetch
|
|
48
|
+
if (!tlsPublicKeyFingerprint) {
|
|
49
|
+
throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
50
|
+
}
|
|
51
|
+
fetchFunction = (0, pinned_tls_fetch_1.createPinnedTlsFetch)(tlsPublicKeyFingerprint);
|
|
52
|
+
}
|
|
53
|
+
this._fetch = fetchFunction;
|
|
54
|
+
}
|
|
55
|
+
async getVerificationDocument() {
|
|
56
|
+
if (!this.initPromise) {
|
|
57
|
+
await this.ready();
|
|
58
|
+
}
|
|
59
|
+
await this.initPromise;
|
|
60
|
+
if (!this.verificationDocument) {
|
|
61
|
+
throw new Error("Verification document unavailable: client not verified yet");
|
|
62
|
+
}
|
|
63
|
+
return this.verificationDocument;
|
|
64
|
+
}
|
|
65
|
+
get fetch() {
|
|
66
|
+
return async (input, init) => {
|
|
67
|
+
await this.ready();
|
|
68
|
+
return this._fetch(input, init);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.SecureClient = SecureClient;
|
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinfoil",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"description": "Tinfoil secure OpenAI client wrapper",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
},
|
|
26
26
|
"./ai-sdk-provider": {
|
|
27
27
|
"types": "./dist/ai-sdk-provider.d.ts",
|
|
28
|
-
"browser": "./dist/esm/ai-sdk-provider.browser.js",
|
|
29
28
|
"import": "./dist/esm/ai-sdk-provider.js",
|
|
30
29
|
"require": "./dist/ai-sdk-provider.js"
|
|
31
30
|
},
|
|
@@ -43,6 +42,12 @@
|
|
|
43
42
|
"types": "./dist/config.d.ts",
|
|
44
43
|
"import": "./dist/esm/config.js",
|
|
45
44
|
"require": "./dist/config.js"
|
|
45
|
+
},
|
|
46
|
+
"./secure-client": {
|
|
47
|
+
"types": "./dist/secure-client.d.ts",
|
|
48
|
+
"browser": "./dist/esm/secure-client.browser.js",
|
|
49
|
+
"import": "./dist/esm/secure-client.js",
|
|
50
|
+
"require": "./dist/secure-client.js"
|
|
46
51
|
}
|
|
47
52
|
},
|
|
48
53
|
"scripts": {
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
interface CreateTinfoilAIOptions {
|
|
2
|
-
baseURL?: string;
|
|
3
|
-
configRepo?: string;
|
|
4
|
-
}
|
|
5
|
-
export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
|
|
6
|
-
export {};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTinfoilAI = createTinfoilAI;
|
|
4
|
-
const openai_compatible_1 = require("@ai-sdk/openai-compatible");
|
|
5
|
-
const config_1 = require("./config");
|
|
6
|
-
const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
|
|
7
|
-
const verifier_1 = require("./verifier");
|
|
8
|
-
const env_1 = require("./env");
|
|
9
|
-
async function createTinfoilAI(apiKey, options = {}) {
|
|
10
|
-
const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
11
|
-
const configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
12
|
-
assertHttpsUrl(baseURL, "Inference baseURL");
|
|
13
|
-
const verifier = new verifier_1.Verifier({ serverURL: baseURL, configRepo });
|
|
14
|
-
const attestationResponse = await verifier.verify();
|
|
15
|
-
const hpkePublicKey = attestationResponse.hpkePublicKey;
|
|
16
|
-
if (!hpkePublicKey) {
|
|
17
|
-
if ((0, env_1.isRealBrowser)()) {
|
|
18
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
19
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
20
|
-
}
|
|
21
|
-
throw new Error("HPKE public key is required in browser environments");
|
|
22
|
-
}
|
|
23
|
-
const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey);
|
|
24
|
-
return (0, openai_compatible_1.createOpenAICompatible)({
|
|
25
|
-
name: "tinfoil",
|
|
26
|
-
baseURL: baseURL.replace(/\/$/, ""),
|
|
27
|
-
apiKey: apiKey,
|
|
28
|
-
fetch: fetchFunction,
|
|
29
|
-
});
|
|
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 +0,0 @@
|
|
|
1
|
-
interface CreateTinfoilAIOptions {
|
|
2
|
-
baseURL?: string;
|
|
3
|
-
configRepo?: string;
|
|
4
|
-
}
|
|
5
|
-
export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
|
|
6
|
-
export {};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
2
|
-
import { TINFOIL_CONFIG } from "./config";
|
|
3
|
-
import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
|
|
4
|
-
import { Verifier } from "./verifier";
|
|
5
|
-
import { isRealBrowser } from "./env";
|
|
6
|
-
export async function createTinfoilAI(apiKey, options = {}) {
|
|
7
|
-
const baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
8
|
-
const configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
9
|
-
assertHttpsUrl(baseURL, "Inference baseURL");
|
|
10
|
-
const verifier = new Verifier({ serverURL: baseURL, configRepo });
|
|
11
|
-
const attestationResponse = await verifier.verify();
|
|
12
|
-
const hpkePublicKey = attestationResponse.hpkePublicKey;
|
|
13
|
-
if (!hpkePublicKey) {
|
|
14
|
-
if (isRealBrowser()) {
|
|
15
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
16
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
17
|
-
}
|
|
18
|
-
throw new Error("HPKE public key is required in browser environments");
|
|
19
|
-
}
|
|
20
|
-
const fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey);
|
|
21
|
-
return createOpenAICompatible({
|
|
22
|
-
name: "tinfoil",
|
|
23
|
-
baseURL: baseURL.replace(/\/$/, ""),
|
|
24
|
-
apiKey: apiKey,
|
|
25
|
-
fetch: fetchFunction,
|
|
26
|
-
});
|
|
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
|
-
}
|