@fadhilp/stateql 0.18.0 → 0.19.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/dist/src/cli.js +8 -3
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/os-credentials.d.ts +6 -1
- package/dist/src/os-credentials.js +171 -10
- package/package.json +1 -1
package/dist/src/cli.js
CHANGED
|
@@ -7,7 +7,7 @@ import { BSON } from "mongodb";
|
|
|
7
7
|
import { exitCodeFor } from "./errors.js";
|
|
8
8
|
import { StateQL } from "./stateql.js";
|
|
9
9
|
import { agentResponse } from "./agent-response.js";
|
|
10
|
-
import { askPassword, cliCredentialResolver, deleteOsPassword, saveOsPassword } from "./os-credentials.js";
|
|
10
|
+
import { askPassword, cliCredentialResolver, createOsPasswordRef, deleteOsPassword, saveOsPassword } from "./os-credentials.js";
|
|
11
11
|
const USAGE = `stql connect <target> [--name NAME] [--read-only|--read-write]
|
|
12
12
|
stql connect --env ENV | --credential-ref REF | --profile NAME [--name NAME] [--read-only|--read-write]
|
|
13
13
|
stql disconnect|status
|
|
@@ -397,6 +397,11 @@ async function dispatchSession(action, args) {
|
|
|
397
397
|
throw new Error(`Unknown session command "${action ?? ""}".`);
|
|
398
398
|
}
|
|
399
399
|
}
|
|
400
|
+
async function askAndSavePassword(profile, target) {
|
|
401
|
+
const reference = createOsPasswordRef();
|
|
402
|
+
await saveOsPassword(reference, target, await askPassword(profile, target));
|
|
403
|
+
return reference;
|
|
404
|
+
}
|
|
400
405
|
async function dispatchProfile(action, args) {
|
|
401
406
|
switch (action) {
|
|
402
407
|
case "add": {
|
|
@@ -405,7 +410,7 @@ async function dispatchProfile(action, args) {
|
|
|
405
410
|
}
|
|
406
411
|
const name = requireValue(args[0], "profile name");
|
|
407
412
|
const passwordRef = values["ask-password"]
|
|
408
|
-
? await
|
|
413
|
+
? await askAndSavePassword(name, requireValue(args[1], "TARGET for --ask-password"))
|
|
409
414
|
: undefined;
|
|
410
415
|
const added = await stateql.addProfile(name, args[1], {
|
|
411
416
|
...(values.env ? { secretEnv: values.env } : {}),
|
|
@@ -426,7 +431,7 @@ async function dispatchProfile(action, args) {
|
|
|
426
431
|
return before;
|
|
427
432
|
const target = args[1] ?? before.data.target;
|
|
428
433
|
const passwordRef = values["ask-password"]
|
|
429
|
-
? await
|
|
434
|
+
? await askAndSavePassword(name, requireValue(target ?? undefined, "TARGET for --ask-password"))
|
|
430
435
|
: undefined;
|
|
431
436
|
const updated = await stateql.updateProfile(name, {
|
|
432
437
|
...(args[1] !== undefined ? { target: args[1] } : {}),
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { StateQL } from "./stateql.js";
|
|
2
2
|
export { agentResponse } from "./agent-response.js";
|
|
3
|
+
export { createOsPasswordRef, deleteOsPassword, invalidateOsPassword, isOsPasswordRef, readOsPassword, saveOsPassword, } from "./os-credentials.js";
|
|
3
4
|
export { CredentialResolutionError, StateQLError, exitCodeFor, } from "./errors.js";
|
|
4
5
|
export type { CredentialResolutionFailure } from "./errors.js";
|
|
5
6
|
export type { TableChange, TableIdentity, TableUpdate } from "./table-editor.js";
|
package/dist/src/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { StateQL } from "./stateql.js";
|
|
2
2
|
export { agentResponse } from "./agent-response.js";
|
|
3
|
+
export { createOsPasswordRef, deleteOsPassword, invalidateOsPassword, isOsPasswordRef, readOsPassword, saveOsPassword, } from "./os-credentials.js";
|
|
3
4
|
export { CredentialResolutionError, StateQLError, exitCodeFor, } from "./errors.js";
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { CredentialRequest } from "./types.js";
|
|
2
2
|
export declare function isOsPasswordRef(reference: string | null | undefined): reference is string;
|
|
3
|
+
export declare function createOsPasswordRef(): string;
|
|
3
4
|
export declare function cliCredentialResolver(request: CredentialRequest): Promise<string | undefined>;
|
|
4
|
-
export declare function saveOsPassword(password: string): Promise<
|
|
5
|
+
export declare function saveOsPassword(reference: string, target: string, password: string): Promise<void>;
|
|
6
|
+
/** Returns the password only when it was saved for the same destination as `target`. */
|
|
7
|
+
export declare function readOsPassword(reference: string, target: string): Promise<string | undefined>;
|
|
8
|
+
/** Marks a saved password as rejected by the database, so it is not used again. */
|
|
9
|
+
export declare function invalidateOsPassword(reference: string): Promise<boolean>;
|
|
5
10
|
export declare function deleteOsPassword(reference: string | null | undefined): Promise<void>;
|
|
6
11
|
export declare function askPassword(profile: string, target: string): Promise<string>;
|
|
@@ -1,35 +1,196 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
3
|
import { env, platform } from "node:process";
|
|
4
4
|
import { promisify } from "node:util";
|
|
5
5
|
const SERVICE = "stql";
|
|
6
6
|
const PREFIX = "os:";
|
|
7
|
+
const MAX_TARGET_LENGTH = 8_192;
|
|
8
|
+
const MAX_PAYLOAD_LENGTH = 16_384;
|
|
9
|
+
const MAX_PASSWORD_LENGTH = 4_096;
|
|
10
|
+
const FINGERPRINT_PATTERN = /^[0-9a-f]{64}$/u;
|
|
11
|
+
const PROTOCOLS = ["postgres:", "mysql:", "mongodb:", "mongodb+srv:", "redis:", "rediss:"];
|
|
12
|
+
const TARGET_OVERRIDE_KEYS = new Set(["password", "pwd", "user", "username", "host", "port", "database", "dbname"]);
|
|
13
|
+
// TLS settings are connection options, not destination identity. pylon_tls_ca is Pylon's CA preset.
|
|
14
|
+
const TLS_QUERY_KEYS = new Set([
|
|
15
|
+
"sslmode",
|
|
16
|
+
"ssl",
|
|
17
|
+
"sslrootcert",
|
|
18
|
+
"sslcert",
|
|
19
|
+
"sslkey",
|
|
20
|
+
"rejectunauthorized",
|
|
21
|
+
"uselibpqcompat",
|
|
22
|
+
"sslnegotiation",
|
|
23
|
+
"pylon_tls_ca",
|
|
24
|
+
]);
|
|
7
25
|
const run = promisify(execFile);
|
|
8
26
|
export function isOsPasswordRef(reference) {
|
|
9
27
|
return reference?.startsWith(PREFIX) ?? false;
|
|
10
28
|
}
|
|
29
|
+
export function createOsPasswordRef() {
|
|
30
|
+
return `${PREFIX}${randomUUID()}`;
|
|
31
|
+
}
|
|
11
32
|
export async function cliCredentialResolver(request) {
|
|
12
33
|
if (request.source === "credential_ref")
|
|
13
34
|
return undefined;
|
|
14
35
|
if (request.source !== "password_ref")
|
|
15
36
|
return env[request.reference];
|
|
16
|
-
|
|
37
|
+
return readOsPassword(request.reference, request.target);
|
|
38
|
+
}
|
|
39
|
+
export async function saveOsPassword(reference, target, password) {
|
|
40
|
+
if (!isOsPasswordRef(reference))
|
|
41
|
+
throw new Error("Password reference must start with os:.");
|
|
42
|
+
if (!validPassword(password))
|
|
43
|
+
throw new Error("Password is too long or contains control characters.");
|
|
44
|
+
const fingerprint = targetFingerprint(target);
|
|
45
|
+
if (!fingerprint)
|
|
46
|
+
throw new Error("Cannot save a password for this target.");
|
|
47
|
+
const stored = { version: 2, fingerprint, password };
|
|
48
|
+
await (await osEntry(reference)).setPassword(JSON.stringify(stored));
|
|
49
|
+
}
|
|
50
|
+
/** Returns the password only when it was saved for the same destination as `target`. */
|
|
51
|
+
export async function readOsPassword(reference, target) {
|
|
52
|
+
if (!isOsPasswordRef(reference))
|
|
53
|
+
return undefined;
|
|
54
|
+
const value = await (await osEntry(reference)).getPassword();
|
|
55
|
+
if (value === null)
|
|
17
56
|
return undefined;
|
|
18
|
-
|
|
57
|
+
if (!isRecord(value))
|
|
58
|
+
return value; // Raw password saved before passwords were bound to a target.
|
|
59
|
+
const stored = storedPassword(value);
|
|
60
|
+
if (!stored || stored.stale)
|
|
61
|
+
return undefined;
|
|
62
|
+
return stored.fingerprint === targetFingerprint(target) ? stored.password : undefined;
|
|
19
63
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
64
|
+
/** Marks a saved password as rejected by the database, so it is not used again. */
|
|
65
|
+
export async function invalidateOsPassword(reference) {
|
|
66
|
+
if (!isOsPasswordRef(reference))
|
|
67
|
+
return false;
|
|
68
|
+
const entry = await osEntry(reference);
|
|
69
|
+
const stored = storedPassword((await entry.getPassword()) ?? "");
|
|
70
|
+
if (!stored)
|
|
71
|
+
return false;
|
|
72
|
+
await entry.setPassword(JSON.stringify({ ...stored, stale: true }));
|
|
73
|
+
return true;
|
|
24
74
|
}
|
|
25
75
|
export async function deleteOsPassword(reference) {
|
|
26
76
|
if (!isOsPasswordRef(reference))
|
|
27
77
|
return;
|
|
28
|
-
(await osEntry(reference)).
|
|
78
|
+
await (await osEntry(reference)).deleteCredential();
|
|
29
79
|
}
|
|
30
80
|
async function osEntry(reference) {
|
|
31
|
-
const {
|
|
32
|
-
|
|
81
|
+
const { AsyncEntry } = await import("@napi-rs/keyring");
|
|
82
|
+
// The keyutils fallback loses saved passwords after reboot.
|
|
83
|
+
return new AsyncEntry(SERVICE, reference.slice(PREFIX.length), { linux: { store: "secret-service" } });
|
|
84
|
+
}
|
|
85
|
+
function isRecord(value) {
|
|
86
|
+
try {
|
|
87
|
+
const parsed = JSON.parse(value);
|
|
88
|
+
return typeof parsed === "object" && parsed !== null && "version" in parsed;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function storedPassword(value) {
|
|
95
|
+
if (!value || value.length > MAX_PAYLOAD_LENGTH)
|
|
96
|
+
return undefined;
|
|
97
|
+
let record;
|
|
98
|
+
try {
|
|
99
|
+
const parsed = JSON.parse(value);
|
|
100
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
101
|
+
return undefined;
|
|
102
|
+
record = parsed;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
const knownKeys = Object.keys(record).every((key) => ["version", "fingerprint", "password", "stale"].includes(key));
|
|
108
|
+
if (!knownKeys || record.version !== 2)
|
|
109
|
+
return undefined;
|
|
110
|
+
if (typeof record.fingerprint !== "string" || !FINGERPRINT_PATTERN.test(record.fingerprint))
|
|
111
|
+
return undefined;
|
|
112
|
+
if (!validPassword(record.password))
|
|
113
|
+
return undefined;
|
|
114
|
+
if (record.stale !== undefined && record.stale !== true)
|
|
115
|
+
return undefined;
|
|
116
|
+
return {
|
|
117
|
+
version: 2,
|
|
118
|
+
fingerprint: record.fingerprint,
|
|
119
|
+
password: record.password,
|
|
120
|
+
...(record.stale ? { stale: true } : {}),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function validPassword(password) {
|
|
124
|
+
return typeof password === "string" && password.length <= MAX_PASSWORD_LENGTH && !hasControlCharacters(password);
|
|
125
|
+
}
|
|
126
|
+
function hasControlCharacters(value) {
|
|
127
|
+
return /[\u0000-\u001f\u007f]/u.test(value);
|
|
128
|
+
}
|
|
129
|
+
function targetFingerprint(target) {
|
|
130
|
+
const unsafeTarget = !target || target.length > MAX_TARGET_LENGTH || hasControlCharacters(target);
|
|
131
|
+
if (unsafeTarget || /%(?![0-9a-f]{2})/iu.test(target))
|
|
132
|
+
return undefined;
|
|
133
|
+
// WHATWG URL rejects multi-host MongoDB targets; bind those to the exact target instead.
|
|
134
|
+
const identity = parsesAsUrl(target) ? destinationIdentity(target) : target;
|
|
135
|
+
return identity ? createHash("sha256").update(identity).digest("hex") : undefined;
|
|
136
|
+
}
|
|
137
|
+
function parsesAsUrl(target) {
|
|
138
|
+
try {
|
|
139
|
+
new URL(target);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function destinationIdentity(target) {
|
|
147
|
+
const url = new URL(target);
|
|
148
|
+
const protocol = url.protocol.toLowerCase() === "postgresql:" ? "postgres:" : url.protocol.toLowerCase();
|
|
149
|
+
if (!PROTOCOLS.includes(protocol) || !url.hostname || url.hash || url.password)
|
|
150
|
+
return undefined;
|
|
151
|
+
const username = decodedPart(url.username);
|
|
152
|
+
const database = decodedPart(url.pathname);
|
|
153
|
+
if (username === undefined || database === undefined)
|
|
154
|
+
return undefined;
|
|
155
|
+
const query = [];
|
|
156
|
+
for (const [key, value] of url.searchParams.entries()) {
|
|
157
|
+
const normalizedKey = key.toLowerCase();
|
|
158
|
+
if (hasControlCharacters(key) || hasControlCharacters(value))
|
|
159
|
+
return undefined;
|
|
160
|
+
if (TARGET_OVERRIDE_KEYS.has(normalizedKey))
|
|
161
|
+
return undefined;
|
|
162
|
+
if (!TLS_QUERY_KEYS.has(normalizedKey))
|
|
163
|
+
query.push([key, value]);
|
|
164
|
+
}
|
|
165
|
+
query.sort(([leftKey, leftValue], [rightKey, rightValue]) => leftKey === rightKey ? leftValue.localeCompare(rightValue) : leftKey.localeCompare(rightKey));
|
|
166
|
+
return JSON.stringify({
|
|
167
|
+
protocol,
|
|
168
|
+
username,
|
|
169
|
+
hostname: url.hostname.toLowerCase().replace(/\.$/u, ""),
|
|
170
|
+
port: url.port || defaultPort(protocol),
|
|
171
|
+
database,
|
|
172
|
+
query,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function decodedPart(value) {
|
|
176
|
+
try {
|
|
177
|
+
const decoded = decodeURIComponent(value);
|
|
178
|
+
return hasControlCharacters(decoded) ? undefined : decoded;
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function defaultPort(protocol) {
|
|
185
|
+
if (protocol === "postgres:")
|
|
186
|
+
return "5432";
|
|
187
|
+
if (protocol === "mysql:")
|
|
188
|
+
return "3306";
|
|
189
|
+
if (protocol === "mongodb:")
|
|
190
|
+
return "27017";
|
|
191
|
+
if (protocol === "redis:" || protocol === "rediss:")
|
|
192
|
+
return "6379";
|
|
193
|
+
return "";
|
|
33
194
|
}
|
|
34
195
|
export async function askPassword(profile, target) {
|
|
35
196
|
const url = new URL(target);
|