shellpuppet 0.0.1 → 0.1.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/LICENSE +202 -0
- package/NOTICE +31 -0
- package/README.md +340 -8
- package/dist/cli/index.d.ts +13 -0
- package/dist/cli/index.js +693 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/client/http.d.ts +82 -0
- package/dist/client/http.js +225 -0
- package/dist/client/http.js.map +1 -0
- package/dist/core/dialogs.d.ts +88 -0
- package/dist/core/dialogs.js +214 -0
- package/dist/core/dialogs.js.map +1 -0
- package/dist/core/echo.d.ts +71 -0
- package/dist/core/echo.js +249 -0
- package/dist/core/echo.js.map +1 -0
- package/dist/core/find.d.ts +34 -0
- package/dist/core/find.js +41 -0
- package/dist/core/find.js.map +1 -0
- package/dist/core/keys.d.ts +22 -0
- package/dist/core/keys.js +80 -0
- package/dist/core/keys.js.map +1 -0
- package/dist/core/manager.d.ts +66 -0
- package/dist/core/manager.js +158 -0
- package/dist/core/manager.js.map +1 -0
- package/dist/core/nativePty.d.ts +69 -0
- package/dist/core/nativePty.js +156 -0
- package/dist/core/nativePty.js.map +1 -0
- package/dist/core/policy.d.ts +61 -0
- package/dist/core/policy.js +143 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/promptDetector.d.ts +75 -0
- package/dist/core/promptDetector.js +606 -0
- package/dist/core/promptDetector.js.map +1 -0
- package/dist/core/reasoning.d.ts +252 -0
- package/dist/core/reasoning.js +740 -0
- package/dist/core/reasoning.js.map +1 -0
- package/dist/core/regexBudget.d.ts +44 -0
- package/dist/core/regexBudget.js +79 -0
- package/dist/core/regexBudget.js.map +1 -0
- package/dist/core/renderer.d.ts +112 -0
- package/dist/core/renderer.js +225 -0
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/session.d.ts +311 -0
- package/dist/core/session.js +966 -0
- package/dist/core/session.js.map +1 -0
- package/dist/core/types.d.ts +281 -0
- package/dist/core/types.js +47 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/waiter.d.ts +86 -0
- package/dist/core/waiter.js +317 -0
- package/dist/core/waiter.js.map +1 -0
- package/dist/core/windowsPtyStart.d.ts +90 -0
- package/dist/core/windowsPtyStart.js +129 -0
- package/dist/core/windowsPtyStart.js.map +1 -0
- package/dist/daemon/audit.d.ts +23 -0
- package/dist/daemon/audit.js +51 -0
- package/dist/daemon/audit.js.map +1 -0
- package/dist/daemon/auth.d.ts +127 -0
- package/dist/daemon/auth.js +241 -0
- package/dist/daemon/auth.js.map +1 -0
- package/dist/daemon/config.d.ts +76 -0
- package/dist/daemon/config.js +301 -0
- package/dist/daemon/config.js.map +1 -0
- package/dist/daemon/index.d.ts +7 -0
- package/dist/daemon/index.js +87 -0
- package/dist/daemon/index.js.map +1 -0
- package/dist/daemon/liveness.d.ts +36 -0
- package/dist/daemon/liveness.js +71 -0
- package/dist/daemon/liveness.js.map +1 -0
- package/dist/daemon/logger.d.ts +7 -0
- package/dist/daemon/logger.js +29 -0
- package/dist/daemon/logger.js.map +1 -0
- package/dist/daemon/meta.d.ts +9 -0
- package/dist/daemon/meta.js +24 -0
- package/dist/daemon/meta.js.map +1 -0
- package/dist/daemon/paths.d.ts +83 -0
- package/dist/daemon/paths.js +154 -0
- package/dist/daemon/paths.js.map +1 -0
- package/dist/daemon/screenshot.d.ts +87 -0
- package/dist/daemon/screenshot.js +256 -0
- package/dist/daemon/screenshot.js.map +1 -0
- package/dist/daemon/server.d.ts +244 -0
- package/dist/daemon/server.js +2187 -0
- package/dist/daemon/server.js.map +1 -0
- package/dist/daemon/terminalReports.d.ts +38 -0
- package/dist/daemon/terminalReports.js +84 -0
- package/dist/daemon/terminalReports.js.map +1 -0
- package/dist/daemon/viewerLauncher.d.ts +135 -0
- package/dist/daemon/viewerLauncher.js +280 -0
- package/dist/daemon/viewerLauncher.js.map +1 -0
- package/dist/daemon/viewerSync.d.ts +38 -0
- package/dist/daemon/viewerSync.js +83 -0
- package/dist/daemon/viewerSync.js.map +1 -0
- package/dist/mcp/config.d.ts +57 -0
- package/dist/mcp/config.js +106 -0
- package/dist/mcp/config.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +69 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/tools.d.ts +59 -0
- package/dist/mcp/tools.js +537 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/viewer/icon-16.png +0 -0
- package/dist/viewer/icon-180.png +0 -0
- package/dist/viewer/icon-32.png +0 -0
- package/dist/viewer/icon-512.png +0 -0
- package/dist/viewer/icon.svg +66 -0
- package/dist/viewer/index.html +56 -0
- package/dist/viewer/viewer.css +84 -0
- package/dist/viewer/viewer.js +661 -0
- package/package.json +66 -7
- package/index.js +0 -4
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token generation and verification (design §4.1).
|
|
3
|
+
*
|
|
4
|
+
* Three credentials, deliberately different in kind:
|
|
5
|
+
*
|
|
6
|
+
* - **Client token**: full API access; generated per daemon run, written to
|
|
7
|
+
* `daemon.json`, read by same-user clients. Rotates on restart. Travels in an
|
|
8
|
+
* `Authorization` header over HTTP, and on the query string for the two
|
|
9
|
+
* WebSocket routes, because a browser will not let a page set headers on an
|
|
10
|
+
* upgrade (design §4.1). It never appears in a URL a human is given.
|
|
11
|
+
* - **Viewer grant**: the `vt` in a viewer URL. **One use, short life.** It
|
|
12
|
+
* is not a session credential and cannot open a stream; it can only be
|
|
13
|
+
* exchanged, once, for the credential below. A viewer URL inevitably lands
|
|
14
|
+
* in the driving agent's transcript, in logs and in whatever the human
|
|
15
|
+
* pasted it into, so the thing that lands there has to become worthless
|
|
16
|
+
* the moment it has done its job: spent by the first redemption, or expired
|
|
17
|
+
* unopened. Until then it is a live bearer capability.
|
|
18
|
+
* - **Stream credential**: what a grant is exchanged for, delivered as an
|
|
19
|
+
* `HttpOnly` cookie scoped to that one session's path. The browser attaches
|
|
20
|
+
* it to the WebSocket upgrade on its own, so page scripts never hold it and
|
|
21
|
+
* cannot leak it. It lives as long as the session, because a viewer that
|
|
22
|
+
* reconnects after a dropped socket must not need a fresh URL.
|
|
23
|
+
*
|
|
24
|
+
* Read-only is a property of the grant, and so of the credential it
|
|
25
|
+
* becomes: it is enforced by the daemon, not by the viewer page. The page's
|
|
26
|
+
* own toggle stops that tab sending keystrokes and is a convenience; a
|
|
27
|
+
* read-only credential means the daemon will not accept them.
|
|
28
|
+
*/
|
|
29
|
+
export type AuthResult = {
|
|
30
|
+
kind: 'client';
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'viewer';
|
|
33
|
+
sessionId: string;
|
|
34
|
+
readOnly: boolean;
|
|
35
|
+
} | null;
|
|
36
|
+
/**
|
|
37
|
+
* How long an unredeemed viewer grant stays usable.
|
|
38
|
+
*
|
|
39
|
+
* The window we open ourselves redeems it in a second or two, so this bounds
|
|
40
|
+
* only the case where nobody opened the link: a URL sitting in a transcript,
|
|
41
|
+
* a chat message or a terminal scrollback. Five minutes is long enough to
|
|
42
|
+
* paste a link to a second device by hand and short enough that yesterday's
|
|
43
|
+
* transcript is inert. Expired is not a dead end: mint another.
|
|
44
|
+
*/
|
|
45
|
+
export declare const VIEWER_GRANT_TTL_MS: number;
|
|
46
|
+
export interface RedeemedGrant {
|
|
47
|
+
/** The value to put in the viewer's cookie. */
|
|
48
|
+
streamToken: string;
|
|
49
|
+
sessionId: string;
|
|
50
|
+
readOnly: boolean;
|
|
51
|
+
}
|
|
52
|
+
export declare function generateToken(): string;
|
|
53
|
+
export declare class TokenStore {
|
|
54
|
+
private readonly clientToken;
|
|
55
|
+
private readonly grants;
|
|
56
|
+
private readonly streams;
|
|
57
|
+
constructor(clientToken?: string);
|
|
58
|
+
get token(): string;
|
|
59
|
+
/**
|
|
60
|
+
* Mint a one-use viewer grant for a session. `readOnly` rides with it, so a
|
|
61
|
+
* watch-only link cannot be turned into a typing one by the page that holds
|
|
62
|
+
* it.
|
|
63
|
+
*/
|
|
64
|
+
mintViewerGrant(sessionId: string, options?: {
|
|
65
|
+
readOnly?: boolean;
|
|
66
|
+
ttlMs?: number;
|
|
67
|
+
now?: number;
|
|
68
|
+
}): string;
|
|
69
|
+
/**
|
|
70
|
+
* Exchange a grant for a stream credential, consuming the grant. Returns
|
|
71
|
+
* null for an unknown, already-redeemed or expired grant. The three cases
|
|
72
|
+
* are deliberately indistinguishable to the caller.
|
|
73
|
+
*/
|
|
74
|
+
redeemViewerGrant(presented: string | undefined | null, now?: number): RedeemedGrant | null;
|
|
75
|
+
/** Verify a stream credential (the viewer's cookie). */
|
|
76
|
+
verifyStreamToken(presented: string | undefined | null): AuthResult;
|
|
77
|
+
/**
|
|
78
|
+
* Take back a grant and whatever it became: the grant itself if nobody spent
|
|
79
|
+
* it, and the stream credential it was exchanged for if somebody did.
|
|
80
|
+
*
|
|
81
|
+
* For a link minted on behalf of something with an end, which is the
|
|
82
|
+
* screenshot's headless browser and not a human's window. Its credential
|
|
83
|
+
* would otherwise live as long as the session, so a session photographed a
|
|
84
|
+
* thousand times would hold a thousand of them, each able to open a socket.
|
|
85
|
+
* A human's viewer is the opposite case and keeps its credential: a dropped
|
|
86
|
+
* socket must reconnect without a fresh link.
|
|
87
|
+
*/
|
|
88
|
+
revokeViewerGrant(grant: string): void;
|
|
89
|
+
/** Drop every viewer grant and credential scoped to a session (on kill/exit cleanup). */
|
|
90
|
+
revokeViewerTokens(sessionId: string): void;
|
|
91
|
+
/**
|
|
92
|
+
* Verify a bearer token. Only the client token authenticates this way:
|
|
93
|
+
* viewer credentials are cookie-borne and session-scoped, and are checked
|
|
94
|
+
* with `verifyStreamToken`.
|
|
95
|
+
*/
|
|
96
|
+
verify(presented: string | undefined | null): AuthResult;
|
|
97
|
+
private pruneExpired;
|
|
98
|
+
}
|
|
99
|
+
/** Extract a bearer token from an Authorization header value. */
|
|
100
|
+
export declare function bearerToken(header: string | undefined): string | null;
|
|
101
|
+
/**
|
|
102
|
+
* The viewer's cookie name. One name, but each is written with the session's
|
|
103
|
+
* own `Path`, so a browser watching several sessions keeps their credentials
|
|
104
|
+
* apart and sends only the one whose path matches the socket being opened.
|
|
105
|
+
*/
|
|
106
|
+
export declare const VIEWER_COOKIE = "sp_viewer";
|
|
107
|
+
/**
|
|
108
|
+
* Every value carried under `name` in a Cookie header, in order, with its
|
|
109
|
+
* percent-escapes decoded. More than one is possible: cookies ignore ports,
|
|
110
|
+
* so anything served from 127.0.0.1 can set a same-named cookie beside the
|
|
111
|
+
* daemon's own, and the caller tries each rather than trusting the first. A
|
|
112
|
+
* value whose escapes do not decode is skipped rather than thrown on.
|
|
113
|
+
*/
|
|
114
|
+
export declare function cookieValues(header: string | undefined, name: string): string[];
|
|
115
|
+
/** The first value carried under `name`, or null. */
|
|
116
|
+
export declare function cookieValue(header: string | undefined, name: string): string | null;
|
|
117
|
+
/**
|
|
118
|
+
* The `Set-Cookie` value delivering a stream credential.
|
|
119
|
+
*
|
|
120
|
+
* `HttpOnly` keeps it out of reach of page scripts, `SameSite=Strict` stops
|
|
121
|
+
* any other site's navigation carrying it and the per-session `Path` means a
|
|
122
|
+
* credential for one session is never even sent on another's socket. No
|
|
123
|
+
* `Max-Age`: it should die with the browsing session, not linger on disk.
|
|
124
|
+
* No `Secure`, deliberately: the daemon is loopback-only http, and a
|
|
125
|
+
* `Secure` cookie would simply never be stored.
|
|
126
|
+
*/
|
|
127
|
+
export declare function viewerCookie(sessionId: string, streamToken: string): string;
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token generation and verification (design §4.1).
|
|
3
|
+
*
|
|
4
|
+
* Three credentials, deliberately different in kind:
|
|
5
|
+
*
|
|
6
|
+
* - **Client token**: full API access; generated per daemon run, written to
|
|
7
|
+
* `daemon.json`, read by same-user clients. Rotates on restart. Travels in an
|
|
8
|
+
* `Authorization` header over HTTP, and on the query string for the two
|
|
9
|
+
* WebSocket routes, because a browser will not let a page set headers on an
|
|
10
|
+
* upgrade (design §4.1). It never appears in a URL a human is given.
|
|
11
|
+
* - **Viewer grant**: the `vt` in a viewer URL. **One use, short life.** It
|
|
12
|
+
* is not a session credential and cannot open a stream; it can only be
|
|
13
|
+
* exchanged, once, for the credential below. A viewer URL inevitably lands
|
|
14
|
+
* in the driving agent's transcript, in logs and in whatever the human
|
|
15
|
+
* pasted it into, so the thing that lands there has to become worthless
|
|
16
|
+
* the moment it has done its job: spent by the first redemption, or expired
|
|
17
|
+
* unopened. Until then it is a live bearer capability.
|
|
18
|
+
* - **Stream credential**: what a grant is exchanged for, delivered as an
|
|
19
|
+
* `HttpOnly` cookie scoped to that one session's path. The browser attaches
|
|
20
|
+
* it to the WebSocket upgrade on its own, so page scripts never hold it and
|
|
21
|
+
* cannot leak it. It lives as long as the session, because a viewer that
|
|
22
|
+
* reconnects after a dropped socket must not need a fresh URL.
|
|
23
|
+
*
|
|
24
|
+
* Read-only is a property of the grant, and so of the credential it
|
|
25
|
+
* becomes: it is enforced by the daemon, not by the viewer page. The page's
|
|
26
|
+
* own toggle stops that tab sending keystrokes and is a convenience; a
|
|
27
|
+
* read-only credential means the daemon will not accept them.
|
|
28
|
+
*/
|
|
29
|
+
import { createHash, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
30
|
+
/**
|
|
31
|
+
* How long an unredeemed viewer grant stays usable.
|
|
32
|
+
*
|
|
33
|
+
* The window we open ourselves redeems it in a second or two, so this bounds
|
|
34
|
+
* only the case where nobody opened the link: a URL sitting in a transcript,
|
|
35
|
+
* a chat message or a terminal scrollback. Five minutes is long enough to
|
|
36
|
+
* paste a link to a second device by hand and short enough that yesterday's
|
|
37
|
+
* transcript is inert. Expired is not a dead end: mint another.
|
|
38
|
+
*/
|
|
39
|
+
export const VIEWER_GRANT_TTL_MS = 5 * 60_000;
|
|
40
|
+
export function generateToken() {
|
|
41
|
+
return randomBytes(32).toString('hex');
|
|
42
|
+
}
|
|
43
|
+
/** Constant-time comparison over sha256 digests, so length never leaks. */
|
|
44
|
+
function tokensEqual(a, b) {
|
|
45
|
+
const da = createHash('sha256').update(a).digest();
|
|
46
|
+
const db = createHash('sha256').update(b).digest();
|
|
47
|
+
return timingSafeEqual(da, db);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Find a key by constant-time comparison rather than by lookup. A `Map.get`
|
|
51
|
+
* would be faster and would compare the presented string against stored keys
|
|
52
|
+
* in whatever way the engine sees fit; these are secrets, so every comparison
|
|
53
|
+
* goes through `tokensEqual`.
|
|
54
|
+
*/
|
|
55
|
+
function findKey(store, presented) {
|
|
56
|
+
for (const key of store.keys()) {
|
|
57
|
+
if (tokensEqual(presented, key))
|
|
58
|
+
return key;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
export class TokenStore {
|
|
63
|
+
clientToken;
|
|
64
|
+
grants = new Map();
|
|
65
|
+
streams = new Map();
|
|
66
|
+
constructor(clientToken = generateToken()) {
|
|
67
|
+
this.clientToken = clientToken;
|
|
68
|
+
}
|
|
69
|
+
get token() {
|
|
70
|
+
return this.clientToken;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Mint a one-use viewer grant for a session. `readOnly` rides with it, so a
|
|
74
|
+
* watch-only link cannot be turned into a typing one by the page that holds
|
|
75
|
+
* it.
|
|
76
|
+
*/
|
|
77
|
+
mintViewerGrant(sessionId, options = {}) {
|
|
78
|
+
const now = options.now ?? Date.now();
|
|
79
|
+
this.pruneExpired(now);
|
|
80
|
+
const token = generateToken();
|
|
81
|
+
this.grants.set(token, {
|
|
82
|
+
sessionId,
|
|
83
|
+
readOnly: options.readOnly ?? false,
|
|
84
|
+
expiresAt: now + (options.ttlMs ?? VIEWER_GRANT_TTL_MS),
|
|
85
|
+
});
|
|
86
|
+
return token;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Exchange a grant for a stream credential, consuming the grant. Returns
|
|
90
|
+
* null for an unknown, already-redeemed or expired grant. The three cases
|
|
91
|
+
* are deliberately indistinguishable to the caller.
|
|
92
|
+
*/
|
|
93
|
+
redeemViewerGrant(presented, now = Date.now()) {
|
|
94
|
+
this.pruneExpired(now);
|
|
95
|
+
if (presented === undefined || presented === null || presented === '')
|
|
96
|
+
return null;
|
|
97
|
+
const key = findKey(this.grants, presented);
|
|
98
|
+
if (key === null)
|
|
99
|
+
return null;
|
|
100
|
+
const grant = this.grants.get(key);
|
|
101
|
+
if (grant === undefined)
|
|
102
|
+
return null;
|
|
103
|
+
// One use: gone whether or not the caller does anything with it.
|
|
104
|
+
this.grants.delete(key);
|
|
105
|
+
if (grant.expiresAt <= now)
|
|
106
|
+
return null;
|
|
107
|
+
const streamToken = generateToken();
|
|
108
|
+
this.streams.set(streamToken, {
|
|
109
|
+
sessionId: grant.sessionId,
|
|
110
|
+
readOnly: grant.readOnly,
|
|
111
|
+
grant: key,
|
|
112
|
+
});
|
|
113
|
+
return { streamToken, sessionId: grant.sessionId, readOnly: grant.readOnly };
|
|
114
|
+
}
|
|
115
|
+
/** Verify a stream credential (the viewer's cookie). */
|
|
116
|
+
verifyStreamToken(presented) {
|
|
117
|
+
if (presented === undefined || presented === null || presented === '')
|
|
118
|
+
return null;
|
|
119
|
+
const key = findKey(this.streams, presented);
|
|
120
|
+
if (key === null)
|
|
121
|
+
return null;
|
|
122
|
+
const credential = this.streams.get(key);
|
|
123
|
+
if (credential === undefined)
|
|
124
|
+
return null;
|
|
125
|
+
return {
|
|
126
|
+
kind: 'viewer',
|
|
127
|
+
sessionId: credential.sessionId,
|
|
128
|
+
readOnly: credential.readOnly,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Take back a grant and whatever it became: the grant itself if nobody spent
|
|
133
|
+
* it, and the stream credential it was exchanged for if somebody did.
|
|
134
|
+
*
|
|
135
|
+
* For a link minted on behalf of something with an end, which is the
|
|
136
|
+
* screenshot's headless browser and not a human's window. Its credential
|
|
137
|
+
* would otherwise live as long as the session, so a session photographed a
|
|
138
|
+
* thousand times would hold a thousand of them, each able to open a socket.
|
|
139
|
+
* A human's viewer is the opposite case and keeps its credential: a dropped
|
|
140
|
+
* socket must reconnect without a fresh link.
|
|
141
|
+
*/
|
|
142
|
+
revokeViewerGrant(grant) {
|
|
143
|
+
if (grant === '')
|
|
144
|
+
return;
|
|
145
|
+
const key = findKey(this.grants, grant);
|
|
146
|
+
if (key !== null)
|
|
147
|
+
this.grants.delete(key);
|
|
148
|
+
for (const [token, credential] of this.streams) {
|
|
149
|
+
if (tokensEqual(credential.grant, grant))
|
|
150
|
+
this.streams.delete(token);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Drop every viewer grant and credential scoped to a session (on kill/exit cleanup). */
|
|
154
|
+
revokeViewerTokens(sessionId) {
|
|
155
|
+
for (const [token, grant] of this.grants) {
|
|
156
|
+
if (grant.sessionId === sessionId)
|
|
157
|
+
this.grants.delete(token);
|
|
158
|
+
}
|
|
159
|
+
for (const [token, credential] of this.streams) {
|
|
160
|
+
if (credential.sessionId === sessionId)
|
|
161
|
+
this.streams.delete(token);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Verify a bearer token. Only the client token authenticates this way:
|
|
166
|
+
* viewer credentials are cookie-borne and session-scoped, and are checked
|
|
167
|
+
* with `verifyStreamToken`.
|
|
168
|
+
*/
|
|
169
|
+
verify(presented) {
|
|
170
|
+
if (presented === undefined || presented === null || presented === '')
|
|
171
|
+
return null;
|
|
172
|
+
if (tokensEqual(presented, this.clientToken))
|
|
173
|
+
return { kind: 'client' };
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
pruneExpired(now) {
|
|
177
|
+
for (const [token, grant] of this.grants) {
|
|
178
|
+
if (grant.expiresAt <= now)
|
|
179
|
+
this.grants.delete(token);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/** Extract a bearer token from an Authorization header value. */
|
|
184
|
+
export function bearerToken(header) {
|
|
185
|
+
if (header === undefined)
|
|
186
|
+
return null;
|
|
187
|
+
const match = /^Bearer\s+(\S+)$/i.exec(header);
|
|
188
|
+
return match?.[1] ?? null;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The viewer's cookie name. One name, but each is written with the session's
|
|
192
|
+
* own `Path`, so a browser watching several sessions keeps their credentials
|
|
193
|
+
* apart and sends only the one whose path matches the socket being opened.
|
|
194
|
+
*/
|
|
195
|
+
export const VIEWER_COOKIE = 'sp_viewer';
|
|
196
|
+
/**
|
|
197
|
+
* Every value carried under `name` in a Cookie header, in order, with its
|
|
198
|
+
* percent-escapes decoded. More than one is possible: cookies ignore ports,
|
|
199
|
+
* so anything served from 127.0.0.1 can set a same-named cookie beside the
|
|
200
|
+
* daemon's own, and the caller tries each rather than trusting the first. A
|
|
201
|
+
* value whose escapes do not decode is skipped rather than thrown on.
|
|
202
|
+
*/
|
|
203
|
+
export function cookieValues(header, name) {
|
|
204
|
+
if (header === undefined)
|
|
205
|
+
return [];
|
|
206
|
+
const values = [];
|
|
207
|
+
for (const part of header.split(';')) {
|
|
208
|
+
const at = part.indexOf('=');
|
|
209
|
+
if (at === -1)
|
|
210
|
+
continue;
|
|
211
|
+
if (part.slice(0, at).trim() !== name)
|
|
212
|
+
continue;
|
|
213
|
+
try {
|
|
214
|
+
values.push(decodeURIComponent(part.slice(at + 1).trim()));
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// Not a value this daemon ever set. Ignored, never fatal.
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return values;
|
|
221
|
+
}
|
|
222
|
+
/** The first value carried under `name`, or null. */
|
|
223
|
+
export function cookieValue(header, name) {
|
|
224
|
+
return cookieValues(header, name)[0] ?? null;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The `Set-Cookie` value delivering a stream credential.
|
|
228
|
+
*
|
|
229
|
+
* `HttpOnly` keeps it out of reach of page scripts, `SameSite=Strict` stops
|
|
230
|
+
* any other site's navigation carrying it and the per-session `Path` means a
|
|
231
|
+
* credential for one session is never even sent on another's socket. No
|
|
232
|
+
* `Max-Age`: it should die with the browsing session, not linger on disk.
|
|
233
|
+
* No `Secure`, deliberately: the daemon is loopback-only http, and a
|
|
234
|
+
* `Secure` cookie would simply never be stored.
|
|
235
|
+
*/
|
|
236
|
+
export function viewerCookie(sessionId, streamToken) {
|
|
237
|
+
return (`${VIEWER_COOKIE}=${encodeURIComponent(streamToken)}` +
|
|
238
|
+
`; Path=/sessions/${encodeURIComponent(sessionId)}` +
|
|
239
|
+
'; HttpOnly; SameSite=Strict');
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/daemon/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAOvE;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,GAAG,MAAM,CAAC;AA2B9C,MAAM,UAAU,aAAa;IAC3B,OAAO,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACzC,CAAC;AAED,2EAA2E;AAC3E,SAAS,WAAW,CAAC,CAAS,EAAE,CAAS;IACvC,MAAM,EAAE,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACnD,MAAM,EAAE,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACnD,OAAO,eAAe,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AACjC,CAAC;AAED;;;;;GAKG;AACH,SAAS,OAAO,CAAI,KAAqB,EAAE,SAAiB;IAC1D,KAAK,MAAM,GA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ACrD,MAAM,UAAU,WAAW,CAAC,MAA0B,EAAE,IAAY;IAClE,OAAO,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAC/C,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,SAAiB,EAAE,WAAmB;IACjE,OAAO,CACL,GAAG,aAAa,IAAI,kBAAkB,CAAC,WAAW,CAAC,EAAE;QACrD,oBAAoB,kBAAkB,CAAC,SAAS,CAAC,EAAE;QACnD,6BAA6B,CAC9B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape validation for `~/.shellpuppet/config.json` (design §4.3 fail-closed).
|
|
3
|
+
*
|
|
4
|
+
* Why this exists: parsed JSON is not a config until its shape has been
|
|
5
|
+
* checked. A string where an array belongs (`"allow": "^goose"`) must not
|
|
6
|
+
* reach `compilePolicy`, whose `for…of` would read it one character at a
|
|
7
|
+
* time as single-character rules. A shape mistake must fail CLOSED, exactly
|
|
8
|
+
* as a malformed pattern does.
|
|
9
|
+
*
|
|
10
|
+
* The rules:
|
|
11
|
+
* - No config file ⇒ pure defaults (the documented behaviour).
|
|
12
|
+
* - A file that cannot be read or parsed, or whose root is not a JSON object
|
|
13
|
+
* ⇒ the intended policy is unknowable: the policy refuses every command
|
|
14
|
+
* (naming the problem in each refusal) and sessionEnv falls back to
|
|
15
|
+
* `clean`.
|
|
16
|
+
* - A structurally invalid `policy` section ⇒ deny-all, problem named. No
|
|
17
|
+
* per-field salvage: a policy that cannot be evaluated as written must not
|
|
18
|
+
* be guessed at. Unknown keys INSIDE `policy` invalidate it too: a
|
|
19
|
+
* misspelt `deny` must not silently deny nothing.
|
|
20
|
+
* - Invalid `sessionEnv` ⇒ `clean`, never passthrough: environment hygiene
|
|
21
|
+
* is a security control, and its failure mode
|
|
22
|
+
* must not silently hand every session the daemon's secrets.
|
|
23
|
+
* - Invalid `idleMinutes` / `audit` ⇒ safe defaults, problem named
|
|
24
|
+
* (`captureContent` records typed secrets, so anything unclear means OFF).
|
|
25
|
+
* - Unknown top-level keys ⇒ reported (typo detection), config otherwise
|
|
26
|
+
* honoured.
|
|
27
|
+
*
|
|
28
|
+
* `parseDaemonConfig` is pure; `readDaemonConfig` adds the file read. The
|
|
29
|
+
* daemon stays up in every case: refusing to start would strand every
|
|
30
|
+
* client with no error message at all, whereas a running daemon can say
|
|
31
|
+
* exactly what is wrong on every refused request.
|
|
32
|
+
*/
|
|
33
|
+
import type { DialogSignature } from '../core/dialogs.js';
|
|
34
|
+
import type { AgentSignature } from '../core/reasoning.js';
|
|
35
|
+
import type { PolicyConfig, SessionEnvMode } from '../core/types.js';
|
|
36
|
+
import type { ViewerLaunchConfig } from './viewerLauncher.js';
|
|
37
|
+
export interface DaemonConfig {
|
|
38
|
+
policy?: PolicyConfig;
|
|
39
|
+
/** Idle self-shutdown threshold in minutes; fractions allowed (design §2.3). */
|
|
40
|
+
idleMinutes?: number;
|
|
41
|
+
/** Session environment hygiene (design §4.6). */
|
|
42
|
+
sessionEnv?: SessionEnvMode;
|
|
43
|
+
/** Audit options (design §4.5). */
|
|
44
|
+
audit?: {
|
|
45
|
+
captureContent?: boolean;
|
|
46
|
+
};
|
|
47
|
+
/** How the viewer window is opened. */
|
|
48
|
+
viewer?: ViewerLaunchConfig;
|
|
49
|
+
/**
|
|
50
|
+
* Permission dialogs to recognise beyond the built-in ones (`dialogs.ts`):
|
|
51
|
+
* the entries that compile. The rest are reported and left out.
|
|
52
|
+
*/
|
|
53
|
+
dialogs?: DialogSignature[];
|
|
54
|
+
/**
|
|
55
|
+
* Agent signatures beyond the built-in ones, or replacing one by name
|
|
56
|
+
* (`reasoning.ts`): the entries that compile. The rest are reported and
|
|
57
|
+
* left out.
|
|
58
|
+
*/
|
|
59
|
+
agentSignatures?: AgentSignature[];
|
|
60
|
+
/**
|
|
61
|
+
* The variables each agent's sessions receive from the environment of the
|
|
62
|
+
* client that starts them (`agents.passEnv`, design §4.6), by agent name.
|
|
63
|
+
* The MCP server and the CLI hold the values and apply the lists; the
|
|
64
|
+
* daemon only checks them, so a mistake is reported in `daemon.log` too.
|
|
65
|
+
*/
|
|
66
|
+
agentPassEnv?: Record<string, string[]>;
|
|
67
|
+
}
|
|
68
|
+
export interface ParsedDaemonConfig {
|
|
69
|
+
config: DaemonConfig;
|
|
70
|
+
/** Human-readable problems. The daemon logs each one loudly at startup. */
|
|
71
|
+
problems: string[];
|
|
72
|
+
}
|
|
73
|
+
/** Validate a parsed config value. `undefined` means "no file": pure defaults. */
|
|
74
|
+
export declare function parseDaemonConfig(raw: unknown): ParsedDaemonConfig;
|
|
75
|
+
/** Read and validate the config file. ENOENT ⇒ defaults; unreadable ⇒ fail closed. */
|
|
76
|
+
export declare function readDaemonConfig(home: string): ParsedDaemonConfig;
|