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/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,15 +1,7 @@
|
|
|
1
1
|
interface CreateTinfoilAIOptions {
|
|
2
|
-
/** Override the inference API base URL */
|
|
3
2
|
baseURL?: string;
|
|
4
|
-
|
|
3
|
+
hpkeKeyURL?: string;
|
|
5
4
|
configRepo?: string;
|
|
6
5
|
}
|
|
7
|
-
/**
|
|
8
|
-
* Creates an AI SDK provider with the specified API key.
|
|
9
|
-
*
|
|
10
|
-
* @param apiKey - The API key for the Tinfoil API
|
|
11
|
-
* @param options - Optional configuration options
|
|
12
|
-
* @returns A TinfoilAI instance
|
|
13
|
-
*/
|
|
14
6
|
export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
|
|
15
7
|
export {};
|
package/dist/ai-sdk-provider.js
CHANGED
|
@@ -1,171 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.createTinfoilAI = createTinfoilAI;
|
|
7
4
|
const openai_compatible_1 = require("@ai-sdk/openai-compatible");
|
|
8
5
|
const config_1 = require("./config");
|
|
9
|
-
const
|
|
10
|
-
const verifier_1 = require("./verifier");
|
|
11
|
-
const env_1 = require("./env");
|
|
12
|
-
const https_1 = __importDefault(require("https"));
|
|
13
|
-
const tls_1 = require("tls");
|
|
14
|
-
const crypto_1 = require("crypto");
|
|
15
|
-
const stream_1 = require("stream");
|
|
16
|
-
/**
|
|
17
|
-
* Creates an AI SDK provider with the specified API key.
|
|
18
|
-
*
|
|
19
|
-
* @param apiKey - The API key for the Tinfoil API
|
|
20
|
-
* @param options - Optional configuration options
|
|
21
|
-
* @returns A TinfoilAI instance
|
|
22
|
-
*/
|
|
6
|
+
const secure_client_1 = require("./secure-client");
|
|
23
7
|
async function createTinfoilAI(apiKey, options = {}) {
|
|
24
8
|
const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
9
|
+
const hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
25
10
|
const configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const tlsPublicKeyFingerprint = attestationResponse.tlsPublicKeyFingerprint;
|
|
33
|
-
// step 2: create the appropriate fetch function based on available keys
|
|
34
|
-
let fetchFunction;
|
|
35
|
-
if (hpkePublicKey) {
|
|
36
|
-
// HPKE available: use encrypted body fetch
|
|
37
|
-
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
// HPKE not available: check if we're in a browser
|
|
41
|
-
if ((0, env_1.isRealBrowser)()) {
|
|
42
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
43
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
44
|
-
}
|
|
45
|
-
// Node.js environment: fall back to TLS-only verification using pinned TLS fetch
|
|
46
|
-
if (!tlsPublicKeyFingerprint) {
|
|
47
|
-
throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
48
|
-
}
|
|
49
|
-
fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
|
|
50
|
-
}
|
|
51
|
-
// step 3: create the openai compatible provider
|
|
52
|
-
// that uses the appropriate fetch function
|
|
11
|
+
const secureClient = new secure_client_1.SecureClient({
|
|
12
|
+
baseURL,
|
|
13
|
+
hpkeKeyURL,
|
|
14
|
+
configRepo,
|
|
15
|
+
});
|
|
16
|
+
await secureClient.ready();
|
|
53
17
|
return (0, openai_compatible_1.createOpenAICompatible)({
|
|
54
18
|
name: "tinfoil",
|
|
55
19
|
baseURL: baseURL.replace(/\/$/, ""),
|
|
56
20
|
apiKey: apiKey,
|
|
57
|
-
fetch:
|
|
58
|
-
});
|
|
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
|
-
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
67
|
-
return (async (input, init) => {
|
|
68
|
-
// Normalize URL
|
|
69
|
-
const makeURL = (value) => {
|
|
70
|
-
if (typeof value === "string")
|
|
71
|
-
return new URL(value);
|
|
72
|
-
if (value instanceof URL)
|
|
73
|
-
return value;
|
|
74
|
-
return new URL(value.url);
|
|
75
|
-
};
|
|
76
|
-
const url = makeURL(input);
|
|
77
|
-
if (url.protocol !== "https:") {
|
|
78
|
-
throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
|
|
79
|
-
}
|
|
80
|
-
// Gather method and headers
|
|
81
|
-
const method = (init?.method || input.method || "GET").toUpperCase();
|
|
82
|
-
const headers = new Headers(init?.headers || input?.headers || {});
|
|
83
|
-
const headerObj = {};
|
|
84
|
-
headers.forEach((v, k) => {
|
|
85
|
-
headerObj[k] = v;
|
|
86
|
-
});
|
|
87
|
-
// Resolve body
|
|
88
|
-
let body = init?.body;
|
|
89
|
-
if (!body && input instanceof Request) {
|
|
90
|
-
// If the original was a Request with a body, read it
|
|
91
|
-
try {
|
|
92
|
-
const buf = await input.arrayBuffer();
|
|
93
|
-
if (buf && buf.byteLength)
|
|
94
|
-
body = Buffer.from(buf);
|
|
95
|
-
}
|
|
96
|
-
catch { }
|
|
97
|
-
}
|
|
98
|
-
// Convert web streams to Node streams if needed
|
|
99
|
-
if (body && typeof body.getReader === "function") {
|
|
100
|
-
body = stream_1.Readable.fromWeb(body);
|
|
101
|
-
}
|
|
102
|
-
if (body instanceof ArrayBuffer) {
|
|
103
|
-
body = Buffer.from(body);
|
|
104
|
-
}
|
|
105
|
-
if (ArrayBuffer.isView(body)) {
|
|
106
|
-
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
107
|
-
}
|
|
108
|
-
const requestOptions = {
|
|
109
|
-
protocol: url.protocol,
|
|
110
|
-
hostname: url.hostname,
|
|
111
|
-
port: url.port ? Number(url.port) : 443,
|
|
112
|
-
path: `${url.pathname}${url.search}`,
|
|
113
|
-
method,
|
|
114
|
-
headers: headerObj,
|
|
115
|
-
checkServerIdentity: (host, cert) => {
|
|
116
|
-
const raw = cert.raw;
|
|
117
|
-
if (!raw) {
|
|
118
|
-
return new Error("Certificate raw bytes are unavailable for pinning");
|
|
119
|
-
}
|
|
120
|
-
const x509 = new crypto_1.X509Certificate(raw);
|
|
121
|
-
const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
|
|
122
|
-
const fp = (0, crypto_1.createHash)("sha256").update(publicKeyDer).digest("hex");
|
|
123
|
-
if (fp !== expectedFingerprintHex) {
|
|
124
|
-
return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
|
|
125
|
-
}
|
|
126
|
-
return (0, tls_1.checkServerIdentity)(host, cert);
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
const { signal } = init || {};
|
|
130
|
-
const res = await new Promise((resolve, reject) => {
|
|
131
|
-
const req = https_1.default.request(requestOptions, resolve);
|
|
132
|
-
req.on("error", reject);
|
|
133
|
-
if (signal) {
|
|
134
|
-
if (signal.aborted) {
|
|
135
|
-
req.destroy(new Error("Request aborted"));
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
|
|
139
|
-
}
|
|
140
|
-
if (body === undefined || body === null) {
|
|
141
|
-
req.end();
|
|
142
|
-
}
|
|
143
|
-
else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
|
|
144
|
-
req.end(body);
|
|
145
|
-
}
|
|
146
|
-
else if (typeof body.pipe === "function") {
|
|
147
|
-
body.pipe(req);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
// Fallback: try to serialize objects
|
|
151
|
-
req.end(String(body));
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
const responseHeaders = new Headers();
|
|
155
|
-
for (const [k, v] of Object.entries(res.headers)) {
|
|
156
|
-
if (Array.isArray(v)) {
|
|
157
|
-
v.forEach(item => responseHeaders.append(k, item));
|
|
158
|
-
}
|
|
159
|
-
else if (v != null) {
|
|
160
|
-
responseHeaders.set(k, String(v));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
// Convert Node stream to Web ReadableStream
|
|
164
|
-
const webStream = stream_1.Readable.toWeb(res);
|
|
165
|
-
return new Response(webStream, {
|
|
166
|
-
status: res.statusCode || 0,
|
|
167
|
-
statusText: res.statusMessage || "",
|
|
168
|
-
headers: responseHeaders,
|
|
169
|
-
});
|
|
21
|
+
fetch: secureClient.fetch,
|
|
170
22
|
});
|
|
171
23
|
}
|
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
|
}
|
|
@@ -12,9 +13,11 @@ export declare class TinfoilAI {
|
|
|
12
13
|
private clientPromise;
|
|
13
14
|
private readyPromise?;
|
|
14
15
|
private configRepo?;
|
|
16
|
+
private secureClient;
|
|
15
17
|
private verificationDocument?;
|
|
16
18
|
apiKey?: string;
|
|
17
19
|
baseURL?: string;
|
|
20
|
+
hpkeKeyURL?: string;
|
|
18
21
|
constructor(options?: TinfoilAIOptions);
|
|
19
22
|
ready(): Promise<void>;
|
|
20
23
|
private initClient;
|
package/dist/client.browser.js
CHANGED
|
@@ -5,10 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.TinfoilAI = void 0;
|
|
7
7
|
const openai_1 = __importDefault(require("openai"));
|
|
8
|
-
const
|
|
8
|
+
const secure_client_browser_1 = require("./secure-client.browser");
|
|
9
9
|
const config_1 = require("./config");
|
|
10
|
-
const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
|
|
11
|
-
const env_1 = require("./env");
|
|
12
10
|
function createAsyncProxy(promise) {
|
|
13
11
|
return new Proxy({}, {
|
|
14
12
|
get(target, prop) {
|
|
@@ -41,8 +39,14 @@ class TinfoilAI {
|
|
|
41
39
|
}
|
|
42
40
|
this.apiKey = openAIOptions.apiKey;
|
|
43
41
|
this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
44
|
-
|
|
42
|
+
this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
45
43
|
this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
44
|
+
// Create the secure client for handling transport security
|
|
45
|
+
this.secureClient = new secure_client_browser_1.SecureClient({
|
|
46
|
+
baseURL: this.baseURL,
|
|
47
|
+
hpkeKeyURL: this.hpkeKeyURL,
|
|
48
|
+
configRepo: this.configRepo,
|
|
49
|
+
});
|
|
46
50
|
this.clientPromise = this.initClient(openAIOptions);
|
|
47
51
|
}
|
|
48
52
|
async ready() {
|
|
@@ -57,34 +61,15 @@ class TinfoilAI {
|
|
|
57
61
|
return this.createOpenAIClient(options);
|
|
58
62
|
}
|
|
59
63
|
async createOpenAIClient(options = {}) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
await verifier.verify();
|
|
66
|
-
this.verificationDocument = verifier.getVerificationDocument();
|
|
67
|
-
if (!this.verificationDocument) {
|
|
68
|
-
throw new Error("Verification document not available after successful verification");
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
catch (error) {
|
|
72
|
-
throw new Error(`Failed to verify enclave: ${error}`);
|
|
73
|
-
}
|
|
74
|
-
const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
|
|
75
|
-
if (!hpkePublicKey) {
|
|
76
|
-
// In browsers we require HPKE; TLS pinning fallback is Node-only
|
|
77
|
-
if ((0, env_1.isRealBrowser)()) {
|
|
78
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
79
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
80
|
-
}
|
|
81
|
-
throw new Error("HPKE public key is required in browser environments");
|
|
64
|
+
await this.secureClient.ready();
|
|
65
|
+
this.verificationDocument = await this.secureClient.getVerificationDocument();
|
|
66
|
+
if (!this.verificationDocument) {
|
|
67
|
+
throw new Error("Verification document not available after successful verification");
|
|
82
68
|
}
|
|
83
|
-
const fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey);
|
|
84
69
|
const clientOptions = {
|
|
85
70
|
...options,
|
|
86
71
|
baseURL: this.baseURL,
|
|
87
|
-
fetch:
|
|
72
|
+
fetch: this.secureClient.fetch,
|
|
88
73
|
};
|
|
89
74
|
// In browser usage, OpenAI SDK typically needs dangerouslyAllowBrowser
|
|
90
75
|
clientOptions.dangerouslyAllowBrowser = true;
|
|
@@ -149,9 +134,3 @@ exports.TinfoilAI = TinfoilAI;
|
|
|
149
134
|
TinfoilAI.Uploads = openai_1.default.Uploads;
|
|
150
135
|
TinfoilAI.VectorStores = openai_1.default.VectorStores;
|
|
151
136
|
})(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;
|
|
@@ -23,9 +25,11 @@ export declare class TinfoilAI {
|
|
|
23
25
|
private clientPromise;
|
|
24
26
|
private readyPromise?;
|
|
25
27
|
private configRepo?;
|
|
28
|
+
private secureClient;
|
|
26
29
|
private verificationDocument?;
|
|
27
30
|
apiKey?: string;
|
|
28
31
|
baseURL?: string;
|
|
32
|
+
hpkeKeyURL?: string;
|
|
29
33
|
/**
|
|
30
34
|
* Creates a new TinfoilAI instance.
|
|
31
35
|
* @param options - Configuration options including apiKey and other OpenAI client options
|
package/dist/client.js
CHANGED
|
@@ -5,17 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.TinfoilAI = void 0;
|
|
7
7
|
const openai_1 = __importDefault(require("openai"));
|
|
8
|
-
const
|
|
8
|
+
const secure_client_1 = require("./secure-client");
|
|
9
9
|
const config_1 = require("./config");
|
|
10
|
-
const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
|
|
11
|
-
const https_1 = __importDefault(require("https"));
|
|
12
|
-
const tls_1 = require("tls");
|
|
13
|
-
const crypto_1 = require("crypto");
|
|
14
|
-
const stream_1 = require("stream");
|
|
15
|
-
/**
|
|
16
|
-
* Detects if the code is running in a real browser environment.
|
|
17
|
-
* Returns false for Node.js environments, even with WASM loaded.
|
|
18
|
-
*/
|
|
19
10
|
const env_1 = require("./env");
|
|
20
11
|
/**
|
|
21
12
|
* Creates a proxy that allows property access and method calls on a Promise before it resolves.
|
|
@@ -72,9 +63,13 @@ class TinfoilAI {
|
|
|
72
63
|
// Store properties for compatibility
|
|
73
64
|
this.apiKey = openAIOptions.apiKey;
|
|
74
65
|
this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
75
|
-
|
|
76
|
-
this.configRepo =
|
|
77
|
-
|
|
66
|
+
this.hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
|
|
67
|
+
this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
68
|
+
this.secureClient = new secure_client_1.SecureClient({
|
|
69
|
+
baseURL: this.baseURL,
|
|
70
|
+
hpkeKeyURL: this.hpkeKeyURL,
|
|
71
|
+
configRepo: this.configRepo,
|
|
72
|
+
});
|
|
78
73
|
this.clientPromise = this.initClient(openAIOptions);
|
|
79
74
|
}
|
|
80
75
|
/**
|
|
@@ -93,44 +88,15 @@ class TinfoilAI {
|
|
|
93
88
|
return this.createOpenAIClient(options);
|
|
94
89
|
}
|
|
95
90
|
async createOpenAIClient(options = {}) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
});
|
|
101
|
-
try {
|
|
102
|
-
await verifier.verify();
|
|
103
|
-
this.verificationDocument = verifier.getVerificationDocument();
|
|
104
|
-
if (!this.verificationDocument) {
|
|
105
|
-
throw new Error("Verification document not available after successful verification");
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
throw new Error(`Failed to verify enclave: ${error}`);
|
|
110
|
-
}
|
|
111
|
-
const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
|
|
112
|
-
const tlsPublicKeyFingerprint = this.verificationDocument.enclaveMeasurement.tlsPublicKeyFingerprint;
|
|
113
|
-
let fetchFunction;
|
|
114
|
-
if (hpkePublicKey) {
|
|
115
|
-
// HPKE available: use encrypted body fetch
|
|
116
|
-
fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
// HPKE not available: check if we're in a browser
|
|
120
|
-
if ((0, env_1.isRealBrowser)()) {
|
|
121
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
122
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
123
|
-
}
|
|
124
|
-
// Node.js environment: fall back to TLS-only verification using pinned TLS fetch
|
|
125
|
-
if (!tlsPublicKeyFingerprint) {
|
|
126
|
-
throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
127
|
-
}
|
|
128
|
-
fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
|
|
91
|
+
await this.secureClient.ready();
|
|
92
|
+
this.verificationDocument = await this.secureClient.getVerificationDocument();
|
|
93
|
+
if (!this.verificationDocument) {
|
|
94
|
+
throw new Error("Verification document not available after successful verification");
|
|
129
95
|
}
|
|
130
96
|
const clientOptions = {
|
|
131
97
|
...options,
|
|
132
98
|
baseURL: this.baseURL,
|
|
133
|
-
fetch:
|
|
99
|
+
fetch: this.secureClient.fetch,
|
|
134
100
|
};
|
|
135
101
|
// Enable dangerouslyAllowBrowser in Node.js with WASM (which makes OpenAI SDK think we're in a browser)
|
|
136
102
|
// OR if explicitly set by the user (for legitimate browser usage like playgrounds)
|
|
@@ -250,115 +216,3 @@ exports.TinfoilAI = TinfoilAI;
|
|
|
250
216
|
TinfoilAI.Uploads = openai_1.default.Uploads;
|
|
251
217
|
TinfoilAI.VectorStores = openai_1.default.VectorStores;
|
|
252
218
|
})(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
|
-
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
260
|
-
return (async (input, init) => {
|
|
261
|
-
// Normalize URL
|
|
262
|
-
const makeURL = (value) => {
|
|
263
|
-
if (typeof value === "string")
|
|
264
|
-
return new URL(value);
|
|
265
|
-
if (value instanceof URL)
|
|
266
|
-
return value;
|
|
267
|
-
return new URL(value.url);
|
|
268
|
-
};
|
|
269
|
-
const url = makeURL(input);
|
|
270
|
-
if (url.protocol !== "https:") {
|
|
271
|
-
throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
|
|
272
|
-
}
|
|
273
|
-
// Gather method and headers
|
|
274
|
-
const method = (init?.method || input.method || "GET").toUpperCase();
|
|
275
|
-
const headers = new Headers(init?.headers || input?.headers || {});
|
|
276
|
-
const headerObj = {};
|
|
277
|
-
headers.forEach((v, k) => {
|
|
278
|
-
headerObj[k] = v;
|
|
279
|
-
});
|
|
280
|
-
// Resolve body
|
|
281
|
-
let body = init?.body;
|
|
282
|
-
if (!body && input instanceof Request) {
|
|
283
|
-
// If the original was a Request with a body, read it
|
|
284
|
-
try {
|
|
285
|
-
const buf = await input.arrayBuffer();
|
|
286
|
-
if (buf && buf.byteLength)
|
|
287
|
-
body = Buffer.from(buf);
|
|
288
|
-
}
|
|
289
|
-
catch { }
|
|
290
|
-
}
|
|
291
|
-
// Convert web streams to Node streams if needed
|
|
292
|
-
if (body && typeof body.getReader === "function") {
|
|
293
|
-
body = stream_1.Readable.fromWeb(body);
|
|
294
|
-
}
|
|
295
|
-
if (body instanceof ArrayBuffer) {
|
|
296
|
-
body = Buffer.from(body);
|
|
297
|
-
}
|
|
298
|
-
if (ArrayBuffer.isView(body)) {
|
|
299
|
-
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
300
|
-
}
|
|
301
|
-
const requestOptions = {
|
|
302
|
-
protocol: url.protocol,
|
|
303
|
-
hostname: url.hostname,
|
|
304
|
-
port: url.port ? Number(url.port) : 443,
|
|
305
|
-
path: `${url.pathname}${url.search}`,
|
|
306
|
-
method,
|
|
307
|
-
headers: headerObj,
|
|
308
|
-
checkServerIdentity: (host, cert) => {
|
|
309
|
-
const raw = cert.raw;
|
|
310
|
-
if (!raw) {
|
|
311
|
-
return new Error("Certificate raw bytes are unavailable for pinning");
|
|
312
|
-
}
|
|
313
|
-
const x509 = new crypto_1.X509Certificate(raw);
|
|
314
|
-
const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
|
|
315
|
-
const fp = (0, crypto_1.createHash)("sha256").update(publicKeyDer).digest("hex");
|
|
316
|
-
if (fp !== expectedFingerprintHex) {
|
|
317
|
-
return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
|
|
318
|
-
}
|
|
319
|
-
return (0, tls_1.checkServerIdentity)(host, cert);
|
|
320
|
-
},
|
|
321
|
-
};
|
|
322
|
-
const { signal } = init || {};
|
|
323
|
-
const res = await new Promise((resolve, reject) => {
|
|
324
|
-
const req = https_1.default.request(requestOptions, resolve);
|
|
325
|
-
req.on("error", reject);
|
|
326
|
-
if (signal) {
|
|
327
|
-
if (signal.aborted) {
|
|
328
|
-
req.destroy(new Error("Request aborted"));
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
|
|
332
|
-
}
|
|
333
|
-
if (body === undefined || body === null) {
|
|
334
|
-
req.end();
|
|
335
|
-
}
|
|
336
|
-
else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
|
|
337
|
-
req.end(body);
|
|
338
|
-
}
|
|
339
|
-
else if (typeof body.pipe === "function") {
|
|
340
|
-
body.pipe(req);
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
// Fallback: try to serialize objects
|
|
344
|
-
req.end(String(body));
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
const responseHeaders = new Headers();
|
|
348
|
-
for (const [k, v] of Object.entries(res.headers)) {
|
|
349
|
-
if (Array.isArray(v)) {
|
|
350
|
-
v.forEach(item => responseHeaders.append(k, item));
|
|
351
|
-
}
|
|
352
|
-
else if (v != null) {
|
|
353
|
-
responseHeaders.set(k, String(v));
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
// Convert Node stream to Web ReadableStream
|
|
357
|
-
const webStream = stream_1.Readable.toWeb(res);
|
|
358
|
-
return new Response(webStream, {
|
|
359
|
-
status: res.statusCode || 0,
|
|
360
|
-
statusText: res.statusMessage || "",
|
|
361
|
-
headers: responseHeaders,
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
}
|
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 {};
|