tinfoil 0.8.6 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -71
- package/dist/ai-sdk-provider.js +1 -1
- package/dist/encrypted-body-fetch.d.ts +1 -0
- package/dist/encrypted-body-fetch.js +43 -80
- package/dist/esm/ai-sdk-provider.js +1 -1
- package/dist/esm/encrypted-body-fetch.d.ts +1 -0
- package/dist/esm/encrypted-body-fetch.js +42 -80
- package/dist/esm/index.browser.d.ts +1 -2
- package/dist/esm/index.browser.js +1 -2
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.js +2 -3
- package/dist/esm/pinned-tls-fetch.d.ts +1 -1
- package/dist/esm/pinned-tls-fetch.js +4 -4
- package/dist/esm/secure-client.js +2 -21
- package/dist/esm/secure-fetch.browser.d.ts +1 -0
- package/dist/esm/secure-fetch.browser.js +10 -0
- package/dist/esm/secure-fetch.d.ts +1 -0
- package/dist/esm/secure-fetch.js +22 -0
- package/dist/esm/tinfoilai.js +1 -1
- package/dist/esm/verifier.d.ts +25 -5
- package/dist/esm/verifier.js +120 -56
- package/dist/index.browser.d.ts +1 -2
- package/dist/index.browser.js +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +4 -4
- package/dist/pinned-tls-fetch.d.ts +1 -1
- package/dist/pinned-tls-fetch.js +4 -4
- package/dist/secure-client.js +2 -21
- package/dist/secure-fetch.browser.d.ts +1 -0
- package/dist/secure-fetch.browser.js +13 -0
- package/dist/secure-fetch.d.ts +1 -0
- package/dist/secure-fetch.js +25 -0
- package/dist/tinfoilai.js +1 -1
- package/dist/verifier.d.ts +25 -5
- package/dist/verifier.js +120 -56
- package/package.json +7 -7
- package/dist/esm/secure-client.browser.d.ts +0 -20
- package/dist/esm/secure-client.browser.js +0 -57
- package/dist/esm/verification-runner.d.ts +0 -91
- package/dist/esm/verification-runner.js +0 -182
- package/dist/secure-client.browser.d.ts +0 -20
- package/dist/secure-client.browser.js +0 -61
- package/dist/verification-runner.d.ts +0 -91
- package/dist/verification-runner.js +0 -187
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ This package exposes verification helpers that load the Go-based WebAssembly ver
|
|
|
75
75
|
|
|
76
76
|
The verification functionality is split into two modules:
|
|
77
77
|
- `verifier.ts`: Core verification logic with low-level attestation methods
|
|
78
|
-
|
|
78
|
+
|
|
79
79
|
|
|
80
80
|
### Core Verifier API
|
|
81
81
|
|
|
@@ -96,76 +96,6 @@ const code = await verifier.verifyCode("tinfoilsh/repo", "digest-hash");
|
|
|
96
96
|
const digest = await verifier.fetchLatestDigest("tinfoilsh/repo");
|
|
97
97
|
```
|
|
98
98
|
|
|
99
|
-
### High-level Orchestration API
|
|
100
|
-
|
|
101
|
-
- `loadVerifier()` boots the verifier with state management and returns an enhanced client.
|
|
102
|
-
- `client.subscribe(callback)` subscribes to real-time verification state updates.
|
|
103
|
-
- `client.runVerification({ configRepo?, serverURL?, releaseDigest?, onUpdate? })` orchestrates the full flow and returns a structured result with step statuses and a comparison outcome. Both `configRepo` and `serverURL` default to values from `TINFOIL_CONFIG`.
|
|
104
|
-
|
|
105
|
-
### End-to-end orchestration
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
import { loadVerifier, TINFOIL_CONFIG } from "tinfoil";
|
|
109
|
-
|
|
110
|
-
const verifier = await loadVerifier();
|
|
111
|
-
|
|
112
|
-
const result = await verifier.runVerification({
|
|
113
|
-
onUpdate: (state) => {
|
|
114
|
-
// Receive stepwise updates: pending -> loading -> success/error
|
|
115
|
-
// Useful for logging or progress indicators
|
|
116
|
-
console.log("verification update:", state);
|
|
117
|
-
},
|
|
118
|
-
// Optional: override defaults if needed
|
|
119
|
-
// configRepo: "tinfoilsh/confidential-inference-proxy",
|
|
120
|
-
// serverURL: "https://inference.tinfoil.sh",
|
|
121
|
-
// releaseDigest: "<specific-release-sha256>",
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
if (result.verification.status === "success" && result.verification.securityVerified) {
|
|
125
|
-
console.log("Measurements match. Digest:", result.releaseDigest);
|
|
126
|
-
} else {
|
|
127
|
-
console.error("Verification failed:", result);
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
`runVerification` returns:
|
|
132
|
-
|
|
133
|
-
```typescript
|
|
134
|
-
type VerificationResult = {
|
|
135
|
-
code: { status: StepStatus; measurement?: AttestationMeasurement; error?: string };
|
|
136
|
-
runtime: {
|
|
137
|
-
status: StepStatus;
|
|
138
|
-
measurement?: AttestationMeasurement;
|
|
139
|
-
tlsPublicKeyFingerprint?: string;
|
|
140
|
-
hpkePublicKey?: string;
|
|
141
|
-
error?: string;
|
|
142
|
-
};
|
|
143
|
-
verification: { status: StepStatus; securityVerified?: boolean; error?: string };
|
|
144
|
-
releaseDigest: string;
|
|
145
|
-
};
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### Subscribe to state updates
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
const verifier = await loadVerifier();
|
|
152
|
-
const unsubscribe = verifier.subscribe((state) => {
|
|
153
|
-
console.log("state:", state);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
// Run verification with default config (recommended)
|
|
157
|
-
await verifier.runVerification();
|
|
158
|
-
|
|
159
|
-
// Or override specific parameters if needed
|
|
160
|
-
// await verifier.runVerification({
|
|
161
|
-
// configRepo: "tinfoilsh/confidential-inference-proxy",
|
|
162
|
-
// serverURL: "https://inference.tinfoil.sh",
|
|
163
|
-
// releaseDigest: "<specific-release-sha256>",
|
|
164
|
-
// });
|
|
165
|
-
|
|
166
|
-
unsubscribe();
|
|
167
|
-
```
|
|
168
|
-
|
|
169
99
|
## Testing
|
|
170
100
|
|
|
171
101
|
The project includes both unit tests and integration tests:
|
|
@@ -201,6 +131,7 @@ The chat example demonstrates both streaming chat completions and real-time atte
|
|
|
201
131
|
|
|
202
132
|
```bash
|
|
203
133
|
npm install
|
|
134
|
+
npm run build
|
|
204
135
|
```
|
|
205
136
|
|
|
206
137
|
3. Optionally create a `.env` file with your configuration:
|
package/dist/ai-sdk-provider.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createTinfoilAI = createTinfoilAI;
|
|
4
4
|
const openai_compatible_1 = require("@ai-sdk/openai-compatible");
|
|
5
5
|
const config_1 = require("./config");
|
|
6
|
-
const secure_client_1 = require("
|
|
6
|
+
const secure_client_1 = require("./secure-client");
|
|
7
7
|
async function createTinfoilAI(apiKey, options = {}) {
|
|
8
8
|
const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
9
9
|
const enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
|
|
@@ -33,17 +33,36 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getHPKEKey = getHPKEKey;
|
|
36
37
|
exports.normalizeEncryptedBodyRequestArgs = normalizeEncryptedBodyRequestArgs;
|
|
37
38
|
exports.encryptedBodyRequest = encryptedBodyRequest;
|
|
38
39
|
exports.createEncryptedBodyFetch = createEncryptedBodyFetch;
|
|
39
40
|
exports.__setEhbpModuleForTests = __setEhbpModuleForTests;
|
|
40
41
|
exports.__resetEhbpModuleStateForTests = __resetEhbpModuleStateForTests;
|
|
41
42
|
const env_1 = require("./env");
|
|
42
|
-
|
|
43
|
-
const compositeTransportCache = new Map();
|
|
43
|
+
let transport = null;
|
|
44
44
|
let ehbpModulePromise = null;
|
|
45
45
|
let ehbpModuleOverride;
|
|
46
46
|
// Public API
|
|
47
|
+
async function getHPKEKey(enclaveURL) {
|
|
48
|
+
const { Identity, PROTOCOL } = await getEhbpModule();
|
|
49
|
+
const url = new URL(enclaveURL);
|
|
50
|
+
const keysURL = new URL(PROTOCOL.KEYS_PATH, enclaveURL);
|
|
51
|
+
if (keysURL.protocol !== 'https:') {
|
|
52
|
+
throw new Error(`HTTPS is required for remote key retrieval. Invalid protocol: ${keysURL.protocol}`);
|
|
53
|
+
}
|
|
54
|
+
const response = await fetch(keysURL.toString());
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw new Error(`Failed to get server public key: ${response.status}`);
|
|
57
|
+
}
|
|
58
|
+
const contentType = response.headers.get('content-type');
|
|
59
|
+
if (contentType !== PROTOCOL.KEYS_MEDIA_TYPE) {
|
|
60
|
+
throw new Error(`Invalid content type: ${contentType}`);
|
|
61
|
+
}
|
|
62
|
+
const keysData = new Uint8Array(await response.arrayBuffer());
|
|
63
|
+
const serverIdentity = await Identity.unmarshalPublicConfig(keysData);
|
|
64
|
+
return serverIdentity.getPublicKey();
|
|
65
|
+
}
|
|
47
66
|
function normalizeEncryptedBodyRequestArgs(input, init) {
|
|
48
67
|
if (typeof input === "string") {
|
|
49
68
|
return { url: input, init };
|
|
@@ -68,28 +87,16 @@ async function encryptedBodyRequest(input, hpkePublicKey, init, enclaveURL) {
|
|
|
68
87
|
const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
|
|
69
88
|
const u = new URL(requestUrl);
|
|
70
89
|
const { origin } = u;
|
|
71
|
-
// Determine which origin to fetch the HPKE key from (may be different from request origin)
|
|
72
90
|
const keyOrigin = enclaveURL ? new URL(enclaveURL).origin : origin;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const transport = keyOrigin === origin
|
|
76
|
-
? await getTransportForOrigin(origin)
|
|
77
|
-
: await getTransportForRequestOriginWithKeyFrom(origin, keyOrigin);
|
|
78
|
-
const serverPublicKey = await transport.getServerPublicKeyHex();
|
|
79
|
-
if (serverPublicKey !== hpkePublicKey) {
|
|
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
|
-
}
|
|
90
|
-
throw new Error(`HPKE public key mismatch: expected ${hpkePublicKey}, got ${serverPublicKey}`);
|
|
91
|
+
if (!transport) {
|
|
92
|
+
transport = getTransportForOrigin(origin, keyOrigin);
|
|
91
93
|
}
|
|
92
|
-
|
|
94
|
+
const transportInstance = await transport;
|
|
95
|
+
const transportKeyHash = await transportInstance.getServerPublicKeyHex();
|
|
96
|
+
if (transportKeyHash !== hpkePublicKey) {
|
|
97
|
+
throw new Error(`HPKE public key mismatch. Expected: ${hpkePublicKey}, Got: ${transportKeyHash}`);
|
|
98
|
+
}
|
|
99
|
+
return transportInstance.request(requestUrl, requestInit);
|
|
93
100
|
}
|
|
94
101
|
function createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL) {
|
|
95
102
|
return (async (input, init) => {
|
|
@@ -124,64 +131,21 @@ function getEhbpModule() {
|
|
|
124
131
|
}
|
|
125
132
|
return ehbpModulePromise;
|
|
126
133
|
}
|
|
127
|
-
async function getTransportForOrigin(origin) {
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const isSecure = globalThis.isSecureContext !== false;
|
|
137
|
-
const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
|
|
138
|
-
if (!isSecure || !hasSubtle) {
|
|
139
|
-
const reason = !isSecure
|
|
140
|
-
? 'insecure context (use HTTPS or localhost)'
|
|
141
|
-
: 'missing WebCrypto SubtleCrypto';
|
|
142
|
-
throw new Error(`EHBP requires a secure browser context: ${reason}`);
|
|
143
|
-
}
|
|
134
|
+
async function getTransportForOrigin(origin, keyOrigin) {
|
|
135
|
+
const { Identity, Transport } = await getEhbpModule();
|
|
136
|
+
// Ensure secure browser context
|
|
137
|
+
if (typeof globalThis !== 'undefined') {
|
|
138
|
+
const isSecure = globalThis.isSecureContext !== false;
|
|
139
|
+
const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
|
|
140
|
+
if (!isSecure || !hasSubtle) {
|
|
141
|
+
const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
|
|
142
|
+
throw new Error(`EHBP requires a secure browser context: ${reason}`);
|
|
144
143
|
}
|
|
145
|
-
const clientIdentity = await Identity.generate();
|
|
146
|
-
return createTransport(origin, clientIdentity);
|
|
147
|
-
})().catch((error) => {
|
|
148
|
-
transportCache.delete(origin);
|
|
149
|
-
throw error;
|
|
150
|
-
});
|
|
151
|
-
transportCache.set(origin, transportPromise);
|
|
152
|
-
return transportPromise;
|
|
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
144
|
}
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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;
|
|
145
|
+
const clientIdentity = await Identity.generate();
|
|
146
|
+
const serverPublicKey = await getHPKEKey(keyOrigin);
|
|
147
|
+
const requestHost = new URL(origin).host;
|
|
148
|
+
return new Transport(clientIdentity, requestHost, serverPublicKey);
|
|
185
149
|
}
|
|
186
150
|
// Test utilities
|
|
187
151
|
function __setEhbpModuleForTests(module) {
|
|
@@ -191,6 +155,5 @@ function __setEhbpModuleForTests(module) {
|
|
|
191
155
|
function __resetEhbpModuleStateForTests() {
|
|
192
156
|
ehbpModuleOverride = undefined;
|
|
193
157
|
ehbpModulePromise = null;
|
|
194
|
-
|
|
195
|
-
compositeTransportCache.clear();
|
|
158
|
+
transport = null;
|
|
196
159
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
2
2
|
import { TINFOIL_CONFIG } from "./config";
|
|
3
|
-
import { SecureClient } from "
|
|
3
|
+
import { SecureClient } from "./secure-client";
|
|
4
4
|
export async function createTinfoilAI(apiKey, options = {}) {
|
|
5
5
|
const baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
6
6
|
const enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { isRealBrowser } from "./env";
|
|
2
|
-
|
|
3
|
-
const compositeTransportCache = new Map();
|
|
2
|
+
let transport = null;
|
|
4
3
|
let ehbpModulePromise = null;
|
|
5
4
|
let ehbpModuleOverride;
|
|
6
5
|
// Public API
|
|
6
|
+
export async function getHPKEKey(enclaveURL) {
|
|
7
|
+
const { Identity, PROTOCOL } = await getEhbpModule();
|
|
8
|
+
const url = new URL(enclaveURL);
|
|
9
|
+
const keysURL = new URL(PROTOCOL.KEYS_PATH, enclaveURL);
|
|
10
|
+
if (keysURL.protocol !== 'https:') {
|
|
11
|
+
throw new Error(`HTTPS is required for remote key retrieval. Invalid protocol: ${keysURL.protocol}`);
|
|
12
|
+
}
|
|
13
|
+
const response = await fetch(keysURL.toString());
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error(`Failed to get server public key: ${response.status}`);
|
|
16
|
+
}
|
|
17
|
+
const contentType = response.headers.get('content-type');
|
|
18
|
+
if (contentType !== PROTOCOL.KEYS_MEDIA_TYPE) {
|
|
19
|
+
throw new Error(`Invalid content type: ${contentType}`);
|
|
20
|
+
}
|
|
21
|
+
const keysData = new Uint8Array(await response.arrayBuffer());
|
|
22
|
+
const serverIdentity = await Identity.unmarshalPublicConfig(keysData);
|
|
23
|
+
return serverIdentity.getPublicKey();
|
|
24
|
+
}
|
|
7
25
|
export function normalizeEncryptedBodyRequestArgs(input, init) {
|
|
8
26
|
if (typeof input === "string") {
|
|
9
27
|
return { url: input, init };
|
|
@@ -28,28 +46,16 @@ export async function encryptedBodyRequest(input, hpkePublicKey, init, enclaveUR
|
|
|
28
46
|
const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
|
|
29
47
|
const u = new URL(requestUrl);
|
|
30
48
|
const { origin } = u;
|
|
31
|
-
// Determine which origin to fetch the HPKE key from (may be different from request origin)
|
|
32
49
|
const keyOrigin = enclaveURL ? new URL(enclaveURL).origin : origin;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const transport = keyOrigin === origin
|
|
36
|
-
? await getTransportForOrigin(origin)
|
|
37
|
-
: await getTransportForRequestOriginWithKeyFrom(origin, keyOrigin);
|
|
38
|
-
const serverPublicKey = await transport.getServerPublicKeyHex();
|
|
39
|
-
if (serverPublicKey !== hpkePublicKey) {
|
|
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
|
-
}
|
|
50
|
-
throw new Error(`HPKE public key mismatch: expected ${hpkePublicKey}, got ${serverPublicKey}`);
|
|
50
|
+
if (!transport) {
|
|
51
|
+
transport = getTransportForOrigin(origin, keyOrigin);
|
|
51
52
|
}
|
|
52
|
-
|
|
53
|
+
const transportInstance = await transport;
|
|
54
|
+
const transportKeyHash = await transportInstance.getServerPublicKeyHex();
|
|
55
|
+
if (transportKeyHash !== hpkePublicKey) {
|
|
56
|
+
throw new Error(`HPKE public key mismatch. Expected: ${hpkePublicKey}, Got: ${transportKeyHash}`);
|
|
57
|
+
}
|
|
58
|
+
return transportInstance.request(requestUrl, requestInit);
|
|
53
59
|
}
|
|
54
60
|
export function createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL) {
|
|
55
61
|
return (async (input, init) => {
|
|
@@ -84,64 +90,21 @@ function getEhbpModule() {
|
|
|
84
90
|
}
|
|
85
91
|
return ehbpModulePromise;
|
|
86
92
|
}
|
|
87
|
-
async function getTransportForOrigin(origin) {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const isSecure = globalThis.isSecureContext !== false;
|
|
97
|
-
const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
|
|
98
|
-
if (!isSecure || !hasSubtle) {
|
|
99
|
-
const reason = !isSecure
|
|
100
|
-
? 'insecure context (use HTTPS or localhost)'
|
|
101
|
-
: 'missing WebCrypto SubtleCrypto';
|
|
102
|
-
throw new Error(`EHBP requires a secure browser context: ${reason}`);
|
|
103
|
-
}
|
|
93
|
+
async function getTransportForOrigin(origin, keyOrigin) {
|
|
94
|
+
const { Identity, Transport } = await getEhbpModule();
|
|
95
|
+
// Ensure secure browser context
|
|
96
|
+
if (typeof globalThis !== 'undefined') {
|
|
97
|
+
const isSecure = globalThis.isSecureContext !== false;
|
|
98
|
+
const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
|
|
99
|
+
if (!isSecure || !hasSubtle) {
|
|
100
|
+
const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
|
|
101
|
+
throw new Error(`EHBP requires a secure browser context: ${reason}`);
|
|
104
102
|
}
|
|
105
|
-
const clientIdentity = await Identity.generate();
|
|
106
|
-
return createTransport(origin, clientIdentity);
|
|
107
|
-
})().catch((error) => {
|
|
108
|
-
transportCache.delete(origin);
|
|
109
|
-
throw error;
|
|
110
|
-
});
|
|
111
|
-
transportCache.set(origin, transportPromise);
|
|
112
|
-
return transportPromise;
|
|
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
103
|
}
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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;
|
|
104
|
+
const clientIdentity = await Identity.generate();
|
|
105
|
+
const serverPublicKey = await getHPKEKey(keyOrigin);
|
|
106
|
+
const requestHost = new URL(origin).host;
|
|
107
|
+
return new Transport(clientIdentity, requestHost, serverPublicKey);
|
|
145
108
|
}
|
|
146
109
|
// Test utilities
|
|
147
110
|
export function __setEhbpModuleForTests(module) {
|
|
@@ -151,6 +114,5 @@ export function __setEhbpModuleForTests(module) {
|
|
|
151
114
|
export function __resetEhbpModuleStateForTests() {
|
|
152
115
|
ehbpModuleOverride = undefined;
|
|
153
116
|
ehbpModulePromise = null;
|
|
154
|
-
|
|
155
|
-
compositeTransportCache.clear();
|
|
117
|
+
transport = null;
|
|
156
118
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { TinfoilAI } from "./tinfoilai";
|
|
2
2
|
export { TinfoilAI as default } from "./tinfoilai";
|
|
3
3
|
export * from "./verifier";
|
|
4
|
-
export * from "./verification-runner";
|
|
5
4
|
export * from "./ai-sdk-provider";
|
|
6
5
|
export * from "./config";
|
|
7
|
-
export { SecureClient } from "
|
|
6
|
+
export { SecureClient } from "./secure-client";
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
export { TinfoilAI } from "./tinfoilai";
|
|
3
3
|
export { TinfoilAI as default } from "./tinfoilai";
|
|
4
4
|
export * from "./verifier";
|
|
5
|
-
export * from "./verification-runner";
|
|
6
5
|
export * from "./ai-sdk-provider";
|
|
7
6
|
export * from "./config";
|
|
8
|
-
export { SecureClient } from "
|
|
7
|
+
export { SecureClient } from "./secure-client";
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export { TinfoilAI } from "./tinfoilai";
|
|
2
2
|
export { TinfoilAI as default } from "./tinfoilai";
|
|
3
3
|
export * from "./verifier";
|
|
4
|
-
export * from "./verification-runner";
|
|
5
4
|
export * from "./ai-sdk-provider";
|
|
6
5
|
export * from "./config";
|
|
7
|
-
export
|
|
6
|
+
export { SecureClient } from "./secure-client";
|
|
8
7
|
export { type Uploadable, toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
|
package/dist/esm/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
// Re-export the TinfoilAI class
|
|
2
2
|
export { TinfoilAI } from "./tinfoilai";
|
|
3
3
|
export { TinfoilAI as default } from "./tinfoilai";
|
|
4
|
-
// Export verifier
|
|
4
|
+
// Export verifier
|
|
5
5
|
export * from "./verifier";
|
|
6
|
-
export * from "./verification-runner";
|
|
7
6
|
export * from "./ai-sdk-provider";
|
|
8
7
|
export * from "./config";
|
|
9
|
-
export
|
|
8
|
+
export { SecureClient } from "./secure-client";
|
|
10
9
|
// Re-export OpenAI utility types and classes that users might need
|
|
11
10
|
// Using public exports from the main OpenAI package instead of deep imports
|
|
12
11
|
export { toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function createPinnedTlsFetch(expectedFingerprintHex: string): typeof fetch;
|
|
1
|
+
export declare function createPinnedTlsFetch(baseURL: string, expectedFingerprintHex: string): typeof fetch;
|
|
@@ -2,15 +2,15 @@ import https from "https";
|
|
|
2
2
|
import { checkServerIdentity as tlsCheckServerIdentity } from "tls";
|
|
3
3
|
import { X509Certificate, createHash } from "crypto";
|
|
4
4
|
import { Readable } from "stream";
|
|
5
|
-
export function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
5
|
+
export function createPinnedTlsFetch(baseURL, expectedFingerprintHex) {
|
|
6
6
|
return (async (input, init) => {
|
|
7
|
-
// Normalize URL
|
|
7
|
+
// Normalize URL with base URL support
|
|
8
8
|
const makeURL = (value) => {
|
|
9
9
|
if (typeof value === "string")
|
|
10
|
-
return new URL(value);
|
|
10
|
+
return new URL(value, baseURL);
|
|
11
11
|
if (value instanceof URL)
|
|
12
12
|
return value;
|
|
13
|
-
return new URL(value.url);
|
|
13
|
+
return new URL(value.url, baseURL);
|
|
14
14
|
};
|
|
15
15
|
const url = makeURL(input);
|
|
16
16
|
if (url.protocol !== "https:") {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Verifier } from "./verifier";
|
|
2
|
-
import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
|
|
3
|
-
import { createPinnedTlsFetch } from "./pinned-tls-fetch";
|
|
4
2
|
import { TINFOIL_CONFIG } from "./config";
|
|
5
|
-
import {
|
|
3
|
+
import { createSecureFetch } from "tinfoil/secure-fetch";
|
|
6
4
|
export class SecureClient {
|
|
7
5
|
constructor(options = {}) {
|
|
8
6
|
this.initPromise = null;
|
|
@@ -31,24 +29,7 @@ export class SecureClient {
|
|
|
31
29
|
this.verificationDocument = doc;
|
|
32
30
|
// Extract keys from the verification document
|
|
33
31
|
const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
|
|
34
|
-
|
|
35
|
-
if (hpkePublicKey) {
|
|
36
|
-
// HPKE available: use encrypted body fetch
|
|
37
|
-
fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.enclaveURL);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
// HPKE not available: check if we're in a browser
|
|
41
|
-
if (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
|
-
this._fetch = fetchFunction;
|
|
32
|
+
this._fetch = createSecureFetch(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
|
|
52
33
|
}
|
|
53
34
|
async getVerificationDocument() {
|
|
54
35
|
if (!this.initPromise) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createSecureFetch(baseURL: string, enclaveURL?: string, hpkePublicKey?: string, tlsPublicKeyFingerprint?: string): typeof fetch;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
|
|
2
|
+
export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
|
|
3
|
+
if (hpkePublicKey) {
|
|
4
|
+
return createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
|
|
5
|
+
}
|
|
6
|
+
else {
|
|
7
|
+
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
8
|
+
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createSecureFetch(baseURL: string, enclaveURL?: string, hpkePublicKey?: string, tlsPublicKeyFingerprint?: string): typeof fetch;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
|
|
2
|
+
import { createPinnedTlsFetch } from "./pinned-tls-fetch";
|
|
3
|
+
import { isRealBrowser } from "./env";
|
|
4
|
+
export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
|
|
5
|
+
let fetchFunction;
|
|
6
|
+
if (hpkePublicKey) {
|
|
7
|
+
fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
// HPKE not available: check if we're in a browser
|
|
11
|
+
if (isRealBrowser()) {
|
|
12
|
+
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
13
|
+
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
14
|
+
}
|
|
15
|
+
// Node.js environment: fall back to TLS-only verification using pinned TLS fetch
|
|
16
|
+
if (!tlsPublicKeyFingerprint) {
|
|
17
|
+
throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
18
|
+
}
|
|
19
|
+
fetchFunction = createPinnedTlsFetch(baseURL, tlsPublicKeyFingerprint);
|
|
20
|
+
}
|
|
21
|
+
return fetchFunction;
|
|
22
|
+
}
|
package/dist/esm/tinfoilai.js
CHANGED