-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathcredential-service.ts
More file actions
985 lines (914 loc) · 29.3 KB
/
Copy pathcredential-service.ts
File metadata and controls
985 lines (914 loc) · 29.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
import { spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { access, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
import path from 'node:path';
import type {
ByokChatProviderConfig,
ByokCredentialProfile,
UpsertByokCredentialProfileRequest,
} from '@open-design/contracts';
import {
isNodePtyUnavailableError,
loadNodePty,
} from '../services/node-pty.js';
import {
WindowsDpapiWorker,
WindowsDpapiWorkerError,
type WindowsDpapiWorkerDiagnostic,
type WindowsDpapiWorkerPhase,
} from './windows-dpapi-worker.js';
const PROFILE_ID_PATTERN = /^byok-[a-z0-9][a-z0-9._-]{2,95}$/u;
const KEYCHAIN_SERVICE = 'dev.opendesign.byok';
const MAX_SECRET_OUTPUT_BYTES = 64 * 1024;
const MAX_BYOK_API_KEY_BYTES = 32 * 1024;
const INTERACTIVE_SECRET_TIMEOUT_MS = 10_000;
type StoredProfile = Omit<ByokCredentialProfile, 'configured' | 'keyTail'>;
type StoredDocument = {
version: 1;
profiles: StoredProfile[];
};
export interface ByokSecretBackend {
readonly kind: string;
available(): Promise<boolean>;
close?(): Promise<void>;
set(profileId: string, secret: string): Promise<void>;
get(profileId: string): Promise<string | null>;
delete(profileId: string): Promise<boolean>;
}
export interface ResolvedByokCredentialProfile {
profile: ByokCredentialProfile;
provider: ByokChatProviderConfig;
apiKey: string;
}
export interface ByokCredentialServiceOptions {
backend?: ByokSecretBackend;
dataDir: string;
persistMetadata?: (
metadataPath: string,
document: { version: 1; profiles: readonly unknown[] },
) => Promise<void>;
}
export class ByokCredentialService {
readonly backend: ByokSecretBackend;
readonly metadataPath: string;
private mutationQueue: Promise<void> = Promise.resolve();
private closePromise: Promise<void> | null = null;
private closed = false;
private readonly persistMetadata: NonNullable<
ByokCredentialServiceOptions['persistMetadata']
>;
constructor(options: ByokCredentialServiceOptions) {
this.backend = options.backend ?? createPlatformByokSecretBackend(
process.platform,
options.dataDir,
);
this.metadataPath = path.join(options.dataDir, 'byok', 'profiles.json');
this.persistMetadata = options.persistMetadata ?? writeMetadataDocument;
}
async status(): Promise<{ available: boolean; backend: string }> {
this.assertOpen();
return {
available: await this.backend.available(),
backend: this.backend.kind,
};
}
async list(): Promise<ByokCredentialProfile[]> {
this.assertOpen();
const document = await this.readDocument();
return Promise.all(document.profiles.map((profile) => this.toPublicProfile(profile)));
}
async get(profileId: string): Promise<ByokCredentialProfile | null> {
this.assertOpen();
assertProfileId(profileId);
const stored = (await this.readDocument()).profiles.find((profile) => profile.id === profileId);
return stored ? this.toPublicProfile(stored) : null;
}
/**
* Checks profile metadata without reading the OS credential store. Run
* admission uses this so the secret is not materialized until the runtime is
* actually being started.
*/
async has(profileId: string): Promise<boolean> {
this.assertOpen();
assertProfileId(profileId);
return (await this.readDocument()).profiles.some((profile) => profile.id === profileId);
}
async upsert(input: UpsertByokCredentialProfileRequest): Promise<ByokCredentialProfile> {
this.assertOpen();
return this.serializeMutation(() => this.upsertUnlocked(input));
}
private async upsertUnlocked(
input: UpsertByokCredentialProfileRequest,
): Promise<ByokCredentialProfile> {
const normalized = normalizeProfileInput(input);
const apiKey = typeof input.apiKey === 'string' ? input.apiKey.trim() : '';
if (Buffer.byteLength(apiKey, 'utf8') > MAX_BYOK_API_KEY_BYTES) {
throw new Error(`apiKey must be at most ${MAX_BYOK_API_KEY_BYTES} UTF-8 bytes.`);
}
const available = await this.backend.available();
if (!available) {
throw new Error('Secure credential storage is unavailable on this system.');
}
const document = await this.readDocument();
const existingIndex = input.id
? document.profiles.findIndex((profile) => profile.id === input.id)
: -1;
const now = Date.now();
const id = input.id ?? createProfileId();
assertProfileId(id);
const existing = existingIndex >= 0 ? document.profiles[existingIndex] : undefined;
if (normalized.requiresApiKey && !apiKey && !existing) {
throw new Error('An API key is required when creating this BYOK profile.');
}
const previousSecret = apiKey ? await this.backend.get(id) : null;
if (apiKey) {
await this.backend.set(id, apiKey);
} else if (normalized.requiresApiKey && !(await this.backend.get(id))) {
throw new Error('The BYOK profile has no credential in secure storage.');
}
const stored: StoredProfile = {
id,
...normalized,
createdAt: existing?.createdAt ?? now,
updatedAt: now,
};
if (existingIndex >= 0) document.profiles[existingIndex] = stored;
else document.profiles.push(stored);
try {
await this.writeDocument(document);
} catch (error) {
if (apiKey) {
await this.restoreSecretAfterMetadataFailure(
id,
previousSecret,
error,
);
}
throw error;
}
return this.toPublicProfile(stored);
}
async resolve(profileId: string): Promise<ResolvedByokCredentialProfile | null> {
this.assertOpen();
assertProfileId(profileId);
const stored = (await this.readDocument()).profiles.find((profile) => profile.id === profileId);
if (!stored) return null;
const apiKey = stored.requiresApiKey ? (await this.backend.get(profileId))?.trim() ?? '' : '';
if (stored.requiresApiKey && !apiKey) return null;
const profile = await this.toPublicProfile(stored, apiKey);
return {
profile,
apiKey,
provider: {
protocol: stored.protocol,
apiKey,
baseUrl: stored.baseUrl,
model: stored.model,
requiresApiKey: stored.requiresApiKey,
...(stored.apiVersion ? { apiVersion: stored.apiVersion } : {}),
},
};
}
async delete(profileId: string): Promise<boolean> {
this.assertOpen();
return this.serializeMutation(() => this.deleteUnlocked(profileId));
}
async close(): Promise<void> {
this.closePromise ??= this.closeUnlocked();
return this.closePromise;
}
private async closeUnlocked(): Promise<void> {
this.closed = true;
await this.mutationQueue;
await this.backend.close?.();
}
private async deleteUnlocked(profileId: string): Promise<boolean> {
assertProfileId(profileId);
const document = await this.readDocument();
const next = document.profiles.filter((profile) => profile.id !== profileId);
const existed = next.length !== document.profiles.length;
const previousSecret = await this.backend.get(profileId);
const secretDeleted = await this.backend.delete(profileId);
if (existed) {
document.profiles = next;
try {
await this.writeDocument(document);
} catch (error) {
if (secretDeleted && previousSecret !== null) {
await this.restoreSecretAfterMetadataFailure(
profileId,
previousSecret,
error,
);
}
throw error;
}
}
return existed || secretDeleted;
}
private serializeMutation<T>(operation: () => Promise<T>): Promise<T> {
const result = this.mutationQueue.then(operation, operation);
this.mutationQueue = result.then(
() => undefined,
() => undefined,
);
return result;
}
private assertOpen(): void {
if (this.closed) {
throw new Error('Secure credential service is closed.');
}
}
private async toPublicProfile(
profile: StoredProfile,
knownSecret?: string,
): Promise<ByokCredentialProfile> {
const secret = profile.requiresApiKey
? knownSecret ?? await this.backend.get(profile.id) ?? ''
: '';
return {
...profile,
configured: !profile.requiresApiKey || Boolean(secret),
...(secret ? { keyTail: secret.slice(-4) } : {}),
};
}
private async readDocument(): Promise<StoredDocument> {
try {
const parsed = JSON.parse(await readFile(this.metadataPath, 'utf8')) as Partial<StoredDocument>;
if (parsed.version !== 1 || !Array.isArray(parsed.profiles)) {
return { version: 1, profiles: [] };
}
return {
version: 1,
profiles: parsed.profiles.filter(isStoredProfile),
};
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return { version: 1, profiles: [] };
}
throw error;
}
}
private async writeDocument(document: StoredDocument): Promise<void> {
await this.persistMetadata(this.metadataPath, document);
}
private async restoreSecretAfterMetadataFailure(
profileId: string,
previousSecret: string | null,
metadataError: unknown,
): Promise<void> {
try {
if (previousSecret === null) {
await this.backend.delete(profileId);
} else {
await this.backend.set(profileId, previousSecret);
}
} catch (rollbackError) {
throw new AggregateError(
[metadataError, rollbackError],
'BYOK metadata persistence failed and the secure credential rollback also failed.',
);
}
}
}
async function writeMetadataDocument(
metadataPath: string,
document: { version: 1; profiles: readonly unknown[] },
): Promise<void> {
await mkdir(path.dirname(metadataPath), { recursive: true, mode: 0o700 });
const temporaryPath = `${metadataPath}.${process.pid}.${randomUUID()}.tmp`;
await writeFile(temporaryPath, `${JSON.stringify(document, null, 2)}\n`, {
encoding: 'utf8',
mode: 0o600,
});
await rename(temporaryPath, metadataPath);
}
function normalizeProfileInput(
input: UpsertByokCredentialProfileRequest,
): Omit<StoredProfile, 'id' | 'createdAt' | 'updatedAt'> {
const label = requiredBoundedString(input.label, 'label', 120);
const baseUrl = requiredBoundedString(input.baseUrl, 'baseUrl', 2_048).replace(/\/+$/u, '');
const model = requiredBoundedString(input.model, 'model', 256);
const protocol = input.protocol;
if (!['anthropic', 'openai', 'azure', 'google', 'ollama', 'senseaudio', 'aihubmix'].includes(protocol)) {
throw new Error('Unsupported BYOK protocol.');
}
try {
const parsed = new URL(baseUrl);
if (
!['http:', 'https:'].includes(parsed.protocol)
|| parsed.username.length > 0
|| parsed.password.length > 0
|| parsed.search.length > 0
|| parsed.hash.length > 0
) {
throw new Error();
}
} catch {
throw new Error(
'BYOK baseUrl must be an absolute HTTP(S) URL without credentials, query, or fragment.',
);
}
const apiVersion = typeof input.apiVersion === 'string' ? input.apiVersion.trim() : '';
return {
label,
protocol,
baseUrl,
model,
requiresApiKey: input.requiresApiKey !== false,
...(apiVersion ? { apiVersion: apiVersion.slice(0, 128) } : {}),
};
}
function requiredBoundedString(value: unknown, field: string, max: number): string {
const trimmed = typeof value === 'string' ? value.trim() : '';
if (!trimmed || trimmed.length > max) {
throw new Error(`${field} is required and must be at most ${max} characters.`);
}
return trimmed;
}
function createProfileId(): string {
return `byok-${randomUUID()}`;
}
function assertProfileId(profileId: string): void {
if (!PROFILE_ID_PATTERN.test(profileId)) throw new Error('Invalid BYOK profile id.');
}
function isStoredProfile(value: unknown): value is StoredProfile {
if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
const profile = value as Partial<StoredProfile>;
return typeof profile.id === 'string'
&& PROFILE_ID_PATTERN.test(profile.id)
&& typeof profile.label === 'string'
&& typeof profile.protocol === 'string'
&& typeof profile.baseUrl === 'string'
&& typeof profile.model === 'string'
&& typeof profile.requiresApiKey === 'boolean'
&& typeof profile.createdAt === 'number'
&& typeof profile.updatedAt === 'number';
}
export function createPlatformByokSecretBackend(
platform: NodeJS.Platform = process.platform,
dataDir?: string,
): ByokSecretBackend {
if (platform === 'darwin') return new MacOsKeychainBackend();
if (platform === 'linux') return new LinuxSecretServiceBackend();
if (platform === 'win32' && dataDir) {
return new WindowsDpapiBackend(path.join(dataDir, 'byok', 'secrets'));
}
return new UnavailableSecretBackend(platform);
}
class MacOsKeychainBackend implements ByokSecretBackend {
readonly kind = 'macos-keychain';
private writable: boolean | null = null;
async available() {
return this.writable !== false && await commandAvailable('/usr/bin/security');
}
async set(profileId: string, secret: string) {
try {
await runInteractiveMacOsSecretCommand(
'/usr/bin/security',
['add-generic-password', '-a', profileId, '-s', KEYCHAIN_SERVICE, '-U', '-w'],
secret,
);
this.writable = true;
} catch (error) {
this.writable = false;
if (isNodePtyUnavailableError(error)) {
throw new Error(
'Secure credential storage is unavailable because its native PTY helper could not be loaded.',
{ cause: error },
);
}
throw new Error('Secure credential backend command failed.');
}
}
async get(profileId: string) {
const result = await runSecretCommand(
'/usr/bin/security',
['find-generic-password', '-a', profileId, '-s', KEYCHAIN_SERVICE, '-w'],
undefined,
true,
);
return result === null ? null : result.trimEnd();
}
async delete(profileId: string) {
return (await runSecretCommand(
'/usr/bin/security',
['delete-generic-password', '-a', profileId, '-s', KEYCHAIN_SERVICE],
undefined,
true,
)) !== null;
}
}
class LinuxSecretServiceBackend implements ByokSecretBackend {
readonly kind = 'linux-secret-service';
private readonly command = 'secret-tool';
async available() {
return commandAvailable(this.command);
}
async set(profileId: string, secret: string) {
await runSecretCommand(
this.command,
['store', '--label=Open Design BYOK', 'service', KEYCHAIN_SERVICE, 'account', profileId],
secret,
);
}
async get(profileId: string) {
const result = await runSecretCommand(
this.command,
['lookup', 'service', KEYCHAIN_SERVICE, 'account', profileId],
undefined,
true,
);
return result === null ? null : result.trimEnd();
}
async delete(profileId: string) {
return (await runSecretCommand(
this.command,
['clear', 'service', KEYCHAIN_SERVICE, 'account', profileId],
undefined,
true,
)) !== null;
}
}
type WindowsDpapiWorkerClient = Pick<WindowsDpapiWorker, 'close' | 'ready' | 'run'>;
type WindowsDpapiWorkerTimeouts = {
connectMs: number;
startupMs: number;
readyMs: number;
operationMs: number;
shutdownMs: number;
};
type WindowsDpapiWorkerSlot = {
abortController: AbortController;
closePromise: Promise<void> | null;
generation: number;
promise: Promise<WindowsDpapiWorkerClient>;
readyPromise: Promise<void> | null;
};
const DEFAULT_WINDOWS_DPAPI_WORKER_TIMEOUTS: WindowsDpapiWorkerTimeouts = {
connectMs: 50_000,
startupMs: 55_000,
readyMs: 3_000,
operationMs: 5_000,
shutdownMs: 5_000,
};
export type WindowsDpapiBackendOptions = {
commandAvailable?: (command: string) => Promise<boolean>;
createWorker?: (options?: { signal: AbortSignal }) => Promise<WindowsDpapiWorkerClient>;
onDiagnostic?: (diagnostic: WindowsDpapiWorkerDiagnostic) => void;
timeouts?: Partial<WindowsDpapiWorkerTimeouts>;
};
export class WindowsDpapiBackend implements ByokSecretBackend {
readonly kind = 'windows-dpapi';
private availability: Promise<boolean> | null = null;
private worker: WindowsDpapiWorkerSlot | null = null;
private workerGeneration = 0;
private closePromise: Promise<void> | null = null;
private closed = false;
private readonly timeouts: WindowsDpapiWorkerTimeouts;
constructor(
private readonly secretsDir: string,
private readonly options: WindowsDpapiBackendOptions = {},
) {
this.timeouts = {
connectMs: positiveWindowsDpapiTimeout(
options.timeouts?.connectMs,
DEFAULT_WINDOWS_DPAPI_WORKER_TIMEOUTS.connectMs,
),
startupMs: positiveWindowsDpapiTimeout(
options.timeouts?.startupMs,
DEFAULT_WINDOWS_DPAPI_WORKER_TIMEOUTS.startupMs,
),
readyMs: positiveWindowsDpapiTimeout(
options.timeouts?.readyMs,
DEFAULT_WINDOWS_DPAPI_WORKER_TIMEOUTS.readyMs,
),
operationMs: positiveWindowsDpapiTimeout(
options.timeouts?.operationMs,
DEFAULT_WINDOWS_DPAPI_WORKER_TIMEOUTS.operationMs,
),
shutdownMs: positiveWindowsDpapiTimeout(
options.timeouts?.shutdownMs,
DEFAULT_WINDOWS_DPAPI_WORKER_TIMEOUTS.shutdownMs,
),
};
}
async available() {
if (this.closed) return false;
this.availability ??= this.probeAvailability();
const attempt = this.availability;
try {
const available = await attempt;
if (!available && this.availability === attempt) {
this.availability = null;
}
return available;
} catch {
if (this.availability === attempt) {
this.availability = null;
}
return false;
}
}
private async probeAvailability() {
if (!(await (this.options.commandAvailable ?? commandAvailable)('powershell.exe'))) {
return false;
}
try {
await this.getReadyWorker();
return true;
} catch {
return false;
}
}
async set(profileId: string, secret: string) {
assertProfileId(profileId);
await this.runWorker(
'set',
path.join(this.secretsDir, `${profileId}.bin`),
secret,
);
}
async get(profileId: string) {
assertProfileId(profileId);
const result = await this.runWorker(
'get',
path.join(this.secretsDir, `${profileId}.bin`),
);
return result.found ? result.value : null;
}
async delete(profileId: string) {
assertProfileId(profileId);
return (await this.runWorker(
'delete',
path.join(this.secretsDir, `${profileId}.bin`),
)).found;
}
async close(): Promise<void> {
this.closePromise ??= this.closeUnlocked();
return this.closePromise;
}
private async closeUnlocked(): Promise<void> {
this.closed = true;
this.availability = null;
const slot = this.worker;
this.worker = null;
if (slot) await this.disposeWorker(slot);
}
private async runWorker(
operation: 'set' | 'get' | 'delete',
secretPath: string,
secret?: string,
) {
const { slot, worker } = await this.getReadyWorker();
const startedAt = Date.now();
try {
return await withWindowsDpapiTimeout(
worker.run(operation, secretPath, secret),
this.timeouts.operationMs,
'operation',
);
} catch (error) {
throw await this.handleWorkerFailure(
slot,
error,
'operation',
startedAt,
worker,
);
}
}
private async getReadyWorker(): Promise<{
slot: WindowsDpapiWorkerSlot;
worker: WindowsDpapiWorkerClient;
}> {
const slot = this.getWorkerSlot();
const startupStartedAt = Date.now();
let worker: WindowsDpapiWorkerClient;
try {
worker = await withWindowsDpapiTimeout(
slot.promise,
this.timeouts.startupMs,
'spawn',
);
} catch (error) {
throw await this.handleWorkerFailure(slot, error, 'spawn', startupStartedAt);
}
const readyStartedAt = Date.now();
try {
slot.readyPromise ??= worker.ready();
await withWindowsDpapiTimeout(
slot.readyPromise,
this.timeouts.readyMs,
'ready',
);
} catch (error) {
throw await this.handleWorkerFailure(
slot,
error,
'ready',
readyStartedAt,
worker,
);
}
return { slot, worker };
}
private getWorkerSlot(): WindowsDpapiWorkerSlot {
if (this.closed) {
throw new WindowsDpapiWorkerError('operation', 'closed', false);
}
if (this.worker) return this.worker;
const generation = ++this.workerGeneration;
const abortController = new AbortController();
const promise = Promise.resolve().then(
() => this.options.createWorker?.({
signal: abortController.signal,
}) ?? WindowsDpapiWorker.create({
connectTimeoutMs: this.timeouts.connectMs,
signal: abortController.signal,
shutdownTimeoutMs: this.timeouts.shutdownMs,
}),
);
const slot: WindowsDpapiWorkerSlot = {
abortController,
closePromise: null,
generation,
promise,
readyPromise: null,
};
this.worker = slot;
return slot;
}
private async handleWorkerFailure(
slot: WindowsDpapiWorkerSlot,
error: unknown,
fallbackPhase: WindowsDpapiWorkerPhase,
startedAt: number,
worker?: WindowsDpapiWorkerClient,
): Promise<WindowsDpapiWorkerError> {
const safeError = normalizeWindowsDpapiError(error, fallbackPhase);
if (this.closed && safeError.failureClass === 'closed') {
return safeError;
}
this.reportDiagnostic({
phase: safeError.phase,
failureClass: safeError.failureClass,
durationMs: Math.max(0, Date.now() - startedAt),
workerGeneration: slot.generation,
});
if (safeError.fatal) {
if (this.worker === slot) {
this.worker = null;
this.availability = null;
}
try {
await this.disposeWorker(slot, worker);
} catch {
// Preserve the operation failure; disposeWorker already emitted the
// separate safe shutdown diagnostic.
}
}
return safeError;
}
private async disposeWorker(
slot: WindowsDpapiWorkerSlot,
knownWorker?: WindowsDpapiWorkerClient,
): Promise<void> {
slot.closePromise ??= this.disposeWorkerUnlocked(slot, knownWorker);
await slot.closePromise;
}
private async disposeWorkerUnlocked(
slot: WindowsDpapiWorkerSlot,
knownWorker?: WindowsDpapiWorkerClient,
): Promise<void> {
slot.abortController.abort();
let worker = knownWorker;
if (!worker) {
try {
worker = await withWindowsDpapiTimeout(
slot.promise,
this.timeouts.shutdownMs,
'shutdown',
);
} catch (error) {
const safeError = normalizeWindowsDpapiError(error, 'shutdown');
if (safeError.failureClass === 'closed') return;
void slot.promise.then(
(lateWorker) => withWindowsDpapiTimeout(
lateWorker.close(),
this.timeouts.shutdownMs,
'shutdown',
).catch(() => undefined),
() => undefined,
);
if (safeError.failureClass === 'timeout') {
this.reportDiagnostic({
phase: 'shutdown',
failureClass: 'timeout',
durationMs: this.timeouts.shutdownMs,
workerGeneration: slot.generation,
});
throw safeError;
}
return;
}
}
const startedAt = Date.now();
try {
await withWindowsDpapiTimeout(
worker.close(),
this.timeouts.shutdownMs,
'shutdown',
);
} catch (error) {
const safeError = normalizeWindowsDpapiError(error, 'shutdown');
this.reportDiagnostic({
phase: safeError.phase,
failureClass: safeError.failureClass,
durationMs: Math.max(0, Date.now() - startedAt),
workerGeneration: slot.generation,
});
throw safeError;
}
}
private reportDiagnostic(diagnostic: WindowsDpapiWorkerDiagnostic): void {
try {
if (this.options.onDiagnostic) {
this.options.onDiagnostic(diagnostic);
} else {
console.warn('[byok] Windows DPAPI worker failure', diagnostic);
}
} catch {
// Observability must never change credential storage behavior.
}
}
}
function withWindowsDpapiTimeout<T>(
promise: Promise<T>,
timeoutMs: number,
phase: WindowsDpapiWorkerPhase,
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const timer = setTimeout(() => {
reject(new WindowsDpapiWorkerError(phase, 'timeout'));
}, timeoutMs);
timer.unref?.();
promise.then(
(value) => {
clearTimeout(timer);
resolve(value);
},
(error) => {
clearTimeout(timer);
reject(error);
},
);
});
}
function positiveWindowsDpapiTimeout(
value: number | undefined,
fallback: number,
): number {
return Number.isFinite(value) && Number(value) > 0 ? Number(value) : fallback;
}
function normalizeWindowsDpapiError(
error: unknown,
phase: WindowsDpapiWorkerPhase,
): WindowsDpapiWorkerError {
return error instanceof WindowsDpapiWorkerError
? error
: new WindowsDpapiWorkerError(phase, 'unknown');
}
class UnavailableSecretBackend implements ByokSecretBackend {
readonly kind: string;
constructor(platform: NodeJS.Platform) {
this.kind = `unavailable-${platform}`;
}
async available() { return false; }
async set() { throw new Error('Secure credential storage is unavailable on this system.'); }
async get() { return null; }
async delete() { return false; }
}
async function commandAvailable(command: string): Promise<boolean> {
if (path.isAbsolute(command)) {
try {
await access(command);
return true;
} catch {
return false;
}
}
const pathEntries = (process.env.PATH ?? '').split(path.delimiter);
return Promise.any(pathEntries.map(async (entry) => {
const candidate = path.join(entry, command);
await access(candidate);
return true;
})).catch(() => false);
}
async function runSecretCommand(
command: string,
args: string[],
secretInput?: string,
allowNotFound = false,
): Promise<string | null> {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
stdio: ['pipe', 'pipe', 'pipe'],
windowsHide: true,
});
const stdout: Buffer[] = [];
let stdoutBytes = 0;
child.stdout.on('data', (chunk: Buffer) => {
stdoutBytes += chunk.length;
if (stdoutBytes <= MAX_SECRET_OUTPUT_BYTES) stdout.push(chunk);
});
child.stderr.resume();
child.on('error', () => reject(new Error('Secure credential backend command failed.')));
child.on('close', (code) => {
if (code === 0) return resolve(Buffer.concat(stdout).toString('utf8'));
if (allowNotFound && code === 44) return resolve(null);
if (allowNotFound && code === 1) return resolve(null);
return reject(new Error('Secure credential backend command failed.'));
});
if (secretInput !== undefined) child.stdin.end(`${secretInput}\n`);
else child.stdin.end();
});
}
/**
* `security add-generic-password -w` intentionally prompts twice when the
* password argument is omitted. A regular stdin pipe is not accepted by the
* macOS tool, while putting the key after `-w` exposes it in the process list.
* Drive only those bounded prompts through a pseudo-terminal and keep all
* output private.
*/
async function runInteractiveMacOsSecretCommand(
command: string,
args: string[],
secret: string,
): Promise<void> {
const { spawn: spawnPty } = await loadNodePty();
return new Promise((resolve, reject) => {
let settled = false;
let promptResponses = 0;
let promptScan = '';
let dataDisposable: { dispose(): void } | null = null;
let exitDisposable: { dispose(): void } | null = null;
// `security` does not reliably disable terminal echo before its password
// prompts. Use a fixed shell program (no interpolation) to disable echo,
// then exec the command and its validated argv verbatim.
const child = spawnPty('/bin/sh', [
'-c',
'stty -echo; exec "$@"',
'open-design-keychain',
command,
...args,
], {
name: 'xterm-256color',
cols: 80,
rows: 24,
cwd: process.cwd(),
env: { ...process.env },
});
const finish = (error?: Error) => {
if (settled) return;
settled = true;
clearTimeout(timer);
dataDisposable?.dispose();
exitDisposable?.dispose();
if (error) reject(error);
else resolve();
};
const timer = setTimeout(() => {
try {
child.kill();
} catch {
// Best-effort termination; the generic failure below stays secret-free.
}
finish(new Error('Secure credential backend command failed.'));
}, INTERACTIVE_SECRET_TIMEOUT_MS);
timer.unref?.();
dataDisposable = child.onData((chunk) => {
// Security prompts should disable terminal echo. Redact defensively
// before retaining the bounded prompt tail anyway.
const safeChunk = secret
? chunk.split(secret).join('[redacted]')
: chunk;
promptScan = `${promptScan}${safeChunk}`.slice(-512);
const promptCount = (
promptScan.match(/(?:retype[^\r\n:]*|password[^\r\n:]*)\s*:/giu)
?? []
).length;
while (promptResponses < Math.min(promptCount, 2)) {
child.write(`${secret}\r`);
promptResponses += 1;
}
});
exitDisposable = child.onExit(({ exitCode }) => {
finish(
exitCode === 0
? undefined
: new Error('Secure credential backend command failed.'),
);
});
});
}