@gitpagedocs/tools 1.1.61 → 1.1.63
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 +33 -33
- package/package.json +1 -1
- package/src/ai/bootstrap.ts +50 -50
- package/src/ai/catalog.ts +188 -188
- package/src/ai/factory.ts +14 -14
- package/src/ai/http/streaming.ts +60 -60
- package/src/ai/index.ts +27 -27
- package/src/ai/legacy-adapter.ts +33 -33
- package/src/ai/model-registry.ts +13 -13
- package/src/ai/providers/anthropic-provider.ts +83 -83
- package/src/ai/providers/cohere-provider.ts +66 -66
- package/src/ai/providers/gemini-provider.ts +84 -84
- package/src/ai/providers/ollama-provider.ts +69 -69
- package/src/ai/providers/openai-compatible-provider.ts +75 -75
- package/src/ai/providers/shared.ts +72 -72
- package/src/ai/registry.ts +29 -29
- package/src/cache/file-cache.ts +86 -86
- package/src/cache/index.ts +9 -9
- package/src/cache/memory-cache.ts +47 -47
- package/src/cache/web-storage-cache.ts +91 -91
- package/src/config/config-loader.ts +59 -59
- package/src/config/index.ts +1 -1
- package/src/constants/config.ts +8 -8
- package/src/constants/index.ts +1 -1
- package/src/crypto/doc-access.ts +62 -62
- package/src/crypto/index.ts +4 -4
- package/src/crypto/node-crypto-service.ts +87 -87
- package/src/crypto/web-crypto-service.ts +102 -102
- package/src/crypto/web.ts +4 -4
- package/src/documentation/doc-generator.ts +74 -74
- package/src/documentation/doc-updater.ts +47 -47
- package/src/documentation/index.ts +18 -18
- package/src/documentation/marker-patcher.ts +33 -33
- package/src/documentation/sections.ts +84 -84
- package/src/errors/app-error.ts +84 -84
- package/src/errors/index.ts +1 -1
- package/src/filesystem/file-service.ts +121 -121
- package/src/filesystem/index.ts +2 -2
- package/src/index.ts +25 -25
- package/src/logger/index.ts +2 -2
- package/src/logger/logger.ts +73 -73
- package/src/logger/redaction.ts +43 -43
- package/src/platform/index.ts +1 -1
- package/src/platform/user-config-dir.ts +48 -48
- package/src/ports/ai.ts +109 -109
- package/src/ports/cache.ts +16 -16
- package/src/ports/config.ts +21 -21
- package/src/ports/crypto.ts +33 -33
- package/src/ports/index.ts +15 -15
- package/src/ports/logger.ts +23 -23
- package/src/ports/security.ts +42 -42
- package/src/security/credential-vault.ts +117 -117
- package/src/security/file-vault-storage.ts +25 -25
- package/src/security/index.ts +8 -8
- package/src/security/migrate-plaintext-key.ts +38 -38
- package/src/security/password-gate.ts +62 -62
- package/src/security/web-storage-vault-storage.ts +23 -23
- package/src/security/web.ts +9 -9
package/src/ports/security.ts
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Security ports: password gate + credential vault. Implemented in Phase 5.
|
|
3
|
-
* Sensitive operations (generate/update docs, analyze, run AI, deploy, run MCP)
|
|
4
|
-
* must pass through the gate before executing.
|
|
5
|
-
*/
|
|
6
|
-
import type { EncryptedPayload } from "./crypto";
|
|
7
|
-
|
|
8
|
-
export type SensitiveOperation =
|
|
9
|
-
| "generate-docs"
|
|
10
|
-
| "update-docs"
|
|
11
|
-
| "analyze-project"
|
|
12
|
-
| "run-ai"
|
|
13
|
-
| "deploy"
|
|
14
|
-
| "run-mcp";
|
|
15
|
-
|
|
16
|
-
export interface StoredCredential {
|
|
17
|
-
readonly providerId: string;
|
|
18
|
-
/** Encrypted secret; plaintext is never persisted. */
|
|
19
|
-
readonly secret: EncryptedPayload;
|
|
20
|
-
readonly updatedAtIso?: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface CredentialVault {
|
|
24
|
-
isInitialized(): Promise<boolean>;
|
|
25
|
-
/** Establish the local password (first run) and create an empty vault. */
|
|
26
|
-
initialize(password: string): Promise<void>;
|
|
27
|
-
unlock(password: string): Promise<boolean>;
|
|
28
|
-
setCredential(password: string, providerId: string, secret: string): Promise<void>;
|
|
29
|
-
/** Returns the decrypted secret only when the password is correct. */
|
|
30
|
-
getCredential(password: string, providerId: string): Promise<string | undefined>;
|
|
31
|
-
listProviders(): Promise<readonly string[]>;
|
|
32
|
-
removeCredential(password: string, providerId: string): Promise<void>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface PasswordGate {
|
|
36
|
-
/**
|
|
37
|
-
* Authorize a sensitive operation. Implementations prompt for / verify the
|
|
38
|
-
* local password (one unlock-per-session policy) and throw SecurityError on
|
|
39
|
-
* failure. Returns the unlock token/password handle for downstream vault use.
|
|
40
|
-
*/
|
|
41
|
-
authorize(operation: SensitiveOperation): Promise<string>;
|
|
42
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Security ports: password gate + credential vault. Implemented in Phase 5.
|
|
3
|
+
* Sensitive operations (generate/update docs, analyze, run AI, deploy, run MCP)
|
|
4
|
+
* must pass through the gate before executing.
|
|
5
|
+
*/
|
|
6
|
+
import type { EncryptedPayload } from "./crypto";
|
|
7
|
+
|
|
8
|
+
export type SensitiveOperation =
|
|
9
|
+
| "generate-docs"
|
|
10
|
+
| "update-docs"
|
|
11
|
+
| "analyze-project"
|
|
12
|
+
| "run-ai"
|
|
13
|
+
| "deploy"
|
|
14
|
+
| "run-mcp";
|
|
15
|
+
|
|
16
|
+
export interface StoredCredential {
|
|
17
|
+
readonly providerId: string;
|
|
18
|
+
/** Encrypted secret; plaintext is never persisted. */
|
|
19
|
+
readonly secret: EncryptedPayload;
|
|
20
|
+
readonly updatedAtIso?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CredentialVault {
|
|
24
|
+
isInitialized(): Promise<boolean>;
|
|
25
|
+
/** Establish the local password (first run) and create an empty vault. */
|
|
26
|
+
initialize(password: string): Promise<void>;
|
|
27
|
+
unlock(password: string): Promise<boolean>;
|
|
28
|
+
setCredential(password: string, providerId: string, secret: string): Promise<void>;
|
|
29
|
+
/** Returns the decrypted secret only when the password is correct. */
|
|
30
|
+
getCredential(password: string, providerId: string): Promise<string | undefined>;
|
|
31
|
+
listProviders(): Promise<readonly string[]>;
|
|
32
|
+
removeCredential(password: string, providerId: string): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PasswordGate {
|
|
36
|
+
/**
|
|
37
|
+
* Authorize a sensitive operation. Implementations prompt for / verify the
|
|
38
|
+
* local password (one unlock-per-session policy) and throw SecurityError on
|
|
39
|
+
* failure. Returns the unlock token/password handle for downstream vault use.
|
|
40
|
+
*/
|
|
41
|
+
authorize(operation: SensitiveOperation): Promise<string>;
|
|
42
|
+
}
|
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import type { CredentialVault } from "../ports/security";
|
|
2
|
-
import type { CryptoService, EncryptedPayload } from "../ports/crypto";
|
|
3
|
-
import { SecurityError } from "../errors/app-error";
|
|
4
|
-
|
|
5
|
-
/** Pluggable persistence for the encrypted vault (file, web storage, …). */
|
|
6
|
-
export interface VaultStorage {
|
|
7
|
-
load(): Promise<string | null>;
|
|
8
|
-
save(serialized: string): Promise<void>;
|
|
9
|
-
/** Permanently remove the stored vault (used by a password reset). */
|
|
10
|
-
clear(): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface VaultFile {
|
|
14
|
-
readonly version: 1;
|
|
15
|
-
/** Encrypts a fixed sentinel so a password can be verified before use. */
|
|
16
|
-
readonly verifier: EncryptedPayload;
|
|
17
|
-
readonly credentials: Record<string, EncryptedPayload>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const SENTINEL = "gitpagedocs::vault::v1";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Password-encrypted credential store. Plaintext secrets are never persisted;
|
|
24
|
-
* every secret is sealed with AES-256-GCM under a PBKDF2-derived key.
|
|
25
|
-
*/
|
|
26
|
-
export class EncryptedCredentialVault implements CredentialVault {
|
|
27
|
-
private file?: VaultFile;
|
|
28
|
-
|
|
29
|
-
constructor(
|
|
30
|
-
private readonly storage: VaultStorage,
|
|
31
|
-
private readonly crypto: CryptoService,
|
|
32
|
-
) {}
|
|
33
|
-
|
|
34
|
-
private async read(): Promise<VaultFile | undefined> {
|
|
35
|
-
if (this.file) return this.file;
|
|
36
|
-
const raw = await this.storage.load();
|
|
37
|
-
if (!raw) return undefined;
|
|
38
|
-
this.file = JSON.parse(raw) as VaultFile;
|
|
39
|
-
return this.file;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private async write(file: VaultFile): Promise<void> {
|
|
43
|
-
this.file = file;
|
|
44
|
-
await this.storage.save(JSON.stringify(file));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async isInitialized(): Promise<boolean> {
|
|
48
|
-
return (await this.read()) !== undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async initialize(password: string): Promise<void> {
|
|
52
|
-
if (await this.isInitialized()) {
|
|
53
|
-
throw new SecurityError("Vault is already initialized.");
|
|
54
|
-
}
|
|
55
|
-
const verifier = await this.crypto.encrypt(SENTINEL, password);
|
|
56
|
-
await this.write({ version: 1, verifier, credentials: {} });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async unlock(password: string): Promise<boolean> {
|
|
60
|
-
const file = await this.read();
|
|
61
|
-
if (!file) throw new SecurityError("Vault is not initialized.");
|
|
62
|
-
try {
|
|
63
|
-
const value = await this.crypto.decrypt(file.verifier, password);
|
|
64
|
-
return value === SENTINEL;
|
|
65
|
-
} catch {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private async requireUnlocked(password: string): Promise<VaultFile> {
|
|
71
|
-
const file = await this.read();
|
|
72
|
-
if (!file) throw new SecurityError("Vault is not initialized.");
|
|
73
|
-
if (!(await this.unlock(password))) {
|
|
74
|
-
throw new SecurityError("Incorrect vault password.");
|
|
75
|
-
}
|
|
76
|
-
return file;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async setCredential(password: string, providerId: string, secret: string): Promise<void> {
|
|
80
|
-
const file = await this.requireUnlocked(password);
|
|
81
|
-
const sealed = await this.crypto.encrypt(secret, password);
|
|
82
|
-
await this.write({
|
|
83
|
-
...file,
|
|
84
|
-
credentials: { ...file.credentials, [providerId]: sealed },
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async getCredential(password: string, providerId: string): Promise<string | undefined> {
|
|
89
|
-
const file = await this.requireUnlocked(password);
|
|
90
|
-
const sealed = file.credentials[providerId];
|
|
91
|
-
if (!sealed) return undefined;
|
|
92
|
-
return this.crypto.decrypt(sealed, password);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async listProviders(): Promise<readonly string[]> {
|
|
96
|
-
const file = await this.read();
|
|
97
|
-
return file ? Object.keys(file.credentials) : [];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async removeCredential(password: string, providerId: string): Promise<void> {
|
|
101
|
-
const file = await this.requireUnlocked(password);
|
|
102
|
-
const next = { ...file.credentials };
|
|
103
|
-
delete next[providerId];
|
|
104
|
-
await this.write({ ...file, credentials: next });
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Wipe the whole vault — every stored credential and the password verifier.
|
|
109
|
-
* Used by a "forgot password" reset: the user loses all saved keys and then
|
|
110
|
-
* creates a fresh password. No password is required (the point is recovery
|
|
111
|
-
* when the password is lost).
|
|
112
|
-
*/
|
|
113
|
-
async reset(): Promise<void> {
|
|
114
|
-
this.file = undefined;
|
|
115
|
-
await this.storage.clear();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
1
|
+
import type { CredentialVault } from "../ports/security";
|
|
2
|
+
import type { CryptoService, EncryptedPayload } from "../ports/crypto";
|
|
3
|
+
import { SecurityError } from "../errors/app-error";
|
|
4
|
+
|
|
5
|
+
/** Pluggable persistence for the encrypted vault (file, web storage, …). */
|
|
6
|
+
export interface VaultStorage {
|
|
7
|
+
load(): Promise<string | null>;
|
|
8
|
+
save(serialized: string): Promise<void>;
|
|
9
|
+
/** Permanently remove the stored vault (used by a password reset). */
|
|
10
|
+
clear(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface VaultFile {
|
|
14
|
+
readonly version: 1;
|
|
15
|
+
/** Encrypts a fixed sentinel so a password can be verified before use. */
|
|
16
|
+
readonly verifier: EncryptedPayload;
|
|
17
|
+
readonly credentials: Record<string, EncryptedPayload>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const SENTINEL = "gitpagedocs::vault::v1";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Password-encrypted credential store. Plaintext secrets are never persisted;
|
|
24
|
+
* every secret is sealed with AES-256-GCM under a PBKDF2-derived key.
|
|
25
|
+
*/
|
|
26
|
+
export class EncryptedCredentialVault implements CredentialVault {
|
|
27
|
+
private file?: VaultFile;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
private readonly storage: VaultStorage,
|
|
31
|
+
private readonly crypto: CryptoService,
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
private async read(): Promise<VaultFile | undefined> {
|
|
35
|
+
if (this.file) return this.file;
|
|
36
|
+
const raw = await this.storage.load();
|
|
37
|
+
if (!raw) return undefined;
|
|
38
|
+
this.file = JSON.parse(raw) as VaultFile;
|
|
39
|
+
return this.file;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private async write(file: VaultFile): Promise<void> {
|
|
43
|
+
this.file = file;
|
|
44
|
+
await this.storage.save(JSON.stringify(file));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async isInitialized(): Promise<boolean> {
|
|
48
|
+
return (await this.read()) !== undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async initialize(password: string): Promise<void> {
|
|
52
|
+
if (await this.isInitialized()) {
|
|
53
|
+
throw new SecurityError("Vault is already initialized.");
|
|
54
|
+
}
|
|
55
|
+
const verifier = await this.crypto.encrypt(SENTINEL, password);
|
|
56
|
+
await this.write({ version: 1, verifier, credentials: {} });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async unlock(password: string): Promise<boolean> {
|
|
60
|
+
const file = await this.read();
|
|
61
|
+
if (!file) throw new SecurityError("Vault is not initialized.");
|
|
62
|
+
try {
|
|
63
|
+
const value = await this.crypto.decrypt(file.verifier, password);
|
|
64
|
+
return value === SENTINEL;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private async requireUnlocked(password: string): Promise<VaultFile> {
|
|
71
|
+
const file = await this.read();
|
|
72
|
+
if (!file) throw new SecurityError("Vault is not initialized.");
|
|
73
|
+
if (!(await this.unlock(password))) {
|
|
74
|
+
throw new SecurityError("Incorrect vault password.");
|
|
75
|
+
}
|
|
76
|
+
return file;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async setCredential(password: string, providerId: string, secret: string): Promise<void> {
|
|
80
|
+
const file = await this.requireUnlocked(password);
|
|
81
|
+
const sealed = await this.crypto.encrypt(secret, password);
|
|
82
|
+
await this.write({
|
|
83
|
+
...file,
|
|
84
|
+
credentials: { ...file.credentials, [providerId]: sealed },
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async getCredential(password: string, providerId: string): Promise<string | undefined> {
|
|
89
|
+
const file = await this.requireUnlocked(password);
|
|
90
|
+
const sealed = file.credentials[providerId];
|
|
91
|
+
if (!sealed) return undefined;
|
|
92
|
+
return this.crypto.decrypt(sealed, password);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async listProviders(): Promise<readonly string[]> {
|
|
96
|
+
const file = await this.read();
|
|
97
|
+
return file ? Object.keys(file.credentials) : [];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async removeCredential(password: string, providerId: string): Promise<void> {
|
|
101
|
+
const file = await this.requireUnlocked(password);
|
|
102
|
+
const next = { ...file.credentials };
|
|
103
|
+
delete next[providerId];
|
|
104
|
+
await this.write({ ...file, credentials: next });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Wipe the whole vault — every stored credential and the password verifier.
|
|
109
|
+
* Used by a "forgot password" reset: the user loses all saved keys and then
|
|
110
|
+
* creates a fresh password. No password is required (the point is recovery
|
|
111
|
+
* when the password is lost).
|
|
112
|
+
*/
|
|
113
|
+
async reset(): Promise<void> {
|
|
114
|
+
this.file = undefined;
|
|
115
|
+
await this.storage.clear();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { readFile, writeFile, mkdir, rm } from "node:fs/promises";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import type { VaultStorage } from "./credential-vault";
|
|
5
|
-
|
|
6
|
-
/** Node file-backed vault storage (CLI + MCP). */
|
|
7
|
-
export class FileVaultStorage implements VaultStorage {
|
|
8
|
-
constructor(private readonly filePath: string) {}
|
|
9
|
-
|
|
10
|
-
async load(): Promise<string | null> {
|
|
11
|
-
if (!existsSync(this.filePath)) return null;
|
|
12
|
-
const raw = await readFile(this.filePath, "utf8");
|
|
13
|
-
return raw.trim() ? raw : null;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async save(serialized: string): Promise<void> {
|
|
17
|
-
const dir = path.dirname(this.filePath);
|
|
18
|
-
if (!existsSync(dir)) await mkdir(dir, { recursive: true });
|
|
19
|
-
await writeFile(this.filePath, serialized, { encoding: "utf8", mode: 0o600 });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async clear(): Promise<void> {
|
|
23
|
-
if (existsSync(this.filePath)) await rm(this.filePath, { force: true });
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
import { readFile, writeFile, mkdir, rm } from "node:fs/promises";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { VaultStorage } from "./credential-vault";
|
|
5
|
+
|
|
6
|
+
/** Node file-backed vault storage (CLI + MCP). */
|
|
7
|
+
export class FileVaultStorage implements VaultStorage {
|
|
8
|
+
constructor(private readonly filePath: string) {}
|
|
9
|
+
|
|
10
|
+
async load(): Promise<string | null> {
|
|
11
|
+
if (!existsSync(this.filePath)) return null;
|
|
12
|
+
const raw = await readFile(this.filePath, "utf8");
|
|
13
|
+
return raw.trim() ? raw : null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async save(serialized: string): Promise<void> {
|
|
17
|
+
const dir = path.dirname(this.filePath);
|
|
18
|
+
if (!existsSync(dir)) await mkdir(dir, { recursive: true });
|
|
19
|
+
await writeFile(this.filePath, serialized, { encoding: "utf8", mode: 0o600 });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async clear(): Promise<void> {
|
|
23
|
+
if (existsSync(this.filePath)) await rm(this.filePath, { force: true });
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/security/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { EncryptedCredentialVault } from "./credential-vault";
|
|
2
|
-
export type { VaultStorage } from "./credential-vault";
|
|
3
|
-
export { SessionPasswordGate } from "./password-gate";
|
|
4
|
-
export type { PasswordPrompt, SessionPasswordGateOptions } from "./password-gate";
|
|
5
|
-
export { FileVaultStorage } from "./file-vault-storage";
|
|
6
|
-
export { WebStorageVaultStorage } from "./web-storage-vault-storage";
|
|
7
|
-
export { migratePlaintextKey } from "./migrate-plaintext-key";
|
|
8
|
-
export type { PlaintextMigrationInput, PlaintextMigrationResult } from "./migrate-plaintext-key";
|
|
1
|
+
export { EncryptedCredentialVault } from "./credential-vault";
|
|
2
|
+
export type { VaultStorage } from "./credential-vault";
|
|
3
|
+
export { SessionPasswordGate } from "./password-gate";
|
|
4
|
+
export type { PasswordPrompt, SessionPasswordGateOptions } from "./password-gate";
|
|
5
|
+
export { FileVaultStorage } from "./file-vault-storage";
|
|
6
|
+
export { WebStorageVaultStorage } from "./web-storage-vault-storage";
|
|
7
|
+
export { migratePlaintextKey } from "./migrate-plaintext-key";
|
|
8
|
+
export type { PlaintextMigrationInput, PlaintextMigrationResult } from "./migrate-plaintext-key";
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import type { CredentialVault } from "../ports/security";
|
|
2
|
-
|
|
3
|
-
export interface PlaintextMigrationInput {
|
|
4
|
-
readonly vault: CredentialVault;
|
|
5
|
-
readonly password: string;
|
|
6
|
-
/** Catalog provider id the key belongs to (e.g. "anthropic"). */
|
|
7
|
-
readonly providerId: string;
|
|
8
|
-
/** The legacy plaintext secret to import. */
|
|
9
|
-
readonly plaintextKey: string;
|
|
10
|
-
/** Clears the legacy plaintext store once the key is sealed. */
|
|
11
|
-
readonly clearPlaintext: () => void | Promise<void>;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface PlaintextMigrationResult {
|
|
15
|
-
readonly migrated: boolean;
|
|
16
|
-
readonly initializedVault: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Move a legacy plaintext API key into the encrypted vault, then wipe the
|
|
21
|
-
* plaintext source. Initializes the vault on first run. Idempotent: a blank key
|
|
22
|
-
* is a no-op.
|
|
23
|
-
*/
|
|
24
|
-
export async function migratePlaintextKey(
|
|
25
|
-
input: PlaintextMigrationInput,
|
|
26
|
-
): Promise<PlaintextMigrationResult> {
|
|
27
|
-
if (!input.plaintextKey) {
|
|
28
|
-
return { migrated: false, initializedVault: false };
|
|
29
|
-
}
|
|
30
|
-
let initializedVault = false;
|
|
31
|
-
if (!(await input.vault.isInitialized())) {
|
|
32
|
-
await input.vault.initialize(input.password);
|
|
33
|
-
initializedVault = true;
|
|
34
|
-
}
|
|
35
|
-
await input.vault.setCredential(input.password, input.providerId, input.plaintextKey);
|
|
36
|
-
await input.clearPlaintext();
|
|
37
|
-
return { migrated: true, initializedVault };
|
|
38
|
-
}
|
|
1
|
+
import type { CredentialVault } from "../ports/security";
|
|
2
|
+
|
|
3
|
+
export interface PlaintextMigrationInput {
|
|
4
|
+
readonly vault: CredentialVault;
|
|
5
|
+
readonly password: string;
|
|
6
|
+
/** Catalog provider id the key belongs to (e.g. "anthropic"). */
|
|
7
|
+
readonly providerId: string;
|
|
8
|
+
/** The legacy plaintext secret to import. */
|
|
9
|
+
readonly plaintextKey: string;
|
|
10
|
+
/** Clears the legacy plaintext store once the key is sealed. */
|
|
11
|
+
readonly clearPlaintext: () => void | Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PlaintextMigrationResult {
|
|
15
|
+
readonly migrated: boolean;
|
|
16
|
+
readonly initializedVault: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Move a legacy plaintext API key into the encrypted vault, then wipe the
|
|
21
|
+
* plaintext source. Initializes the vault on first run. Idempotent: a blank key
|
|
22
|
+
* is a no-op.
|
|
23
|
+
*/
|
|
24
|
+
export async function migratePlaintextKey(
|
|
25
|
+
input: PlaintextMigrationInput,
|
|
26
|
+
): Promise<PlaintextMigrationResult> {
|
|
27
|
+
if (!input.plaintextKey) {
|
|
28
|
+
return { migrated: false, initializedVault: false };
|
|
29
|
+
}
|
|
30
|
+
let initializedVault = false;
|
|
31
|
+
if (!(await input.vault.isInitialized())) {
|
|
32
|
+
await input.vault.initialize(input.password);
|
|
33
|
+
initializedVault = true;
|
|
34
|
+
}
|
|
35
|
+
await input.vault.setCredential(input.password, input.providerId, input.plaintextKey);
|
|
36
|
+
await input.clearPlaintext();
|
|
37
|
+
return { migrated: true, initializedVault };
|
|
38
|
+
}
|
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import type { CredentialVault, PasswordGate, SensitiveOperation } from "../ports/security";
|
|
2
|
-
import { SecurityError } from "../errors/app-error";
|
|
3
|
-
|
|
4
|
-
/** Prompts the host environment for a password (CLI prompt, browser modal, …). */
|
|
5
|
-
export type PasswordPrompt = (context: {
|
|
6
|
-
operation: SensitiveOperation;
|
|
7
|
-
firstRun: boolean;
|
|
8
|
-
attempt: number;
|
|
9
|
-
}) => Promise<string>;
|
|
10
|
-
|
|
11
|
-
export interface SessionPasswordGateOptions {
|
|
12
|
-
readonly vault: CredentialVault;
|
|
13
|
-
readonly prompt: PasswordPrompt;
|
|
14
|
-
/** Max password attempts before authorize() throws. Default 3. */
|
|
15
|
-
readonly maxAttempts?: number;
|
|
16
|
-
/** Cache the verified password for the process lifetime. Default true. */
|
|
17
|
-
readonly cacheForSession?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Authorizes sensitive operations behind the local password. On first run it
|
|
22
|
-
* initializes the vault; subsequent calls verify (one unlock-per-session by
|
|
23
|
-
* default). Returns the verified password so callers can read the vault.
|
|
24
|
-
*/
|
|
25
|
-
export class SessionPasswordGate implements PasswordGate {
|
|
26
|
-
private cached?: string;
|
|
27
|
-
|
|
28
|
-
constructor(private readonly options: SessionPasswordGateOptions) {}
|
|
29
|
-
|
|
30
|
-
async authorize(operation: SensitiveOperation): Promise<string> {
|
|
31
|
-
if (this.options.cacheForSession !== false && this.cached) {
|
|
32
|
-
return this.cached;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const firstRun = !(await this.options.vault.isInitialized());
|
|
36
|
-
const maxAttempts = this.options.maxAttempts ?? 3;
|
|
37
|
-
|
|
38
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
39
|
-
const password = await this.options.prompt({ operation, firstRun, attempt });
|
|
40
|
-
|
|
41
|
-
if (firstRun) {
|
|
42
|
-
await this.options.vault.initialize(password);
|
|
43
|
-
return this.remember(password);
|
|
44
|
-
}
|
|
45
|
-
if (await this.options.vault.unlock(password)) {
|
|
46
|
-
return this.remember(password);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
throw new SecurityError(`Authorization failed for "${operation}" after ${maxAttempts} attempts.`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private remember(password: string): string {
|
|
54
|
-
if (this.options.cacheForSession !== false) this.cached = password;
|
|
55
|
-
return password;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Clears any cached password (e.g. on logout). */
|
|
59
|
-
reset(): void {
|
|
60
|
-
this.cached = undefined;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
1
|
+
import type { CredentialVault, PasswordGate, SensitiveOperation } from "../ports/security";
|
|
2
|
+
import { SecurityError } from "../errors/app-error";
|
|
3
|
+
|
|
4
|
+
/** Prompts the host environment for a password (CLI prompt, browser modal, …). */
|
|
5
|
+
export type PasswordPrompt = (context: {
|
|
6
|
+
operation: SensitiveOperation;
|
|
7
|
+
firstRun: boolean;
|
|
8
|
+
attempt: number;
|
|
9
|
+
}) => Promise<string>;
|
|
10
|
+
|
|
11
|
+
export interface SessionPasswordGateOptions {
|
|
12
|
+
readonly vault: CredentialVault;
|
|
13
|
+
readonly prompt: PasswordPrompt;
|
|
14
|
+
/** Max password attempts before authorize() throws. Default 3. */
|
|
15
|
+
readonly maxAttempts?: number;
|
|
16
|
+
/** Cache the verified password for the process lifetime. Default true. */
|
|
17
|
+
readonly cacheForSession?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Authorizes sensitive operations behind the local password. On first run it
|
|
22
|
+
* initializes the vault; subsequent calls verify (one unlock-per-session by
|
|
23
|
+
* default). Returns the verified password so callers can read the vault.
|
|
24
|
+
*/
|
|
25
|
+
export class SessionPasswordGate implements PasswordGate {
|
|
26
|
+
private cached?: string;
|
|
27
|
+
|
|
28
|
+
constructor(private readonly options: SessionPasswordGateOptions) {}
|
|
29
|
+
|
|
30
|
+
async authorize(operation: SensitiveOperation): Promise<string> {
|
|
31
|
+
if (this.options.cacheForSession !== false && this.cached) {
|
|
32
|
+
return this.cached;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const firstRun = !(await this.options.vault.isInitialized());
|
|
36
|
+
const maxAttempts = this.options.maxAttempts ?? 3;
|
|
37
|
+
|
|
38
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
39
|
+
const password = await this.options.prompt({ operation, firstRun, attempt });
|
|
40
|
+
|
|
41
|
+
if (firstRun) {
|
|
42
|
+
await this.options.vault.initialize(password);
|
|
43
|
+
return this.remember(password);
|
|
44
|
+
}
|
|
45
|
+
if (await this.options.vault.unlock(password)) {
|
|
46
|
+
return this.remember(password);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
throw new SecurityError(`Authorization failed for "${operation}" after ${maxAttempts} attempts.`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private remember(password: string): string {
|
|
54
|
+
if (this.options.cacheForSession !== false) this.cached = password;
|
|
55
|
+
return password;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Clears any cached password (e.g. on logout). */
|
|
59
|
+
reset(): void {
|
|
60
|
+
this.cached = undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import type { VaultStorage } from "./credential-vault";
|
|
2
|
-
import type { WebStorageLike } from "../cache/web-storage-cache";
|
|
3
|
-
|
|
4
|
-
/** Browser vault storage backed by a single Web Storage entry. */
|
|
5
|
-
export class WebStorageVaultStorage implements VaultStorage {
|
|
6
|
-
constructor(
|
|
7
|
-
private readonly storage: WebStorageLike,
|
|
8
|
-
private readonly key = "gitpagedocs:vault",
|
|
9
|
-
) {}
|
|
10
|
-
|
|
11
|
-
async load(): Promise<string | null> {
|
|
12
|
-
const raw = this.storage.getItem(this.key);
|
|
13
|
-
return raw && raw.trim() ? raw : null;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async save(serialized: string): Promise<void> {
|
|
17
|
-
this.storage.setItem(this.key, serialized);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async clear(): Promise<void> {
|
|
21
|
-
this.storage.removeItem(this.key);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
1
|
+
import type { VaultStorage } from "./credential-vault";
|
|
2
|
+
import type { WebStorageLike } from "../cache/web-storage-cache";
|
|
3
|
+
|
|
4
|
+
/** Browser vault storage backed by a single Web Storage entry. */
|
|
5
|
+
export class WebStorageVaultStorage implements VaultStorage {
|
|
6
|
+
constructor(
|
|
7
|
+
private readonly storage: WebStorageLike,
|
|
8
|
+
private readonly key = "gitpagedocs:vault",
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
async load(): Promise<string | null> {
|
|
12
|
+
const raw = this.storage.getItem(this.key);
|
|
13
|
+
return raw && raw.trim() ? raw : null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async save(serialized: string): Promise<void> {
|
|
17
|
+
this.storage.setItem(this.key, serialized);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async clear(): Promise<void> {
|
|
21
|
+
this.storage.removeItem(this.key);
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/security/web.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// Browser-safe security entry: encrypted vault + web storage + migration.
|
|
2
|
-
// Excludes FileVaultStorage (node:fs).
|
|
3
|
-
export { EncryptedCredentialVault } from "./credential-vault";
|
|
4
|
-
export type { VaultStorage } from "./credential-vault";
|
|
5
|
-
export { SessionPasswordGate } from "./password-gate";
|
|
6
|
-
export type { PasswordPrompt, SessionPasswordGateOptions } from "./password-gate";
|
|
7
|
-
export { WebStorageVaultStorage } from "./web-storage-vault-storage";
|
|
8
|
-
export { migratePlaintextKey } from "./migrate-plaintext-key";
|
|
9
|
-
export type { PlaintextMigrationInput, PlaintextMigrationResult } from "./migrate-plaintext-key";
|
|
1
|
+
// Browser-safe security entry: encrypted vault + web storage + migration.
|
|
2
|
+
// Excludes FileVaultStorage (node:fs).
|
|
3
|
+
export { EncryptedCredentialVault } from "./credential-vault";
|
|
4
|
+
export type { VaultStorage } from "./credential-vault";
|
|
5
|
+
export { SessionPasswordGate } from "./password-gate";
|
|
6
|
+
export type { PasswordPrompt, SessionPasswordGateOptions } from "./password-gate";
|
|
7
|
+
export { WebStorageVaultStorage } from "./web-storage-vault-storage";
|
|
8
|
+
export { migratePlaintextKey } from "./migrate-plaintext-key";
|
|
9
|
+
export type { PlaintextMigrationInput, PlaintextMigrationResult } from "./migrate-plaintext-key";
|