pendra 0.23.0 → 0.24.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 +54 -11
- package/dist/index.d.mts +13 -4
- package/dist/index.d.ts +13 -4
- package/dist/index.js +141 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +141 -37
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Official TypeScript/JavaScript SDK for the [Pendra](https://pendra.ai) LLM inference API. Mirrors the OpenAI SDK interface for easy migration.
|
|
4
4
|
|
|
5
|
-
**Requires Node.js
|
|
5
|
+
**Requires Node.js 20+** (uses native `fetch`). The only runtime dependencies are
|
|
6
|
+
`@noble/ciphers`, `@noble/curves` and `@noble/hashes`, used for private inference.
|
|
6
7
|
|
|
7
8
|
## Installation
|
|
8
9
|
|
|
@@ -209,24 +210,66 @@ returns the same result. If the workers serving the model are too old to answer
|
|
|
209
210
|
encrypted speech, the request fails with a 400 `speech_encryption_unsupported`
|
|
210
211
|
error until you update them.
|
|
211
212
|
|
|
213
|
+
## Private inference
|
|
214
|
+
|
|
215
|
+
Private inference encrypts each request end to end, between your code and your
|
|
216
|
+
own workers. Pendra passes it on but can't read it. It is available on the Pro
|
|
217
|
+
and Enterprise plans. Turn it on when you create the client:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
const client = new Pendra({
|
|
221
|
+
apiKey: 'pdr_sk_...',
|
|
222
|
+
privateInference: true,
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
To turn it on without changing code, set `PENDRA_PRIVATE_INFERENCE=1`. Your
|
|
227
|
+
calls stay the same: chat completions, embeddings, reranking, moderations, image
|
|
228
|
+
generation, audio transcription and text to speech are all encrypted before they
|
|
229
|
+
leave your machine, and the client decrypts the replies.
|
|
230
|
+
|
|
231
|
+
By default the client trusts every key the API returns for your organisation;
|
|
232
|
+
nothing is pinned. To encrypt only to a key you have checked, pin its
|
|
233
|
+
fingerprint, copied from the
|
|
234
|
+
[Encryption page](https://console.pendra.ai/encryption) in the console:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
const client = new Pendra({
|
|
238
|
+
apiKey: 'pdr_sk_...',
|
|
239
|
+
// The full 64-character fingerprint. Separate several with commas.
|
|
240
|
+
privateInference: { pinnedFingerprint: '<fingerprint>' },
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
With private inference on, you can also set the pin in
|
|
245
|
+
`PENDRA_PINNED_ORG_FINGERPRINT`; a `pinnedFingerprint` you pass takes priority.
|
|
246
|
+
If none of your organisation's keys match the pin, the request throws a
|
|
247
|
+
`PrivateInferenceError` and is not sent. Browser bundles must pin, because they
|
|
248
|
+
have nowhere to record trusted keys.
|
|
249
|
+
|
|
250
|
+
Without a pin, the fingerprints the client has trusted are recorded in
|
|
251
|
+
`~/.pendra/known_org_keys.json`. Set `PENDRA_TOFU_PATH` to use another file, e.g.
|
|
252
|
+
under `/tmp` on a read-only filesystem. If the file can't be written, requests
|
|
253
|
+
still succeed and a warning is logged.
|
|
254
|
+
|
|
255
|
+
See [End-to-end encryption](https://pendra.ai/docs/capabilities/encryption/) for
|
|
256
|
+
what Pendra can and can't see, how to check a key is yours, and when pinning is
|
|
257
|
+
a good idea.
|
|
258
|
+
|
|
212
259
|
## Configuration
|
|
213
260
|
|
|
214
|
-
| Option
|
|
215
|
-
|
|
|
216
|
-
| `apiKey`
|
|
217
|
-
| `baseURL`
|
|
218
|
-
| `timeout`
|
|
261
|
+
| Option | Env var | Default |
|
|
262
|
+
| ------------------ | -------------------------- | ----------------------- |
|
|
263
|
+
| `apiKey` | `PENDRA_API_KEY` | — |
|
|
264
|
+
| `baseURL` | — | `https://api.pendra.ai` |
|
|
265
|
+
| `timeout` | — | `120000` (ms) |
|
|
266
|
+
| `privateInference` | `PENDRA_PRIVATE_INFERENCE` | off |
|
|
219
267
|
|
|
220
268
|
`timeout` covers waiting for the response to start and then any gap in the
|
|
221
269
|
data, not the length of the whole response, so long generations are not cut
|
|
222
270
|
off, streamed or not. Breaking out of a stream's `for await` loop early
|
|
223
271
|
cancels the request.
|
|
224
272
|
|
|
225
|
-
With private inference on, the key fingerprints the client has trusted are
|
|
226
|
-
recorded in `~/.pendra/known_org_keys.json`; set `PENDRA_TOFU_PATH` to use
|
|
227
|
-
another file, e.g. under `/tmp` on a read-only filesystem. If the file can't be
|
|
228
|
-
written, requests still succeed and a warning is logged.
|
|
229
|
-
|
|
230
273
|
## Migrating from OpenAI
|
|
231
274
|
|
|
232
275
|
```diff
|
package/dist/index.d.mts
CHANGED
|
@@ -852,6 +852,9 @@ declare class PrivateInferenceError extends Error {
|
|
|
852
852
|
interface FetchedKey {
|
|
853
853
|
public_key: string;
|
|
854
854
|
fingerprint?: string;
|
|
855
|
+
/** The key's X-Wing (post-quantum hybrid) companion, when its workers have one. */
|
|
856
|
+
pq_public_key?: string | null;
|
|
857
|
+
pq_fingerprint?: string | null;
|
|
855
858
|
}
|
|
856
859
|
interface EngineOptions {
|
|
857
860
|
baseUrl: string;
|
|
@@ -890,11 +893,17 @@ declare class PrivateInferenceEngine {
|
|
|
890
893
|
get isPinned(): boolean;
|
|
891
894
|
/**
|
|
892
895
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
893
|
-
* allowlist (or auto-trust TOFU), cache the
|
|
896
|
+
* allowlist (or auto-trust TOFU), cache the selected public keys, and return
|
|
894
897
|
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
895
|
-
* so any worker holding any of them can serve the request.
|
|
896
|
-
*
|
|
897
|
-
*
|
|
898
|
+
* so any worker holding any of them can serve the request.
|
|
899
|
+
*
|
|
900
|
+
* A key may carry a post-quantum X-Wing companion (`pq_public_key`). Each key
|
|
901
|
+
* contributes its companion when it has a valid one the client may use, else
|
|
902
|
+
* its classical key; and if any selected key is a companion, only companions
|
|
903
|
+
* are sealed to (see `hybridOnlyIfAny`).
|
|
904
|
+
*
|
|
905
|
+
* Throws PrivateInferenceError on absence, a malformed key, or (when pinned)
|
|
906
|
+
* an empty intersection with the pin set.
|
|
898
907
|
*/
|
|
899
908
|
resolveOrgKey(fetchedKeys: FetchedKey[]): Promise<Uint8Array[]>;
|
|
900
909
|
private tofuTrust;
|
package/dist/index.d.ts
CHANGED
|
@@ -852,6 +852,9 @@ declare class PrivateInferenceError extends Error {
|
|
|
852
852
|
interface FetchedKey {
|
|
853
853
|
public_key: string;
|
|
854
854
|
fingerprint?: string;
|
|
855
|
+
/** The key's X-Wing (post-quantum hybrid) companion, when its workers have one. */
|
|
856
|
+
pq_public_key?: string | null;
|
|
857
|
+
pq_fingerprint?: string | null;
|
|
855
858
|
}
|
|
856
859
|
interface EngineOptions {
|
|
857
860
|
baseUrl: string;
|
|
@@ -890,11 +893,17 @@ declare class PrivateInferenceEngine {
|
|
|
890
893
|
get isPinned(): boolean;
|
|
891
894
|
/**
|
|
892
895
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
893
|
-
* allowlist (or auto-trust TOFU), cache the
|
|
896
|
+
* allowlist (or auto-trust TOFU), cache the selected public keys, and return
|
|
894
897
|
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
895
|
-
* so any worker holding any of them can serve the request.
|
|
896
|
-
*
|
|
897
|
-
*
|
|
898
|
+
* so any worker holding any of them can serve the request.
|
|
899
|
+
*
|
|
900
|
+
* A key may carry a post-quantum X-Wing companion (`pq_public_key`). Each key
|
|
901
|
+
* contributes its companion when it has a valid one the client may use, else
|
|
902
|
+
* its classical key; and if any selected key is a companion, only companions
|
|
903
|
+
* are sealed to (see `hybridOnlyIfAny`).
|
|
904
|
+
*
|
|
905
|
+
* Throws PrivateInferenceError on absence, a malformed key, or (when pinned)
|
|
906
|
+
* an empty intersection with the pin set.
|
|
898
907
|
*/
|
|
899
908
|
resolveOrgKey(fetchedKeys: FetchedKey[]): Promise<Uint8Array[]>;
|
|
900
909
|
private tofuTrust;
|
package/dist/index.js
CHANGED
|
@@ -206,10 +206,14 @@ var import_hkdf2 = require("@noble/hashes/hkdf.js");
|
|
|
206
206
|
var import_ed25519 = require("@noble/curves/ed25519.js");
|
|
207
207
|
var import_chacha = require("@noble/ciphers/chacha.js");
|
|
208
208
|
var import_sha2 = require("@noble/hashes/sha2.js");
|
|
209
|
+
var import_sha3 = require("@noble/hashes/sha3.js");
|
|
210
|
+
var import_ml_kem = require("@noble/post-quantum/ml-kem.js");
|
|
209
211
|
var import_hmac = require("@noble/hashes/hmac.js");
|
|
210
212
|
var import_hkdf = require("@noble/hashes/hkdf.js");
|
|
211
213
|
var import_utils = require("@noble/hashes/utils.js");
|
|
212
|
-
var
|
|
214
|
+
var KEM_DHKEM_X25519 = 32;
|
|
215
|
+
var KEM_XWING = 25722;
|
|
216
|
+
var KEM_ID = KEM_DHKEM_X25519;
|
|
213
217
|
var KDF_ID = 1;
|
|
214
218
|
var AEAD_ID = 3;
|
|
215
219
|
var NH = 32;
|
|
@@ -239,9 +243,19 @@ function concat(...parts) {
|
|
|
239
243
|
}
|
|
240
244
|
return out;
|
|
241
245
|
}
|
|
242
|
-
|
|
246
|
+
function suiteId(kemId) {
|
|
247
|
+
return concat(bytes("HPKE"), i2osp(kemId, 2), i2osp(KDF_ID, 2), i2osp(AEAD_ID, 2));
|
|
248
|
+
}
|
|
249
|
+
var SUITE_ID = suiteId(KEM_DHKEM_X25519);
|
|
250
|
+
var XWING_SUITE_ID = suiteId(KEM_XWING);
|
|
243
251
|
var KEM_SUITE_ID = concat(bytes("KEM"), i2osp(KEM_ID, 2));
|
|
244
252
|
var HPKE_VERSION = bytes("HPKE-v1");
|
|
253
|
+
var X25519_PUBLIC_KEY_SIZE = 32;
|
|
254
|
+
var MLKEM768_EK_SIZE = 1184;
|
|
255
|
+
var MLKEM768_CT_SIZE = 1088;
|
|
256
|
+
var XWING_PUBLIC_KEY_SIZE = MLKEM768_EK_SIZE + 32;
|
|
257
|
+
var XWING_ENC_SIZE = MLKEM768_CT_SIZE + 32;
|
|
258
|
+
var XWING_LABEL = bytes("\\.//^\\");
|
|
245
259
|
function hkdfExtract(salt, ikm) {
|
|
246
260
|
const effectiveSalt = salt.length === 0 ? new Uint8Array(NH) : salt;
|
|
247
261
|
return (0, import_hkdf.extract)(import_sha2.sha256, ikm, effectiveSalt);
|
|
@@ -249,11 +263,11 @@ function hkdfExtract(salt, ikm) {
|
|
|
249
263
|
function hkdfExpand(prk, info, length) {
|
|
250
264
|
return (0, import_hkdf.expand)(import_sha2.sha256, prk, info, length);
|
|
251
265
|
}
|
|
252
|
-
function labeledExtract(salt, label, ikm,
|
|
253
|
-
return hkdfExtract(salt, concat(HPKE_VERSION,
|
|
266
|
+
function labeledExtract(salt, label, ikm, suiteId2) {
|
|
267
|
+
return hkdfExtract(salt, concat(HPKE_VERSION, suiteId2, label, ikm));
|
|
254
268
|
}
|
|
255
|
-
function labeledExpand(prk, label, info, length,
|
|
256
|
-
const labeledInfo = concat(i2osp(length, 2), HPKE_VERSION,
|
|
269
|
+
function labeledExpand(prk, label, info, length, suiteId2) {
|
|
270
|
+
const labeledInfo = concat(i2osp(length, 2), HPKE_VERSION, suiteId2, label, info);
|
|
257
271
|
return hkdfExpand(prk, labeledInfo, length);
|
|
258
272
|
}
|
|
259
273
|
function extractAndExpand(dh, kemContext) {
|
|
@@ -267,12 +281,34 @@ function encap(pkR, skE) {
|
|
|
267
281
|
const kemContext = concat(enc, pkR);
|
|
268
282
|
return [extractAndExpand(dh, kemContext), enc];
|
|
269
283
|
}
|
|
284
|
+
function xwingCombine(ssPq, ssT, ctT, pkT) {
|
|
285
|
+
return (0, import_sha3.sha3_256)(concat(ssPq, ssT, ctT, pkT, XWING_LABEL));
|
|
286
|
+
}
|
|
287
|
+
function validateXWingPublicKey(pk) {
|
|
288
|
+
if (pk.length !== XWING_PUBLIC_KEY_SIZE) {
|
|
289
|
+
throw new Error(`X-Wing public key must be ${XWING_PUBLIC_KEY_SIZE} bytes, got ${pk.length}`);
|
|
290
|
+
}
|
|
291
|
+
import_ml_kem.ml_kem768.encapsulate(pk.subarray(0, MLKEM768_EK_SIZE));
|
|
292
|
+
}
|
|
293
|
+
function xwingEncap(pk, skE, pqRandomness) {
|
|
294
|
+
if (pk.length !== XWING_PUBLIC_KEY_SIZE) {
|
|
295
|
+
throw new Error(`X-Wing public key must be ${XWING_PUBLIC_KEY_SIZE} bytes, got ${pk.length}`);
|
|
296
|
+
}
|
|
297
|
+
const pkPq = pk.subarray(0, MLKEM768_EK_SIZE);
|
|
298
|
+
const pkT = pk.subarray(MLKEM768_EK_SIZE);
|
|
299
|
+
const skEObj = skE ?? import_ed25519.x25519.utils.randomSecretKey();
|
|
300
|
+
const ssT = import_ed25519.x25519.getSharedSecret(skEObj, pkT);
|
|
301
|
+
const ctT = import_ed25519.x25519.getPublicKey(skEObj);
|
|
302
|
+
const { cipherText: ctPq, sharedSecret: ssPq } = import_ml_kem.ml_kem768.encapsulate(pkPq, pqRandomness);
|
|
303
|
+
return [xwingCombine(ssPq, ssT, ctT, pkT), concat(ctPq, ctT)];
|
|
304
|
+
}
|
|
270
305
|
var Context = class {
|
|
271
|
-
constructor(key, baseNonce, exporterSecret) {
|
|
306
|
+
constructor(key, baseNonce, exporterSecret, suite = SUITE_ID) {
|
|
272
307
|
this.seq = 0;
|
|
273
308
|
this.key = key;
|
|
274
309
|
this.baseNonce = baseNonce;
|
|
275
310
|
this.exporterSecret = exporterSecret;
|
|
311
|
+
this.suiteId = suite;
|
|
276
312
|
}
|
|
277
313
|
computeNonce() {
|
|
278
314
|
const seqBytes = i2osp(this.seq, NN);
|
|
@@ -293,23 +329,32 @@ var Context = class {
|
|
|
293
329
|
return pt;
|
|
294
330
|
}
|
|
295
331
|
export(exporterContext, length) {
|
|
296
|
-
return labeledExpand(this.exporterSecret, bytes("sec"), exporterContext, length,
|
|
332
|
+
return labeledExpand(this.exporterSecret, bytes("sec"), exporterContext, length, this.suiteId);
|
|
297
333
|
}
|
|
298
334
|
};
|
|
299
|
-
function keySchedule(sharedSecret, info) {
|
|
335
|
+
function keySchedule(sharedSecret, info, suite = SUITE_ID) {
|
|
300
336
|
const empty = new Uint8Array(0);
|
|
301
|
-
const pskIdHash = labeledExtract(empty, bytes("psk_id_hash"), empty,
|
|
302
|
-
const infoHash = labeledExtract(empty, bytes("info_hash"), info,
|
|
337
|
+
const pskIdHash = labeledExtract(empty, bytes("psk_id_hash"), empty, suite);
|
|
338
|
+
const infoHash = labeledExtract(empty, bytes("info_hash"), info, suite);
|
|
303
339
|
const keyScheduleContext = concat(new Uint8Array([0]), pskIdHash, infoHash);
|
|
304
|
-
const secret = labeledExtract(sharedSecret, bytes("secret"), empty,
|
|
305
|
-
const key = labeledExpand(secret, bytes("key"), keyScheduleContext, NK,
|
|
306
|
-
const baseNonce = labeledExpand(secret, bytes("base_nonce"), keyScheduleContext, NN,
|
|
307
|
-
const exporterSecret = labeledExpand(secret, bytes("exp"), keyScheduleContext, NH,
|
|
308
|
-
return new Context(key, baseNonce, exporterSecret);
|
|
309
|
-
}
|
|
310
|
-
function setupBaseS(pkR, info, skE) {
|
|
311
|
-
|
|
312
|
-
|
|
340
|
+
const secret = labeledExtract(sharedSecret, bytes("secret"), empty, suite);
|
|
341
|
+
const key = labeledExpand(secret, bytes("key"), keyScheduleContext, NK, suite);
|
|
342
|
+
const baseNonce = labeledExpand(secret, bytes("base_nonce"), keyScheduleContext, NN, suite);
|
|
343
|
+
const exporterSecret = labeledExpand(secret, bytes("exp"), keyScheduleContext, NH, suite);
|
|
344
|
+
return new Context(key, baseNonce, exporterSecret, suite);
|
|
345
|
+
}
|
|
346
|
+
function setupBaseS(pkR, info, skE, pqRandomness) {
|
|
347
|
+
if (pkR.length === X25519_PUBLIC_KEY_SIZE) {
|
|
348
|
+
const [sharedSecret, enc] = encap(pkR, skE);
|
|
349
|
+
return { enc, ctx: keySchedule(sharedSecret, info, SUITE_ID) };
|
|
350
|
+
}
|
|
351
|
+
if (pkR.length === XWING_PUBLIC_KEY_SIZE) {
|
|
352
|
+
const [sharedSecret, enc] = xwingEncap(pkR, skE, pqRandomness);
|
|
353
|
+
return { enc, ctx: keySchedule(sharedSecret, info, XWING_SUITE_ID) };
|
|
354
|
+
}
|
|
355
|
+
throw new Error(
|
|
356
|
+
`HPKE recipient key is ${pkR.length} bytes; expected ${X25519_PUBLIC_KEY_SIZE} (X25519) or ${XWING_PUBLIC_KEY_SIZE} (X-Wing)`
|
|
357
|
+
);
|
|
313
358
|
}
|
|
314
359
|
function randomBytes(n) {
|
|
315
360
|
return (0, import_utils.randomBytes)(n);
|
|
@@ -389,6 +434,11 @@ function sealRequest(orgPubs, body, opts) {
|
|
|
389
434
|
const seen = /* @__PURE__ */ new Set();
|
|
390
435
|
const uniquePubs = [];
|
|
391
436
|
for (const p of pubs) {
|
|
437
|
+
if (p.length !== X25519_PUBLIC_KEY_SIZE && p.length !== XWING_PUBLIC_KEY_SIZE) {
|
|
438
|
+
throw new Error(
|
|
439
|
+
`sealRequest: org public key is ${p.length} bytes; expected ${X25519_PUBLIC_KEY_SIZE} (X25519) or ${XWING_PUBLIC_KEY_SIZE} (X-Wing)`
|
|
440
|
+
);
|
|
441
|
+
}
|
|
392
442
|
const fp = fingerprint(p);
|
|
393
443
|
if (!seen.has(fp)) {
|
|
394
444
|
seen.add(fp);
|
|
@@ -1827,7 +1877,7 @@ var Rerank = class {
|
|
|
1827
1877
|
};
|
|
1828
1878
|
|
|
1829
1879
|
// src/version.ts
|
|
1830
|
-
var VERSION = "0.
|
|
1880
|
+
var VERSION = "0.24.0";
|
|
1831
1881
|
var USER_AGENT = `pendra-node/${VERSION}`;
|
|
1832
1882
|
|
|
1833
1883
|
// src/_privateInferenceEngine.ts
|
|
@@ -1856,6 +1906,41 @@ function parsePinnedFingerprints(pin) {
|
|
|
1856
1906
|
}
|
|
1857
1907
|
return new Set(fps);
|
|
1858
1908
|
}
|
|
1909
|
+
var B64_RE = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
1910
|
+
function warnIgnoredCompanion(reason) {
|
|
1911
|
+
console.warn(`pendra: ignoring a post-quantum key from the server (${reason}); using the classical key.`);
|
|
1912
|
+
}
|
|
1913
|
+
function parseCompanion(entry) {
|
|
1914
|
+
const none = { pqPub: null, pqFp: null };
|
|
1915
|
+
const raw = entry.pq_public_key;
|
|
1916
|
+
if (raw === void 0 || raw === null) return none;
|
|
1917
|
+
if (typeof raw !== "string" || raw.length % 4 !== 0 || !B64_RE.test(raw)) {
|
|
1918
|
+
warnIgnoredCompanion("not valid base64");
|
|
1919
|
+
return none;
|
|
1920
|
+
}
|
|
1921
|
+
const pqPub = b64d2(raw);
|
|
1922
|
+
if (pqPub.length !== XWING_PUBLIC_KEY_SIZE) {
|
|
1923
|
+
warnIgnoredCompanion(`${pqPub.length} bytes, expected ${XWING_PUBLIC_KEY_SIZE}`);
|
|
1924
|
+
return none;
|
|
1925
|
+
}
|
|
1926
|
+
const pqFp = fingerprint(pqPub);
|
|
1927
|
+
const claimed = entry.pq_fingerprint;
|
|
1928
|
+
if (typeof claimed !== "string" || claimed.toLowerCase() !== pqFp) {
|
|
1929
|
+
warnIgnoredCompanion(`its fingerprint (${String(claimed)}) doesn't match its bytes (${pqFp})`);
|
|
1930
|
+
return none;
|
|
1931
|
+
}
|
|
1932
|
+
try {
|
|
1933
|
+
validateXWingPublicKey(pqPub);
|
|
1934
|
+
} catch (e) {
|
|
1935
|
+
warnIgnoredCompanion(String(e instanceof Error ? e.message : e));
|
|
1936
|
+
return none;
|
|
1937
|
+
}
|
|
1938
|
+
return { pqPub, pqFp };
|
|
1939
|
+
}
|
|
1940
|
+
function hybridOnlyIfAny(selected) {
|
|
1941
|
+
const hybrid = selected.filter((s) => s.pub.length === XWING_PUBLIC_KEY_SIZE);
|
|
1942
|
+
return hybrid.length > 0 ? hybrid : selected;
|
|
1943
|
+
}
|
|
1859
1944
|
function defaultTofuPath() {
|
|
1860
1945
|
const env = typeof process !== "undefined" ? process.env : void 0;
|
|
1861
1946
|
if (env?.PENDRA_TOFU_PATH) return env.PENDRA_TOFU_PATH;
|
|
@@ -1925,11 +2010,17 @@ var PrivateInferenceEngine = class {
|
|
|
1925
2010
|
}
|
|
1926
2011
|
/**
|
|
1927
2012
|
* Validate the key(s) returned by GET /v1/private-inference/key, apply the pin
|
|
1928
|
-
* allowlist (or auto-trust TOFU), cache the
|
|
2013
|
+
* allowlist (or auto-trust TOFU), cache the selected public keys, and return
|
|
1929
2014
|
* them (newest-seen first). The client seals to **all** of the returned keys
|
|
1930
|
-
* so any worker holding any of them can serve the request.
|
|
1931
|
-
*
|
|
1932
|
-
*
|
|
2015
|
+
* so any worker holding any of them can serve the request.
|
|
2016
|
+
*
|
|
2017
|
+
* A key may carry a post-quantum X-Wing companion (`pq_public_key`). Each key
|
|
2018
|
+
* contributes its companion when it has a valid one the client may use, else
|
|
2019
|
+
* its classical key; and if any selected key is a companion, only companions
|
|
2020
|
+
* are sealed to (see `hybridOnlyIfAny`).
|
|
2021
|
+
*
|
|
2022
|
+
* Throws PrivateInferenceError on absence, a malformed key, or (when pinned)
|
|
2023
|
+
* an empty intersection with the pin set.
|
|
1933
2024
|
*/
|
|
1934
2025
|
async resolveOrgKey(fetchedKeys) {
|
|
1935
2026
|
if (this.orgPubs !== null) return this.orgPubs;
|
|
@@ -1938,8 +2029,7 @@ var PrivateInferenceEngine = class {
|
|
|
1938
2029
|
"No encryption key is registered for this organisation. Start a worker and it will be issued one automatically."
|
|
1939
2030
|
);
|
|
1940
2031
|
}
|
|
1941
|
-
const
|
|
1942
|
-
const fps = [];
|
|
2032
|
+
const keys = [];
|
|
1943
2033
|
for (const entry of fetchedKeys) {
|
|
1944
2034
|
let pub;
|
|
1945
2035
|
try {
|
|
@@ -1947,32 +2037,46 @@ var PrivateInferenceEngine = class {
|
|
|
1947
2037
|
} catch (e) {
|
|
1948
2038
|
throw new PrivateInferenceError(`Malformed workload key from the server: ${String(e)}`);
|
|
1949
2039
|
}
|
|
1950
|
-
if (pub.length !==
|
|
2040
|
+
if (pub.length !== X25519_PUBLIC_KEY_SIZE) {
|
|
1951
2041
|
throw new PrivateInferenceError(
|
|
1952
2042
|
`Workload key from the server is ${pub.length} bytes; expected a 32-byte X25519 key.`
|
|
1953
2043
|
);
|
|
1954
2044
|
}
|
|
1955
|
-
|
|
1956
|
-
fps.push(fingerprint(pub));
|
|
2045
|
+
keys.push({ pub, fp: fingerprint(pub), ...parseCompanion(entry) });
|
|
1957
2046
|
}
|
|
1958
|
-
let
|
|
2047
|
+
let selected;
|
|
1959
2048
|
if (this.pinnedFingerprints !== null) {
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
2049
|
+
const pins = this.pinnedFingerprints;
|
|
2050
|
+
selected = [];
|
|
2051
|
+
for (const k of keys) {
|
|
2052
|
+
if (k.pqPub !== null && k.pqFp !== null && pins.has(k.pqFp)) {
|
|
2053
|
+
selected.push({ pub: k.pqPub, fp: k.pqFp });
|
|
2054
|
+
} else if (pins.has(k.fp)) {
|
|
2055
|
+
selected.push({ pub: k.pub, fp: k.fp });
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
if (selected.length === 0) {
|
|
2059
|
+
const offered = [...keys.map((k) => k.fp), ...keys.flatMap((k) => k.pqFp ? [k.pqFp] : [])];
|
|
2060
|
+
const pinned = Array.from(pins).sort().join(", ");
|
|
1963
2061
|
throw new PrivateInferenceError(
|
|
1964
|
-
`None of the org's workload key fingerprints (${
|
|
2062
|
+
`None of the org's workload key fingerprints (${offered.join(", ")}) match your pinned fingerprint(s) (${pinned}). Refusing to seal (possible man-in-the-middle). If your fleet re-keyed \u2014 which happens when every worker goes offline at once \u2014 update the pin to a current fingerprint from the console.`
|
|
1965
2063
|
);
|
|
1966
2064
|
}
|
|
1967
2065
|
} else {
|
|
2066
|
+
selected = keys.map(
|
|
2067
|
+
(k) => k.pqPub !== null && k.pqFp !== null ? { pub: k.pqPub, fp: k.pqFp } : { pub: k.pub, fp: k.fp }
|
|
2068
|
+
);
|
|
2069
|
+
}
|
|
2070
|
+
selected = hybridOnlyIfAny(selected);
|
|
2071
|
+
if (this.pinnedFingerprints === null) {
|
|
1968
2072
|
try {
|
|
1969
|
-
await this.tofuTrust(
|
|
2073
|
+
await this.tofuTrust(selected.map((s) => s.fp));
|
|
1970
2074
|
} catch (e) {
|
|
1971
2075
|
if (!isFsError(e)) throw e;
|
|
1972
2076
|
warnTofuUnwritable(this.tofuPath, e);
|
|
1973
2077
|
}
|
|
1974
|
-
accepted = pubs;
|
|
1975
2078
|
}
|
|
2079
|
+
const accepted = selected.map((s) => s.pub);
|
|
1976
2080
|
this.orgPubs = accepted;
|
|
1977
2081
|
return accepted;
|
|
1978
2082
|
}
|