@conarium-ai/core 0.2.6 → 0.2.8
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/CHANGELOG.md +31 -1
- package/README.md +34 -0
- package/SECURITY.md +79 -41
- package/bin/conarium-console.mjs +86 -4
- package/bin/conarium-init.mjs +1 -1
- package/dist/console-handoff.d.ts +24 -0
- package/dist/console-handoff.d.ts.map +1 -0
- package/dist/console-handoff.js +49 -0
- package/dist/console-handoff.js.map +1 -0
- package/dist/console-notify.d.ts +6 -0
- package/dist/console-notify.d.ts.map +1 -0
- package/dist/console-notify.js +41 -0
- package/dist/console-notify.js.map +1 -0
- package/dist/console-ready.d.ts +5 -0
- package/dist/console-ready.d.ts.map +1 -0
- package/dist/console-ready.js +27 -0
- package/dist/console-ready.js.map +1 -0
- package/dist/console-receipts.d.ts +30 -0
- package/dist/console-receipts.d.ts.map +1 -0
- package/dist/console-receipts.js +137 -0
- package/dist/console-receipts.js.map +1 -0
- package/dist/console-shortcut.d.ts +49 -0
- package/dist/console-shortcut.d.ts.map +1 -0
- package/dist/console-shortcut.js +176 -0
- package/dist/console-shortcut.js.map +1 -0
- package/dist/console.d.ts +6 -0
- package/dist/console.d.ts.map +1 -1
- package/dist/console.js +150 -19
- package/dist/console.js.map +1 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +9 -0
- package/dist/http.js.map +1 -1
- package/dist/keys.js +1 -1
- package/dist/keys.js.map +1 -1
- package/dist/receipt-view.d.ts +89 -0
- package/dist/receipt-view.d.ts.map +1 -0
- package/dist/receipt-view.js +317 -0
- package/dist/receipt-view.js.map +1 -0
- package/dist/receipt.d.ts +14 -0
- package/dist/receipt.d.ts.map +1 -1
- package/dist/receipt.js +39 -0
- package/dist/receipt.js.map +1 -1
- package/examples/per-user-identity/mint-token.mjs +3 -2
- package/package.json +2 -2
- package/public/app.js +119 -7
- package/public/index.html +28 -0
- package/public/style.css +16 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load the operator's receipt sink for the console. No invented receipts.
|
|
3
|
+
*/
|
|
4
|
+
import { createPublicKey } from 'node:crypto';
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
6
|
+
import { basename, dirname, isAbsolute, resolve } from 'node:path';
|
|
7
|
+
import { verifyReceiptChain, } from './receipt.js';
|
|
8
|
+
import { parseTrustPubkeyPaths } from './keys.js';
|
|
9
|
+
export const EMPTY_RECEIPTS_MESSAGE = 'henüz makbuz yok. Gateway `audit.receiptSink` ile çalışıyorsa her erişim bir satır yazar.';
|
|
10
|
+
export const NO_SINK_MESSAGE = 'henüz makbuz yok. `conarium.config.json` içinde `audit.receiptSink` yok; denetim kaydı makbuz değildir.';
|
|
11
|
+
export function resolveReceiptSink(configFile) {
|
|
12
|
+
try {
|
|
13
|
+
if (!existsSync(configFile))
|
|
14
|
+
return null;
|
|
15
|
+
const raw = JSON.parse(readFileSync(configFile, 'utf8'));
|
|
16
|
+
const sink = raw?.audit?.receiptSink;
|
|
17
|
+
if (typeof sink !== 'string' || !sink.trim())
|
|
18
|
+
return null;
|
|
19
|
+
return isAbsolute(sink) ? sink : resolve(dirname(configFile), sink);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function readReceiptJsonl(file) {
|
|
26
|
+
if (!existsSync(file))
|
|
27
|
+
return { receipts: [] };
|
|
28
|
+
const lines = readFileSync(file, 'utf8').split(/\r?\n/);
|
|
29
|
+
const receipts = [];
|
|
30
|
+
let seen = 0;
|
|
31
|
+
for (const raw of lines) {
|
|
32
|
+
const line = raw.trim();
|
|
33
|
+
if (!line)
|
|
34
|
+
continue;
|
|
35
|
+
seen += 1;
|
|
36
|
+
try {
|
|
37
|
+
receipts.push(JSON.parse(line));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return { receipts, parseBreak: seen };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { receipts };
|
|
44
|
+
}
|
|
45
|
+
export function loadReceiptsForConsole(configFile) {
|
|
46
|
+
const sink = resolveReceiptSink(configFile);
|
|
47
|
+
if (!sink) {
|
|
48
|
+
return {
|
|
49
|
+
sink: null,
|
|
50
|
+
receipts: [],
|
|
51
|
+
chain: { ok: true, entries: 0 },
|
|
52
|
+
emptyMessage: NO_SINK_MESSAGE,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (!existsSync(sink)) {
|
|
56
|
+
return {
|
|
57
|
+
sink,
|
|
58
|
+
receipts: [],
|
|
59
|
+
chain: { ok: true, entries: 0 },
|
|
60
|
+
emptyMessage: EMPTY_RECEIPTS_MESSAGE,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const { receipts, parseBreak } = readReceiptJsonl(sink);
|
|
64
|
+
if (parseBreak != null) {
|
|
65
|
+
return {
|
|
66
|
+
sink,
|
|
67
|
+
receipts,
|
|
68
|
+
chain: { ok: false, brokenAt: parseBreak, reason: 'unreadable', entries: receipts.length },
|
|
69
|
+
emptyMessage: receipts.length === 0 ? EMPTY_RECEIPTS_MESSAGE : null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const chain = verifyReceiptChain(receipts);
|
|
73
|
+
return {
|
|
74
|
+
sink,
|
|
75
|
+
receipts,
|
|
76
|
+
chain,
|
|
77
|
+
emptyMessage: receipts.length === 0 ? EMPTY_RECEIPTS_MESSAGE : null,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export function toListItems(receipts) {
|
|
81
|
+
return receipts
|
|
82
|
+
.map((r) => ({
|
|
83
|
+
id: String(r.id || ''),
|
|
84
|
+
ts: String(r.ts || ''),
|
|
85
|
+
actor: r.actor?.id || 'unknown',
|
|
86
|
+
tool: r.request?.tool || '',
|
|
87
|
+
decision: r.policy?.decision || '',
|
|
88
|
+
maskedCount: Number(r.masking?.maskedCount || 0),
|
|
89
|
+
seq: Number(r.chain?.seq || 0),
|
|
90
|
+
}))
|
|
91
|
+
.reverse();
|
|
92
|
+
}
|
|
93
|
+
export function findReceipt(receipts, id) {
|
|
94
|
+
return receipts.find((r) => r.id === id) ?? null;
|
|
95
|
+
}
|
|
96
|
+
/** Public PEM only. Never returns a private key block. */
|
|
97
|
+
export function publicPemForPanel() {
|
|
98
|
+
for (const p of parseTrustPubkeyPaths()) {
|
|
99
|
+
if (!existsSync(p))
|
|
100
|
+
continue;
|
|
101
|
+
const pem = readFileSync(p, 'utf8');
|
|
102
|
+
if (/BEGIN PRIVATE KEY|BEGIN.*PRIVATE/.test(pem))
|
|
103
|
+
continue;
|
|
104
|
+
if (/BEGIN PUBLIC KEY/.test(pem))
|
|
105
|
+
return pem;
|
|
106
|
+
}
|
|
107
|
+
const priv = process.env.CONARIUM_AUDIT_SIGNING_KEY?.trim();
|
|
108
|
+
if (!priv)
|
|
109
|
+
return null;
|
|
110
|
+
const candidates = [priv.replace(/\.pem$/i, '.pub.pem'), `${priv}.pub.pem`];
|
|
111
|
+
for (const c of candidates) {
|
|
112
|
+
if (c === priv || !existsSync(c))
|
|
113
|
+
continue;
|
|
114
|
+
const pem = readFileSync(c, 'utf8');
|
|
115
|
+
if (/BEGIN PRIVATE KEY|BEGIN.*PRIVATE/.test(pem))
|
|
116
|
+
continue;
|
|
117
|
+
if (/BEGIN PUBLIC KEY/.test(pem))
|
|
118
|
+
return pem;
|
|
119
|
+
}
|
|
120
|
+
if (!existsSync(priv))
|
|
121
|
+
return null;
|
|
122
|
+
try {
|
|
123
|
+
const raw = readFileSync(priv, 'utf8');
|
|
124
|
+
const pub = createPublicKey(raw).export({ type: 'spki', format: 'pem' }).toString();
|
|
125
|
+
if (/BEGIN PRIVATE KEY|BEGIN.*PRIVATE/.test(pub))
|
|
126
|
+
return null;
|
|
127
|
+
return pub;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export function verifyCommandFor(sink) {
|
|
134
|
+
const file = sink ? basename(sink) : 'conarium-receipts.jsonl';
|
|
135
|
+
return `npx conarium-verify ${file} --pubkey <key.pub.pem>`;
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=console-receipts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console-receipts.js","sourceRoot":"","sources":["../src/console-receipts.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAClE,OAAO,EAGL,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAA;AAEjD,MAAM,CAAC,MAAM,sBAAsB,GACjC,2FAA2F,CAAA;AAE7F,MAAM,CAAC,MAAM,eAAe,GAC1B,yGAAyG,CAAA;AAmB3G,MAAM,UAAU,kBAAkB,CAAC,UAAkB;IACnD,IAAI,CAAC;QACH,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;YAAE,OAAO,IAAI,CAAA;QACxC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAEtD,CAAA;QACD,MAAM,IAAI,GAAG,GAAG,EAAE,KAAK,EAAE,WAAW,CAAA;QACpC,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YAAE,OAAO,IAAI,CAAA;QACzD,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,CAAA;IACrE,CAAC;IAAC,MAAM,CAAC;Q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C;YAAE,OAAO,GAAG,CAAA;IAC9C,CAAC;IACD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAA;IAClC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QACtC,MAAM,GAAG,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAA;QACnF,IAAI,kCAAkC,CAAC,IAAI,CAAC,GAAG,CAAC;YAAE,OAAO,IAAI,CAAA;QAC7D,OAAO,GAAG,CAAA;IACZ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAmB;IAClD,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,yBAAyB,CAAA;IAC9D,OAAO,uBAAuB,IAAI,yBAAyB,CAAA;AAC7D,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export declare const SHORTCUT_NAME = "Conarium Console";
|
|
2
|
+
export declare const STATE_REL: string;
|
|
3
|
+
export declare const TOKEN_REL: string;
|
|
4
|
+
export interface ShortcutPlan {
|
|
5
|
+
dest: string;
|
|
6
|
+
kind: 'lnk' | 'app' | 'desktop';
|
|
7
|
+
}
|
|
8
|
+
export interface ShortcutState {
|
|
9
|
+
path: string;
|
|
10
|
+
workdir: string;
|
|
11
|
+
kind: ShortcutPlan['kind'];
|
|
12
|
+
}
|
|
13
|
+
export declare function nextAvailablePath(preferred: string, exists?: (p: string) => boolean): string;
|
|
14
|
+
export declare function planShortcut(platform: NodeJS.Platform, home: string): ShortcutPlan;
|
|
15
|
+
export declare function writeBorn0600(file: string, contents: string): void;
|
|
16
|
+
export declare function windowsShortcutScript(opts: {
|
|
17
|
+
dest: string;
|
|
18
|
+
nodePath: string;
|
|
19
|
+
scriptPath: string;
|
|
20
|
+
workdir: string;
|
|
21
|
+
}): string;
|
|
22
|
+
export declare function macLauncherScript(nodePath: string, scriptPath: string): string;
|
|
23
|
+
export declare function macInfoPlist(): string;
|
|
24
|
+
export declare function linuxDesktopFile(opts: {
|
|
25
|
+
nodePath: string;
|
|
26
|
+
scriptPath: string;
|
|
27
|
+
workdir: string;
|
|
28
|
+
}): string;
|
|
29
|
+
export declare function installShortcut(opts: {
|
|
30
|
+
platform?: NodeJS.Platform;
|
|
31
|
+
home?: string;
|
|
32
|
+
nodePath?: string;
|
|
33
|
+
scriptPath: string;
|
|
34
|
+
workdir: string;
|
|
35
|
+
token?: string;
|
|
36
|
+
runWindows?: (script: string) => void;
|
|
37
|
+
}): {
|
|
38
|
+
dest: string;
|
|
39
|
+
kind: ShortcutPlan['kind'];
|
|
40
|
+
collided: boolean;
|
|
41
|
+
};
|
|
42
|
+
export declare function uninstallShortcut(opts?: {
|
|
43
|
+
home?: string;
|
|
44
|
+
}): {
|
|
45
|
+
removed: string | null;
|
|
46
|
+
};
|
|
47
|
+
export declare function loadLaunchToken(home?: string): string | null;
|
|
48
|
+
export declare function loadShortcutState(home?: string): ShortcutState | null;
|
|
49
|
+
//# sourceMappingURL=console-shortcut.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console-shortcut.d.ts","sourceRoot":"","sources":["../src/console-shortcut.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,aAAa,qBAAqB,CAAA;AAC/C,eAAO,MAAM,SAAS,QAA6C,CAAA;AACnE,eAAO,MAAM,SAAS,QAAqC,CAAA;AAE3D,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAA;CAChC;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CA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AC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAqClE;AAED,wBAAgB,iBAAiB,CAAC,IAAI,GAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG;IAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAiB1F;AAED,wBAAgB,eAAe,CAAC,IAAI,SAAmD,GAAG,MAAM,GAAG,IAAI,CAOtG;AAED,wBAAgB,iBAAiB,CAAC,IAAI,SAAmD,GAAG,aAAa,GAAG,IAAI,CAQ/G"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Desktop shortcut for conarium-console. The shortcut holds a command,
|
|
3
|
+
* never a token.
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, mkdirSync, writeFileSync, chmodSync, readFileSync, rmSync, unlinkSync } from 'node:fs';
|
|
6
|
+
import { homedir as osHomedir } from 'node:os';
|
|
7
|
+
import { dirname, join } from 'node:path';
|
|
8
|
+
import { spawnSync } from 'node:child_process';
|
|
9
|
+
export const SHORTCUT_NAME = 'Conarium Console';
|
|
10
|
+
export const STATE_REL = join('.conarium', 'console-shortcut.json');
|
|
11
|
+
export const TOKEN_REL = join('.conarium', 'console.token');
|
|
12
|
+
export function nextAvailablePath(preferred, exists = existsSync) {
|
|
13
|
+
if (!exists(preferred))
|
|
14
|
+
return preferred;
|
|
15
|
+
const m = preferred.match(/^(.*?)(\.[^./\\]+)?$/);
|
|
16
|
+
const stem = m?.[1] ?? preferred;
|
|
17
|
+
const ext = m?.[2] ?? '';
|
|
18
|
+
for (let n = 2; n < 100; n++) {
|
|
19
|
+
const cand = `${stem}-${n}${ext}`;
|
|
20
|
+
if (!exists(cand))
|
|
21
|
+
return cand;
|
|
22
|
+
}
|
|
23
|
+
throw new Error('could not find a free shortcut name');
|
|
24
|
+
}
|
|
25
|
+
export function planShortcut(platform, home) {
|
|
26
|
+
if (platform === 'win32') {
|
|
27
|
+
return { dest: join(home, 'Desktop', `${SHORTCUT_NAME}.lnk`), kind: 'lnk' };
|
|
28
|
+
}
|
|
29
|
+
if (platform === 'darwin') {
|
|
30
|
+
return { dest: join(home, 'Applications', `${SHORTCUT_NAME}.app`), kind: 'app' };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
dest: join(home, '.local', 'share', 'applications', 'conarium-console.desktop'),
|
|
34
|
+
kind: 'desktop',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function writeBorn0600(file, contents) {
|
|
38
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
39
|
+
writeFileSync(file, contents, { encoding: 'utf8', mode: 0o600 });
|
|
40
|
+
try {
|
|
41
|
+
chmodSync(file, 0o600);
|
|
42
|
+
}
|
|
43
|
+
catch { /* windows */ }
|
|
44
|
+
}
|
|
45
|
+
export function windowsShortcutScript(opts) {
|
|
46
|
+
const q = (s) => s.replace(/'/g, "''");
|
|
47
|
+
return [
|
|
48
|
+
`$ws = New-Object -ComObject WScript.Shell`,
|
|
49
|
+
`$s = $ws.CreateShortcut('${q(opts.dest)}')`,
|
|
50
|
+
`$s.TargetPath = '${q(opts.nodePath)}'`,
|
|
51
|
+
`$s.Arguments = '"${q(opts.scriptPath)}" --launch'`,
|
|
52
|
+
`$s.WorkingDirectory = '${q(opts.workdir)}'`,
|
|
53
|
+
`$s.WindowStyle = 7`,
|
|
54
|
+
`$s.Description = 'Conarium Console'`,
|
|
55
|
+
`$s.Save()`,
|
|
56
|
+
].join('\n');
|
|
57
|
+
}
|
|
58
|
+
export function macLauncherScript(nodePath, scriptPath) {
|
|
59
|
+
return `#!/bin/sh\nexec "${nodePath.replace(/"/g, '\\"')}" "${scriptPath.replace(/"/g, '\\"')}" --launch\n`;
|
|
60
|
+
}
|
|
61
|
+
export function macInfoPlist() {
|
|
62
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
63
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
64
|
+
<plist version="1.0">
|
|
65
|
+
<dict>
|
|
66
|
+
<key>CFBundleName</key><string>Conarium Console</string>
|
|
67
|
+
<key>CFBundleIdentifier</key><string>dev.conarium.console</string>
|
|
68
|
+
<key>CFBundleVersion</key><string>1</string>
|
|
69
|
+
<key>CFBundleExecutable</key><string>launcher</string>
|
|
70
|
+
<key>CFBundlePackageType</key><string>APPL</string>
|
|
71
|
+
</dict>
|
|
72
|
+
</plist>
|
|
73
|
+
`;
|
|
74
|
+
}
|
|
75
|
+
export function linuxDesktopFile(opts) {
|
|
76
|
+
return [
|
|
77
|
+
'[Desktop Entry]',
|
|
78
|
+
'Type=Application',
|
|
79
|
+
'Name=Conarium Console',
|
|
80
|
+
`Exec="${opts.nodePath}" "${opts.scriptPath}" --launch`,
|
|
81
|
+
`Path=${opts.workdir}`,
|
|
82
|
+
'Terminal=false',
|
|
83
|
+
'Categories=Utility;',
|
|
84
|
+
'',
|
|
85
|
+
].join('\n');
|
|
86
|
+
}
|
|
87
|
+
export function installShortcut(opts) {
|
|
88
|
+
const platform = opts.platform ?? process.platform;
|
|
89
|
+
const home = opts.home ?? process.env.CONARIUM_CONSOLE_HOME ?? osHomedir();
|
|
90
|
+
const nodePath = opts.nodePath ?? process.execPath;
|
|
91
|
+
const planned = planShortcut(platform, home);
|
|
92
|
+
const dest = nextAvailablePath(planned.dest);
|
|
93
|
+
const collided = dest !== planned.dest;
|
|
94
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
95
|
+
if (planned.kind === 'lnk') {
|
|
96
|
+
const script = windowsShortcutScript({ dest, nodePath, scriptPath: opts.scriptPath, workdir: opts.workdir });
|
|
97
|
+
if (script.includes(opts.token || '\u0000') && opts.token) {
|
|
98
|
+
throw new Error('shortcut script must not contain the token');
|
|
99
|
+
}
|
|
100
|
+
const run = opts.runWindows ?? ((ps) => {
|
|
101
|
+
const r = spawnSync('powershell.exe', ['-NoProfile', '-Command', ps], { encoding: 'utf8' });
|
|
102
|
+
if (r.status !== 0)
|
|
103
|
+
throw new Error(r.stderr || r.stdout || 'powershell failed');
|
|
104
|
+
});
|
|
105
|
+
run(script);
|
|
106
|
+
}
|
|
107
|
+
else if (planned.kind === 'app') {
|
|
108
|
+
const mac = dest;
|
|
109
|
+
mkdirSync(join(mac, 'Contents', 'MacOS'), { recursive: true });
|
|
110
|
+
mkdirSync(join(mac, 'Contents', 'Resources'), { recursive: true });
|
|
111
|
+
writeFileSync(join(mac, 'Contents', 'Info.plist'), macInfoPlist());
|
|
112
|
+
const launcher = join(mac, 'Contents', 'MacOS', 'launcher');
|
|
113
|
+
writeFileSync(launcher, macLauncherScript(nodePath, opts.scriptPath), { mode: 0o755 });
|
|
114
|
+
try {
|
|
115
|
+
chmodSync(launcher, 0o755);
|
|
116
|
+
}
|
|
117
|
+
catch { /* */ }
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
writeFileSync(dest, linuxDesktopFile({ nodePath, scriptPath: opts.scriptPath, workdir: opts.workdir }));
|
|
121
|
+
}
|
|
122
|
+
const state = { path: dest, workdir: opts.workdir, kind: planned.kind };
|
|
123
|
+
writeBorn0600(join(home, STATE_REL), JSON.stringify(state) + '\n');
|
|
124
|
+
if (opts.token?.trim()) {
|
|
125
|
+
writeBorn0600(join(home, TOKEN_REL), opts.token.trim() + '\n');
|
|
126
|
+
}
|
|
127
|
+
return { dest, kind: planned.kind, collided };
|
|
128
|
+
}
|
|
129
|
+
export function uninstallShortcut(opts = {}) {
|
|
130
|
+
const home = opts.home ?? process.env.CONARIUM_CONSOLE_HOME ?? osHomedir();
|
|
131
|
+
const stateFile = join(home, STATE_REL);
|
|
132
|
+
if (!existsSync(stateFile))
|
|
133
|
+
return { removed: null };
|
|
134
|
+
let state;
|
|
135
|
+
try {
|
|
136
|
+
state = JSON.parse(readFileSync(stateFile, 'utf8'));
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return { removed: null };
|
|
140
|
+
}
|
|
141
|
+
if (state.path && existsSync(state.path)) {
|
|
142
|
+
rmSync(state.path, { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
unlinkSync(stateFile);
|
|
146
|
+
}
|
|
147
|
+
catch { /* */ }
|
|
148
|
+
const tokenFile = join(home, TOKEN_REL);
|
|
149
|
+
try {
|
|
150
|
+
unlinkSync(tokenFile);
|
|
151
|
+
}
|
|
152
|
+
catch { /* */ }
|
|
153
|
+
return { removed: state.path };
|
|
154
|
+
}
|
|
155
|
+
export function loadLaunchToken(home = process.env.CONARIUM_CONSOLE_HOME || osHomedir()) {
|
|
156
|
+
const fromEnv = process.env.CONARIUM_CONSOLE_TOKEN?.trim();
|
|
157
|
+
if (fromEnv)
|
|
158
|
+
return fromEnv;
|
|
159
|
+
const file = join(home, TOKEN_REL);
|
|
160
|
+
if (!existsSync(file))
|
|
161
|
+
return null;
|
|
162
|
+
const v = readFileSync(file, 'utf8').trim();
|
|
163
|
+
return v || null;
|
|
164
|
+
}
|
|
165
|
+
export function loadShortcutState(home = process.env.CONARIUM_CONSOLE_HOME || osHomedir()) {
|
|
166
|
+
const file = join(home, STATE_REL);
|
|
167
|
+
if (!existsSync(file))
|
|
168
|
+
return null;
|
|
169
|
+
try {
|
|
170
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=console-shortcut.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console-shortcut.js","sourceRoot":"","sources":["../src/console-shortcut.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAC3G,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,MAAM,CAAC,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAA;IAC3C,OAAO,CAAC,IAAI,IAAI,CAAA;AAClB,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,qBAAqB,IAAI,SAAS,EAAE;IACvF,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;IAClC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAA;IAClC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAkB,CAAA;IAChE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
|
package/dist/console.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { Audit } from './audit.js';
|
|
3
|
+
import { createHandoffStore, createSessionStore } from './console-handoff.js';
|
|
3
4
|
export declare const DEFAULT_CONSOLE_HOST = "127.0.0.1";
|
|
4
5
|
declare const ConsolePolicyPatchSchema: z.ZodObject<{
|
|
5
6
|
maxRows: z.ZodOptional<z.ZodNumber>;
|
|
@@ -34,11 +35,16 @@ export declare function validateConsoleConfig(input: unknown): ConsolePolicyPatc
|
|
|
34
35
|
* silent policy downgrade.
|
|
35
36
|
*/
|
|
36
37
|
export declare function mergeConsolePolicyPatch(existing: Record<string, unknown>, patch: ConsolePolicyPatch): Record<string, unknown>;
|
|
38
|
+
/** Same-directory temp + rename: a crash mid-write cannot leave a half config. */
|
|
39
|
+
export declare function writeConfigAtomic(configFile: string, contents: string): void;
|
|
37
40
|
export declare function redactSecretFields(value: unknown): unknown;
|
|
38
41
|
export declare function createPlaygroundAudit(auditFile: string): Audit;
|
|
39
42
|
export declare function createConsoleApp(opts?: {
|
|
40
43
|
configFile?: string;
|
|
41
44
|
auditFile?: string;
|
|
45
|
+
handoff?: ReturnType<typeof createHandoffStore>;
|
|
46
|
+
sessions?: ReturnType<typeof createSessionStore>;
|
|
47
|
+
onPresence?: () => void;
|
|
42
48
|
}): import("express-serve-static-core").Express;
|
|
43
49
|
export declare function startConsole(port?: number, host?: string): void;
|
|
44
50
|
export {};
|
package/dist/console.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"console.d.ts","sourceRoot":"","sources":["../src/console.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"console.d.ts","sourceRoot":"","sources":["../src/console.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAa7E,eAAO,MAAM,oBAAoB,cAAc,CAAA;AAE/C,QAAA,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;EASnB,CAAA;AAEX,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAA;AAIzE,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,kBAAkB,CAExE;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,KAAK,EAAE,kBAAkB,GACxB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CASzB;AAED,kFAAkF;AAClF,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAoB5E;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAa1D;AAiFD,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK,CAQ9D;AAED,wBAAgB,gBAAgB,CAAC,IAAI,GAAE;IACrC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAA;IAC/C,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAA;IAChD,UAAU,CAAC,EAAE,MAAM,IAAI,CAAA;CACnB,+CAkQL;AAED,wBAAgB,YAAY,CAAC,IAAI,GAAE,MAAa,EAAE,IAAI,GAAE,MAAkE,QAKzH"}
|
package/dist/console.js
CHANGED
|
@@ -7,6 +7,9 @@ import { z } from 'zod';
|
|
|
7
7
|
import { Governance } from './governance.js';
|
|
8
8
|
import { Audit } from './audit.js';
|
|
9
9
|
import { RateLimiter, clientKey } from './rate_limit.js';
|
|
10
|
+
import { createHandoffStore, createSessionStore } from './console-handoff.js';
|
|
11
|
+
import { findReceipt, loadReceiptsForConsole, publicPemForPanel, toListItems, verifyCommandFor, } from './console-receipts.js';
|
|
12
|
+
import { receiptToView, renderReceiptHtml } from './receipt-view.js';
|
|
10
13
|
const __filename = fileURLToPath(import.meta.url);
|
|
11
14
|
const __dirname = path.dirname(__filename);
|
|
12
15
|
export const DEFAULT_CONSOLE_HOST = '127.0.0.1';
|
|
@@ -39,6 +42,34 @@ export function mergeConsolePolicyPatch(existing, patch) {
|
|
|
39
42
|
}
|
|
40
43
|
return { ...existing, policy: prev };
|
|
41
44
|
}
|
|
45
|
+
/** Same-directory temp + rename: a crash mid-write cannot leave a half config. */
|
|
46
|
+
export function writeConfigAtomic(configFile, contents) {
|
|
47
|
+
const dir = path.dirname(configFile);
|
|
48
|
+
const tmp = path.join(dir, `.${path.basename(configFile)}.${process.pid}.tmp`);
|
|
49
|
+
try {
|
|
50
|
+
fs.writeFileSync(tmp, contents, { encoding: 'utf8', mode: 0o600 });
|
|
51
|
+
try {
|
|
52
|
+
fs.renameSync(tmp, configFile);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
const code = err.code;
|
|
56
|
+
if (process.platform === 'win32' && (code === 'EPERM' || code === 'EEXIST')) {
|
|
57
|
+
fs.rmSync(configFile, { force: true });
|
|
58
|
+
fs.renameSync(tmp, configFile);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
try {
|
|
67
|
+
fs.rmSync(tmp, { force: true });
|
|
68
|
+
}
|
|
69
|
+
catch { /* leftover tmp */ }
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
42
73
|
export function redactSecretFields(value) {
|
|
43
74
|
if (Array.isArray(value))
|
|
44
75
|
return value.map(item => redactSecretFields(item));
|
|
@@ -62,26 +93,56 @@ function constantTimeEqual(a, b) {
|
|
|
62
93
|
return false;
|
|
63
94
|
return timingSafeEqual(left, right);
|
|
64
95
|
}
|
|
65
|
-
function
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
96
|
+
function readCookie(req, name) {
|
|
97
|
+
const raw = req.headers.cookie || '';
|
|
98
|
+
for (const part of raw.split(';')) {
|
|
99
|
+
const i = part.indexOf('=');
|
|
100
|
+
if (i < 0)
|
|
101
|
+
continue;
|
|
102
|
+
if (part.slice(0, i).trim() === name) {
|
|
103
|
+
try {
|
|
104
|
+
return decodeURIComponent(part.slice(i + 1).trim());
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return '';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
76
110
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
111
|
+
return '';
|
|
112
|
+
}
|
|
113
|
+
function requireConsoleAuth(sessions) {
|
|
114
|
+
return (req, res, next) => {
|
|
115
|
+
const token = process.env.CONARIUM_CONSOLE_TOKEN;
|
|
116
|
+
if (!token) {
|
|
117
|
+
res.status(503).json({ error: 'Console auth token is not configured' });
|
|
81
118
|
return;
|
|
82
119
|
}
|
|
83
|
-
|
|
84
|
-
|
|
120
|
+
const sess = sessions.get(readCookie(req, 'conarium_console_sess'));
|
|
121
|
+
if (sess) {
|
|
122
|
+
if (!['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
|
|
123
|
+
if (!constantTimeEqual(req.header('x-csrf-token') || '', sess.csrf)) {
|
|
124
|
+
res.status(403).json({ error: 'CSRF token required' });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
next();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const auth = req.header('authorization') || '';
|
|
132
|
+
const supplied = auth.startsWith('Bearer ') ? auth.slice(7) : req.header('x-conarium-console-token') || '';
|
|
133
|
+
if (!constantTimeEqual(supplied, token)) {
|
|
134
|
+
res.status(401).json({ error: 'Unauthorized' });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (!['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
|
|
138
|
+
const csrf = process.env.CONARIUM_CONSOLE_CSRF_TOKEN || token;
|
|
139
|
+
if (!constantTimeEqual(req.header('x-csrf-token') || '', csrf)) {
|
|
140
|
+
res.status(403).json({ error: 'CSRF token required' });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
next();
|
|
145
|
+
};
|
|
85
146
|
}
|
|
86
147
|
// A pre-chain playground file: first line parses as JSON but was written before
|
|
87
148
|
// entries carried hashes. A file whose entries HAVE hashes but fail validation is
|
|
@@ -125,6 +186,21 @@ export function createConsoleApp(opts = {}) {
|
|
|
125
186
|
const configFile = opts.configFile || path.join(__dirname, '../conarium.config.json');
|
|
126
187
|
const auditFile = opts.auditFile || path.join(__dirname, '../audit.log.jsonl');
|
|
127
188
|
const audit = createPlaygroundAudit(auditFile);
|
|
189
|
+
const handoff = opts.handoff ?? createHandoffStore();
|
|
190
|
+
const sessions = opts.sessions ?? createSessionStore();
|
|
191
|
+
app.get('/handoff', (req, res) => {
|
|
192
|
+
const n = String(req.query.n || '');
|
|
193
|
+
if (!handoff.consume(n)) {
|
|
194
|
+
res.status(403).type('text/plain').send('handoff expired or already used');
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const sess = sessions.create();
|
|
198
|
+
res.setHeader('Set-Cookie', [
|
|
199
|
+
`conarium_console_sess=${sess.id}; HttpOnly; SameSite=Strict; Path=/; Max-Age=86400`,
|
|
200
|
+
`conarium_console_csrf=${sess.csrf}; SameSite=Strict; Path=/; Max-Age=86400`,
|
|
201
|
+
]);
|
|
202
|
+
res.redirect(302, '/');
|
|
203
|
+
});
|
|
128
204
|
app.use(express.static(publicDir));
|
|
129
205
|
// Hiz siniri KIMLIK KONTROLUNDEN ONCE.
|
|
130
206
|
//
|
|
@@ -144,7 +220,62 @@ export function createConsoleApp(opts = {}) {
|
|
|
144
220
|
}
|
|
145
221
|
next();
|
|
146
222
|
});
|
|
147
|
-
app.use('/api', requireConsoleAuth);
|
|
223
|
+
app.use('/api', requireConsoleAuth(sessions));
|
|
224
|
+
app.get('/api/presence', (_req, res) => {
|
|
225
|
+
opts.onPresence?.();
|
|
226
|
+
res.status(204).end();
|
|
227
|
+
});
|
|
228
|
+
app.get('/api/receipts', (_req, res) => {
|
|
229
|
+
const loaded = loadReceiptsForConsole(configFile);
|
|
230
|
+
res.json({
|
|
231
|
+
chain: loaded.chain,
|
|
232
|
+
items: toListItems(loaded.receipts),
|
|
233
|
+
empty: loaded.emptyMessage,
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
app.get('/api/receipts/:id/raw', (req, res) => {
|
|
237
|
+
const loaded = loadReceiptsForConsole(configFile);
|
|
238
|
+
const receipt = findReceipt(loaded.receipts, String(req.params.id));
|
|
239
|
+
if (!receipt) {
|
|
240
|
+
res.status(404).json({ error: 'receipt not found' });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
244
|
+
res.setHeader('content-disposition', `attachment; filename="${receipt.id}.json"`);
|
|
245
|
+
res.send(JSON.stringify(receipt));
|
|
246
|
+
});
|
|
247
|
+
app.get('/api/receipts/:id/html', (req, res) => {
|
|
248
|
+
const loaded = loadReceiptsForConsole(configFile);
|
|
249
|
+
const receipt = findReceipt(loaded.receipts, String(req.params.id));
|
|
250
|
+
if (!receipt) {
|
|
251
|
+
res.status(404).type('text/plain').send('receipt not found');
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const html = renderReceiptHtml(receiptToView(receipt, {
|
|
255
|
+
publicKey: publicPemForPanel(),
|
|
256
|
+
verify: verifyCommandFor(loaded.sink),
|
|
257
|
+
entries: loaded.receipts.length,
|
|
258
|
+
chainIntegrity: loaded.chain,
|
|
259
|
+
jsonHref: `/api/receipts/${encodeURIComponent(receipt.id)}/raw`,
|
|
260
|
+
}), { mode: 'fragment' });
|
|
261
|
+
res.type('html').send(html);
|
|
262
|
+
});
|
|
263
|
+
app.get('/api/receipts/:id', (req, res) => {
|
|
264
|
+
const loaded = loadReceiptsForConsole(configFile);
|
|
265
|
+
const receipt = findReceipt(loaded.receipts, String(req.params.id));
|
|
266
|
+
if (!receipt) {
|
|
267
|
+
res.status(404).json({ error: 'receipt not found' });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const html = renderReceiptHtml(receiptToView(receipt, {
|
|
271
|
+
publicKey: publicPemForPanel(),
|
|
272
|
+
verify: verifyCommandFor(loaded.sink),
|
|
273
|
+
entries: loaded.receipts.length,
|
|
274
|
+
chainIntegrity: loaded.chain,
|
|
275
|
+
jsonHref: `/api/receipts/${encodeURIComponent(receipt.id)}/raw`,
|
|
276
|
+
}), { mode: 'fragment' });
|
|
277
|
+
res.json({ receipt, html, chain: loaded.chain });
|
|
278
|
+
});
|
|
148
279
|
app.get('/api/config', (req, res) => {
|
|
149
280
|
try {
|
|
150
281
|
if (fs.existsSync(configFile)) {
|
|
@@ -171,7 +302,7 @@ export function createConsoleApp(opts = {}) {
|
|
|
171
302
|
}
|
|
172
303
|
}
|
|
173
304
|
const next = mergeConsolePolicyPatch(existing, patch);
|
|
174
|
-
|
|
305
|
+
writeConfigAtomic(configFile, JSON.stringify(next, null, 2) + '\n');
|
|
175
306
|
const tables = next.policy?.allowTables;
|
|
176
307
|
const allowTablesEmpty = !Array.isArray(tables) || tables.length === 0;
|
|
177
308
|
res.json({ success: true, allowTablesEmpty });
|