tinfoil 0.8.3 → 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.
Files changed (37) hide show
  1. package/dist/ai-sdk-provider.d.ts +0 -10
  2. package/dist/ai-sdk-provider.js +8 -150
  3. package/dist/client.browser.d.ts +1 -0
  4. package/dist/client.browser.js +12 -27
  5. package/dist/client.d.ts +1 -0
  6. package/dist/client.js +12 -152
  7. package/dist/esm/ai-sdk-provider.d.ts +0 -10
  8. package/dist/esm/ai-sdk-provider.js +8 -147
  9. package/dist/esm/client.browser.d.ts +1 -0
  10. package/dist/esm/client.browser.js +12 -27
  11. package/dist/esm/client.d.ts +1 -0
  12. package/dist/esm/client.js +12 -152
  13. package/dist/esm/index.browser.d.ts +2 -1
  14. package/dist/esm/index.browser.js +2 -1
  15. package/dist/esm/index.d.ts +1 -0
  16. package/dist/esm/index.js +1 -0
  17. package/dist/esm/pinned-tls-fetch.d.ts +1 -0
  18. package/dist/esm/pinned-tls-fetch.js +110 -0
  19. package/dist/esm/secure-client.browser.d.ts +20 -0
  20. package/dist/esm/secure-client.browser.js +57 -0
  21. package/dist/esm/secure-client.d.ts +20 -0
  22. package/dist/esm/secure-client.js +68 -0
  23. package/dist/index.browser.d.ts +2 -1
  24. package/dist/index.browser.js +2 -1
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +1 -0
  27. package/dist/pinned-tls-fetch.d.ts +1 -0
  28. package/dist/pinned-tls-fetch.js +116 -0
  29. package/dist/secure-client.browser.d.ts +20 -0
  30. package/dist/secure-client.browser.js +61 -0
  31. package/dist/secure-client.d.ts +20 -0
  32. package/dist/secure-client.js +72 -0
  33. package/package.json +7 -2
  34. package/dist/ai-sdk-provider.browser.d.ts +0 -7
  35. package/dist/ai-sdk-provider.browser.js +0 -30
  36. package/dist/esm/ai-sdk-provider.browser.d.ts +0 -7
  37. package/dist/esm/ai-sdk-provider.browser.js +0 -27
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.8.3",
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,7 +0,0 @@
1
- interface CreateTinfoilAIOptions {
2
- baseURL?: string;
3
- hpkeKeyURL?: string;
4
- configRepo?: string;
5
- }
6
- export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
7
- export {};
@@ -1,30 +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 hpkeKeyURL = options.hpkeKeyURL || config_1.TINFOIL_CONFIG.HPKE_KEY_URL;
12
- const configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
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, hpkeKeyURL);
24
- return (0, openai_compatible_1.createOpenAICompatible)({
25
- name: "tinfoil",
26
- baseURL: baseURL.replace(/\/$/, ""),
27
- apiKey: apiKey,
28
- fetch: fetchFunction,
29
- });
30
- }
@@ -1,7 +0,0 @@
1
- interface CreateTinfoilAIOptions {
2
- baseURL?: string;
3
- hpkeKeyURL?: string;
4
- configRepo?: string;
5
- }
6
- export declare function createTinfoilAI(apiKey: string, options?: CreateTinfoilAIOptions): Promise<import("@ai-sdk/openai-compatible").OpenAICompatibleProvider<string, string, string, string>>;
7
- export {};
@@ -1,27 +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 hpkeKeyURL = options.hpkeKeyURL || TINFOIL_CONFIG.HPKE_KEY_URL;
9
- const configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
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, hpkeKeyURL);
21
- return createOpenAICompatible({
22
- name: "tinfoil",
23
- baseURL: baseURL.replace(/\/$/, ""),
24
- apiKey: apiKey,
25
- fetch: fetchFunction,
26
- });
27
- }