tinfoil 0.9.0 → 0.10.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.
@@ -4,13 +4,14 @@ exports.SecureClient = void 0;
4
4
  const verifier_1 = require("./verifier");
5
5
  const config_1 = require("./config");
6
6
  const secure_fetch_1 = require("tinfoil/secure-fetch");
7
+ const router_1 = require("./router");
7
8
  class SecureClient {
8
9
  constructor(options = {}) {
9
10
  this.initPromise = null;
10
11
  this.verificationDocument = null;
11
12
  this._fetch = null;
12
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
13
- this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
13
+ this.baseURL = options.baseURL;
14
+ this.enclaveURL = options.enclaveURL;
14
15
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
15
16
  }
16
17
  async ready() {
@@ -20,34 +21,136 @@ class SecureClient {
20
21
  return this.initPromise;
21
22
  }
22
23
  async initSecureClient() {
24
+ // Only fetch router if neither baseURL nor enclaveURL is provided
25
+ if (!this.baseURL && !this.enclaveURL) {
26
+ const routerAddress = await (0, router_1.fetchRouter)();
27
+ this.enclaveURL = `https://${routerAddress}`;
28
+ this.baseURL = `https://${routerAddress}/v1/`;
29
+ }
30
+ // Ensure both baseURL and enclaveURL are initialized
31
+ if (!this.baseURL) {
32
+ if (this.enclaveURL) {
33
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
34
+ const enclaveUrl = new URL(this.enclaveURL);
35
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
36
+ }
37
+ else {
38
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
39
+ }
40
+ }
41
+ if (!this.enclaveURL) {
42
+ if (this.baseURL) {
43
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
44
+ const baseUrl = new URL(this.baseURL);
45
+ this.enclaveURL = baseUrl.origin;
46
+ }
47
+ else {
48
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
49
+ }
50
+ }
23
51
  const verifier = new verifier_1.Verifier({
24
52
  serverURL: this.enclaveURL,
25
53
  configRepo: this.configRepo,
26
54
  });
27
- await verifier.verify();
28
- const doc = verifier.getVerificationDocument();
29
- if (!doc) {
30
- throw new Error("Verification document not available after successful verification");
31
- }
32
- this.verificationDocument = doc;
33
- // Extract keys from the verification document
34
- const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
35
- this._fetch = (0, secure_fetch_1.createSecureFetch)(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
55
+ try {
56
+ await verifier.verify();
57
+ const doc = verifier.getVerificationDocument();
58
+ if (!doc) {
59
+ throw new Error("Verification document not available after successful verification");
60
+ }
61
+ this.verificationDocument = doc;
62
+ // Extract keys from the verification document
63
+ const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
64
+ try {
65
+ this._fetch = (0, secure_fetch_1.createSecureFetch)(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
66
+ }
67
+ catch (transportError) {
68
+ this.verificationDocument.steps.createTransport = {
69
+ status: 'failed',
70
+ error: transportError.message
71
+ };
72
+ this.verificationDocument.securityVerified = false;
73
+ throw transportError;
74
+ }
75
+ }
76
+ catch (error) {
77
+ const doc = verifier.getVerificationDocument();
78
+ if (doc) {
79
+ this.verificationDocument = doc;
80
+ }
81
+ else {
82
+ this.verificationDocument = {
83
+ configRepo: this.configRepo,
84
+ enclaveHost: new URL(this.enclaveURL).hostname,
85
+ releaseDigest: '',
86
+ codeMeasurement: { type: '', registers: [] },
87
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
88
+ securityVerified: false,
89
+ steps: {
90
+ fetchDigest: { status: 'pending' },
91
+ verifyCode: { status: 'pending' },
92
+ verifyEnclave: { status: 'pending' },
93
+ compareMeasurements: { status: 'pending' },
94
+ otherError: { status: 'failed', error: error.message }
95
+ }
96
+ };
97
+ }
98
+ throw error;
99
+ }
36
100
  }
37
101
  async getVerificationDocument() {
38
102
  if (!this.initPromise) {
39
103
  await this.ready();
40
104
  }
41
- await this.initPromise;
105
+ await this.initPromise.catch(() => { });
42
106
  if (!this.verificationDocument) {
43
107
  throw new Error("Verification document unavailable: client not verified yet");
44
108
  }
45
109
  return this.verificationDocument;
46
110
  }
111
+ getBaseURL() {
112
+ return this.baseURL;
113
+ }
47
114
  get fetch() {
48
115
  return async (input, init) => {
49
116
  await this.ready();
50
- return this._fetch(input, init);
117
+ try {
118
+ return await this._fetch(input, init);
119
+ }
120
+ catch (error) {
121
+ if (this.verificationDocument) {
122
+ const errorMessage = error.message;
123
+ if (errorMessage.includes('HPKE public key mismatch')) {
124
+ this.verificationDocument.steps.verifyHPKEKey = {
125
+ status: 'failed',
126
+ error: errorMessage
127
+ };
128
+ this.verificationDocument.securityVerified = false;
129
+ }
130
+ else if (errorMessage.includes('Transport initialization failed') || errorMessage.includes('Request initialization failed')) {
131
+ this.verificationDocument.steps.createTransport = {
132
+ status: 'failed',
133
+ error: errorMessage
134
+ };
135
+ this.verificationDocument.securityVerified = false;
136
+ }
137
+ else if (errorMessage.includes('Failed to get HPKE key')) {
138
+ this.verificationDocument.steps.verifyHPKEKey = {
139
+ status: 'failed',
140
+ error: errorMessage
141
+ };
142
+ this.verificationDocument.securityVerified = false;
143
+ }
144
+ else {
145
+ this.verificationDocument.steps.otherError = {
146
+ status: 'failed',
147
+ error: errorMessage
148
+ };
149
+ this.verificationDocument.securityVerified = false;
150
+ }
151
+ }
152
+ throw error;
153
+ }
51
154
  };
52
155
  }
53
156
  }
package/dist/tinfoilai.js CHANGED
@@ -42,8 +42,8 @@ class TinfoilAI {
42
42
  openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
43
43
  }
44
44
  this.apiKey = openAIOptions.apiKey;
45
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
46
- this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
45
+ this.baseURL = options.baseURL;
46
+ this.enclaveURL = options.enclaveURL;
47
47
  this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
48
48
  this.secureClient = new secure_client_1.SecureClient({
49
49
  baseURL: this.baseURL,
@@ -69,9 +69,11 @@ class TinfoilAI {
69
69
  if (!this.verificationDocument) {
70
70
  throw new Error("Verification document not available after successful verification");
71
71
  }
72
+ // Use the provided baseURL, or get it from SecureClient after initialization
73
+ const baseURL = this.baseURL || this.secureClient.getBaseURL();
72
74
  const clientOptions = {
73
75
  ...options,
74
- baseURL: this.baseURL,
76
+ baseURL: baseURL,
75
77
  fetch: this.secureClient.fetch,
76
78
  };
77
79
  if ((0, env_1.isRealBrowser)() || options.dangerouslyAllowBrowser === true) {
@@ -0,0 +1,18 @@
1
+ interface UnverifiedClientOptions {
2
+ baseURL?: string;
3
+ enclaveURL?: string;
4
+ configRepo?: string;
5
+ }
6
+ export declare class UnverifiedClient {
7
+ private initPromise;
8
+ private _fetch;
9
+ private baseURL?;
10
+ private enclaveURL?;
11
+ private readonly configRepo;
12
+ constructor(options?: UnverifiedClientOptions);
13
+ ready(): Promise<void>;
14
+ private initUnverifiedClient;
15
+ getVerificationDocument(): Promise<void>;
16
+ get fetch(): typeof fetch;
17
+ }
18
+ export {};
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UnverifiedClient = void 0;
4
+ const config_1 = require("./config");
5
+ const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
6
+ const router_1 = require("./router");
7
+ class UnverifiedClient {
8
+ constructor(options = {}) {
9
+ this.initPromise = null;
10
+ this._fetch = null;
11
+ this.baseURL = options.baseURL;
12
+ this.enclaveURL = options.enclaveURL;
13
+ this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
14
+ }
15
+ async ready() {
16
+ if (!this.initPromise) {
17
+ this.initPromise = this.initUnverifiedClient();
18
+ }
19
+ return this.initPromise;
20
+ }
21
+ async initUnverifiedClient() {
22
+ // Only fetch router if neither baseURL nor enclaveURL is provided
23
+ if (!this.baseURL && !this.enclaveURL) {
24
+ const routerAddress = await (0, router_1.fetchRouter)();
25
+ this.enclaveURL = `https://${routerAddress}`;
26
+ this.baseURL = `https://${routerAddress}/v1/`;
27
+ }
28
+ // Ensure both baseURL and enclaveURL are initialized
29
+ if (!this.baseURL) {
30
+ if (this.enclaveURL) {
31
+ // If enclaveURL is provided but baseURL is not, derive baseURL from enclaveURL
32
+ const enclaveUrl = new URL(this.enclaveURL);
33
+ this.baseURL = `${enclaveUrl.origin}/v1/`;
34
+ }
35
+ else {
36
+ throw new Error("Unable to determine baseURL: neither baseURL nor enclaveURL provided");
37
+ }
38
+ }
39
+ if (!this.enclaveURL) {
40
+ if (this.baseURL) {
41
+ // If baseURL is provided but enclaveURL is not, derive enclaveURL from baseURL
42
+ const baseUrl = new URL(this.baseURL);
43
+ this.enclaveURL = baseUrl.origin;
44
+ }
45
+ else {
46
+ throw new Error("Unable to determine enclaveURL: neither baseURL nor enclaveURL provided");
47
+ }
48
+ }
49
+ this._fetch = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, undefined, this.enclaveURL);
50
+ }
51
+ async getVerificationDocument() {
52
+ if (!this.initPromise) {
53
+ await this.ready();
54
+ }
55
+ await this.initPromise;
56
+ throw new Error("Verification document unavailable: this version of the client is unverified");
57
+ }
58
+ get fetch() {
59
+ return async (input, init) => {
60
+ await this.ready();
61
+ return this._fetch(input, init);
62
+ };
63
+ }
64
+ }
65
+ exports.UnverifiedClient = UnverifiedClient;
@@ -37,6 +37,9 @@ export interface VerificationDocument {
37
37
  verifyCode: VerificationStepState;
38
38
  verifyEnclave: VerificationStepState;
39
39
  compareMeasurements: VerificationStepState;
40
+ createTransport?: VerificationStepState;
41
+ verifyHPKEKey?: VerificationStepState;
42
+ otherError?: VerificationStepState;
40
43
  };
41
44
  }
42
45
  export interface MeasurementComparisonResult {
@@ -84,7 +87,7 @@ export declare class Verifier {
84
87
  private static executeWithWasm;
85
88
  /**
86
89
  * Fetch the latest release digest from GitHub
87
- * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
90
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-model-router")
88
91
  * @returns The digest hash
89
92
  */
90
93
  fetchLatestDigest(configRepo?: string): Promise<string>;
package/dist/verifier.js CHANGED
@@ -163,7 +163,8 @@ function compareMeasurementsError(codeMeasurement, runtimeMeasurement) {
163
163
  }
164
164
  if (codeMeasurement.type === PLATFORM_TYPES.SNP_TDX_MULTI_PLATFORM_V1) {
165
165
  switch (runtimeMeasurement.type) {
166
- case PLATFORM_TYPES.TDX_GUEST_V1: {
166
+ case PLATFORM_TYPES.TDX_GUEST_V1:
167
+ case PLATFORM_TYPES.TDX_GUEST_V2: {
167
168
  if (codeMeasurement.registers.length < 3 || runtimeMeasurement.registers.length < 4) {
168
169
  return new Error(MEASUREMENT_ERROR_MESSAGES.FEW_REGISTERS);
169
170
  }
@@ -233,8 +234,10 @@ function compareMeasurements(codeMeasurement, runtimeMeasurement) {
233
234
  */
234
235
  class Verifier {
235
236
  constructor(options) {
236
- const serverURL = options?.serverURL ?? config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
237
- this.serverURL = new URL(serverURL).hostname;
237
+ if (!options?.serverURL) {
238
+ throw new Error("serverURL is required for Verifier");
239
+ }
240
+ this.serverURL = new URL(options.serverURL).hostname;
238
241
  this.configRepo = options?.configRepo ?? config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
239
242
  }
240
243
  /**
@@ -295,7 +298,7 @@ class Verifier {
295
298
  }
296
299
  /**
297
300
  * Fetch the latest release digest from GitHub
298
- * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-inference-proxy")
301
+ * @param configRepo - Repository name (e.g., "tinfoilsh/confidential-model-router")
299
302
  * @returns The digest hash
300
303
  */
301
304
  async fetchLatestDigest(configRepo) {
@@ -478,6 +481,15 @@ class Verifier {
478
481
  }
479
482
  catch (error) {
480
483
  steps.fetchDigest = { status: 'failed', error: error.message };
484
+ this.lastVerificationDocument = {
485
+ configRepo: this.configRepo,
486
+ enclaveHost: this.serverURL,
487
+ releaseDigest: '',
488
+ codeMeasurement: { type: '', registers: [] },
489
+ enclaveMeasurement: { measurement: { type: '', registers: [] } },
490
+ securityVerified: false,
491
+ steps,
492
+ };
481
493
  throw error;
482
494
  }
483
495
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Tinfoil secure OpenAI client wrapper",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -81,7 +81,7 @@
81
81
  "@ai-sdk/openai-compatible": "^1.0.10",
82
82
  "ai": "^5.0.19",
83
83
  "dotenv": "^17.2.1",
84
- "ehbp": "^0.0.2",
84
+ "ehbp": "^0.0.4",
85
85
  "openai": "^5.13.1",
86
86
  "ts-node": "^10.9.2"
87
87
  },