@chrischall/warapp-mcp 0.0.0 → 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/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +199 -8
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +159 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +551 -0
- package/dist/analysis/opponents.js +1245 -0
- package/dist/analysis/outlook.js +633 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1218 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +927 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2922 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +577 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1109 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +117959 -36172
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +192 -0
- package/dist/client.js +361 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/live.js +278 -0
- package/dist/map-stats.js +152 -0
- package/dist/map-store.js +204 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +180 -0
- package/dist/normalize.js +411 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +138 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +229 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +677 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +531 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +215 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +269 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2168 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +146 -0
- package/dist/tools/chat.js +483 -0
- package/dist/tools/check-plan.js +688 -0
- package/dist/tools/commander-check.js +102 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/game.js +126 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +142 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-stats.js +117 -0
- package/dist/tools/map.js +133 -0
- package/dist/tools/my-games.js +123 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +151 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/picks.js +292 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +385 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +200 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender.js +240 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +182 -0
- package/dist/tools/turn.js +2761 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +275 -0
- package/dist/tools/write-kit.js +158 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +452 -0
- package/dist/version.js +4 -0
- package/manifest.json +174 -4
- package/mint.yaml +101 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +969 -21
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Linking (and relinking) the chrome-less "direct" session.
|
|
2
|
+
//
|
|
3
|
+
// The direct transport (src/direct/transport.ts) authenticates every war.app
|
|
4
|
+
// request with the signed-in `AuthToken` cookie (live-verified 2026-11-12).
|
|
5
|
+
// {@link linkSession} captures that cookie ONCE off the user's signed-in
|
|
6
|
+
// war.app tab through the browser bridge (mcp-utils `captureBearerHeader`,
|
|
7
|
+
// which snapshots the `Cookie` request header via the fetchproxy server's
|
|
8
|
+
// `captureRequestHeader`), extracts the `AuthToken` value, and stores it with
|
|
9
|
+
// M1's on-disk hygiene. The bridge is touched only here, never on the hot
|
|
10
|
+
// path — once linked, every later call is a plain server-side HTTPS request.
|
|
11
|
+
//
|
|
12
|
+
// SECURITY: the AuthToken is a FULL-ACCOUNT credential. linkSession never
|
|
13
|
+
// returns it, never logs it, never puts it in an error: it returns only when
|
|
14
|
+
// the capture happened ({@link LinkResult} is `capturedAt` + `host`). Every
|
|
15
|
+
// cookie but `AuthToken` is discarded. A capture that yields no AuthToken
|
|
16
|
+
// throws {@link NoAuthTokenError} and stores nothing. Direct mode is
|
|
17
|
+
// LOCAL-ONLY — the hosted path keeps the browser bridge and stores no token.
|
|
18
|
+
import { McpToolError, readPortEnv } from '@chrischall/mcp-utils';
|
|
19
|
+
import { captureBearerHeader } from '@chrischall/mcp-utils/fetchproxy';
|
|
20
|
+
import { isHosted } from '../hosted.js';
|
|
21
|
+
import { DEFAULT_WS_PORT, SERVER_NAME, WS_PORT_ENV } from '../transport-bridge.js';
|
|
22
|
+
import { VERSION } from '../version.js';
|
|
23
|
+
import { scrubError, SESSION_HOST, writeSession } from './session.js';
|
|
24
|
+
/** The cookie that carries the signed-in war.app session. */
|
|
25
|
+
const AUTH_COOKIE = 'AuthToken';
|
|
26
|
+
/** The capture waits this long (ms) for the signed-in tab to make a request. */
|
|
27
|
+
const DEFAULT_CAPTURE_WINDOW_MS = 60_000;
|
|
28
|
+
/** No `AuthToken` cookie was present on the captured war.app request. */
|
|
29
|
+
export class NoAuthTokenError extends McpToolError {
|
|
30
|
+
name = 'NoAuthTokenError';
|
|
31
|
+
constructor() {
|
|
32
|
+
super(`No ${AUTH_COOKIE} cookie was found on the war.app request. Sign in to war.app in the bridged tab, then retry.`, { kind: 'no_credential' });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The `AuthToken` value from a `Cookie` header, or `null` when it is absent or
|
|
37
|
+
* empty. Only an exact `AuthToken` cookie counts; every other cookie (and a
|
|
38
|
+
* name that merely ends in `AuthToken`) is ignored.
|
|
39
|
+
*/
|
|
40
|
+
export function authTokenFromCookie(cookie) {
|
|
41
|
+
for (const part of cookie.split(';')) {
|
|
42
|
+
const eq = part.indexOf('=');
|
|
43
|
+
if (eq < 0)
|
|
44
|
+
continue;
|
|
45
|
+
if (part.slice(0, eq).trim() !== AUTH_COOKIE)
|
|
46
|
+
continue;
|
|
47
|
+
const value = part.slice(eq + 1).trim();
|
|
48
|
+
return value.length > 0 ? value : null;
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
/** The default capture: snapshot the `Cookie` header off a war.app request through the bridge. */
|
|
53
|
+
function captureWarappCookie(opts) {
|
|
54
|
+
const env = opts.env ?? process.env;
|
|
55
|
+
return captureBearerHeader({
|
|
56
|
+
captures: [{ host: SESSION_HOST, headerName: 'Cookie' }],
|
|
57
|
+
domains: [SESSION_HOST],
|
|
58
|
+
windowMs: opts.windowMs ?? DEFAULT_CAPTURE_WINDOW_MS,
|
|
59
|
+
serverName: SERVER_NAME,
|
|
60
|
+
version: VERSION,
|
|
61
|
+
// The AuthToken is parsed out of the whole Cookie header ourselves, so keep
|
|
62
|
+
// it intact (no Bearer-stripping) and never reject on length.
|
|
63
|
+
minLength: 1,
|
|
64
|
+
normalise: (raw) => raw,
|
|
65
|
+
port: opts.port ?? readPortEnv(WS_PORT_ENV, DEFAULT_WS_PORT, { env }),
|
|
66
|
+
what: 'your war.app session',
|
|
67
|
+
guidance: 'Open a signed-in war.app tab (e.g. war.app/MultiPlayer) and load or click into a game so the tab sends a ' +
|
|
68
|
+
'request, then retry.',
|
|
69
|
+
transportOptions: { env },
|
|
70
|
+
...(opts.createTransport ? { createTransport: opts.createTransport } : {}),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Capture the signed-in war.app session cookie through the bridge, extract the
|
|
75
|
+
* `AuthToken`, and store it (M1). Returns only {@link LinkResult} — when and
|
|
76
|
+
* where, never the token. Throws {@link NoAuthTokenError} (storing nothing)
|
|
77
|
+
* when the captured cookie carries no `AuthToken`.
|
|
78
|
+
*/
|
|
79
|
+
export async function linkSession(opts = {}) {
|
|
80
|
+
// Local-only: a hosted child never pulls the full-account cookie into memory
|
|
81
|
+
// (the tool is not registered there, and writeSession refuses too).
|
|
82
|
+
if (isHosted(opts.env ?? process.env)) {
|
|
83
|
+
throw new McpToolError('The direct transport is local-only: a hosted server never links a war.app session.', {
|
|
84
|
+
kind: 'unknown',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const cookie = await (opts.capture ?? (() => captureWarappCookie(opts)))();
|
|
88
|
+
const authToken = authTokenFromCookie(cookie);
|
|
89
|
+
if (!authToken)
|
|
90
|
+
throw new NoAuthTokenError();
|
|
91
|
+
const capturedAt = (opts.now?.() ?? new Date()).toISOString();
|
|
92
|
+
const session = { authToken, capturedAt, host: SESSION_HOST };
|
|
93
|
+
try {
|
|
94
|
+
await (opts.save ?? ((s) => writeSession(s, opts.env)))(session);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
// Whatever failed after the capture never carries the token or the rest
|
|
98
|
+
// of the captured cookie (other session cookies) out in its error.
|
|
99
|
+
throw scrubError(err, [authToken, cookie]);
|
|
100
|
+
}
|
|
101
|
+
return { capturedAt, host: SESSION_HOST };
|
|
102
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// The signed-in war.app session for the chrome-less "direct" transport.
|
|
2
|
+
//
|
|
3
|
+
// The AuthToken cookie is a FULL-ACCOUNT credential (live-verified 2026-11-12
|
|
4
|
+
// to authenticate both the server-rendered pages and the binary /Services
|
|
5
|
+
// protocol on its own). It is stored at `<dataDir>/session.json`
|
|
6
|
+
// (`WARAPP_SESSION_FILE` overrides): directory `0700` (an existing directory
|
|
7
|
+
// of ours is tightened to it; another user's is refused, and so is root's when
|
|
8
|
+
// others can write to it; on macOS any ACL on our directory and on the file is
|
|
9
|
+
// removed, since mode bits do not govern ACLs), file `0600`, written atomically through a randomly named tmp file
|
|
10
|
+
// created exclusively (never an existing file, never through a symlink) and
|
|
11
|
+
// renamed into place, so a crash never leaves a half-written token.
|
|
12
|
+
//
|
|
13
|
+
// SECURITY: treat the AuthToken exactly like WARAPP_API_TOKEN — never log it,
|
|
14
|
+
// never put it in a tool result, never leave it in an error or hint string.
|
|
15
|
+
// {@link scrubToken} removes it from any upstream string BY VALUE (reusing
|
|
16
|
+
// mcp-utils `stripBearer`, so a `Cookie:`/`Bearer`-prefixed form is caught too,
|
|
17
|
+
// and src/redact.ts, so its encoded spellings are too).
|
|
18
|
+
//
|
|
19
|
+
// Deferred, like every other credential in this server: nothing here runs at
|
|
20
|
+
// import time, the server boots without a session, and a missing OR corrupt
|
|
21
|
+
// file reads as `null` — never a throw.
|
|
22
|
+
//
|
|
23
|
+
// LOCAL-ONLY: on a hosted child (src/hosted.ts) the store reads as empty — a
|
|
24
|
+
// session file already in the data dir is never read — and refuses to write.
|
|
25
|
+
import { execFile } from 'node:child_process';
|
|
26
|
+
import { randomBytes } from 'node:crypto';
|
|
27
|
+
import { readFileSync } from 'node:fs';
|
|
28
|
+
import { chmod, mkdir, open, readFile, rename, rm, stat } from 'node:fs/promises';
|
|
29
|
+
import { dirname, join, resolve } from 'node:path';
|
|
30
|
+
import { promisify } from 'node:util';
|
|
31
|
+
import { McpToolError, readEnvVar } from '@chrischall/mcp-utils';
|
|
32
|
+
import { stripBearer } from '@chrischall/mcp-utils/fetchproxy';
|
|
33
|
+
import { dataDir } from '../config.js';
|
|
34
|
+
import { isHosted } from '../hosted.js';
|
|
35
|
+
import { scrubSecrets } from '../redact.js';
|
|
36
|
+
/** The only host a session is ever stored for. */
|
|
37
|
+
export const SESSION_HOST = 'war.app';
|
|
38
|
+
const isRec = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
39
|
+
/**
|
|
40
|
+
* Where the session is stored: `WARAPP_SESSION_FILE` if set, else
|
|
41
|
+
* `<dataDir>/session.json` (the same `dataDir` the client bundle cache uses).
|
|
42
|
+
* {@link writeSession} makes the containing directory `0700`, so the variable
|
|
43
|
+
* should name a file in a directory of its own, not a shared folder.
|
|
44
|
+
*/
|
|
45
|
+
export function sessionFilePath(env = process.env) {
|
|
46
|
+
return readEnvVar('WARAPP_SESSION_FILE', { env }) ?? join(dataDir(env), 'session.json');
|
|
47
|
+
}
|
|
48
|
+
/** Parse a decoded session record, returning `null` for any unusable shape. */
|
|
49
|
+
function parseSession(raw) {
|
|
50
|
+
if (!isRec(raw))
|
|
51
|
+
return null;
|
|
52
|
+
const { authToken, capturedAt, host } = raw;
|
|
53
|
+
if (typeof authToken !== 'string' || authToken.length === 0)
|
|
54
|
+
return null;
|
|
55
|
+
if (typeof capturedAt !== 'string' || capturedAt.length === 0)
|
|
56
|
+
return null;
|
|
57
|
+
if (host !== SESSION_HOST)
|
|
58
|
+
return null;
|
|
59
|
+
return { authToken, capturedAt, host };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The linked session, or `null` when none is linked or the file is missing or
|
|
63
|
+
* unusable (wrong shape, wrong host, not JSON). Never throws.
|
|
64
|
+
*/
|
|
65
|
+
export async function readSession(env = process.env) {
|
|
66
|
+
if (isHosted(env))
|
|
67
|
+
return null;
|
|
68
|
+
try {
|
|
69
|
+
return parseSession(JSON.parse(await readFile(sessionFilePath(env), 'utf8')));
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* {@link readSession}'s synchronous twin, for the one caller that needs the
|
|
77
|
+
* session inside a synchronous getter (the adaptive healthcheck's `path`, which
|
|
78
|
+
* reports only the non-secret linked/`capturedAt` facts — never the token).
|
|
79
|
+
* Same guarantees: a missing or corrupt file reads as `null`, never a throw.
|
|
80
|
+
*/
|
|
81
|
+
export function readSessionSync(env = process.env) {
|
|
82
|
+
if (isHosted(env))
|
|
83
|
+
return null;
|
|
84
|
+
try {
|
|
85
|
+
return parseSession(JSON.parse(readFileSync(sessionFilePath(env), 'utf8')));
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Judge the session directory from its `stat` and this process's uid
|
|
93
|
+
* (`undefined` where there are no POSIX owners: nothing to check). Ours with
|
|
94
|
+
* any group/other bit → `tighten` (to `0700`). Root's and writable by group or
|
|
95
|
+
* others → `refuse` (someone else could plant or swap files in it). Another
|
|
96
|
+
* user's → `refuse` whatever its mode bits say: we could write into it only
|
|
97
|
+
* through an ACL its owner controls, and the owner could add an inheritable
|
|
98
|
+
* entry that reaches the session file.
|
|
99
|
+
*/
|
|
100
|
+
export function sessionDirVerdict(st, myUid) {
|
|
101
|
+
if (myUid === undefined)
|
|
102
|
+
return 'ok';
|
|
103
|
+
if (st.uid === myUid)
|
|
104
|
+
return (st.mode & 0o077) !== 0 ? 'tighten' : 'ok';
|
|
105
|
+
if (st.uid !== 0)
|
|
106
|
+
return 'refuse';
|
|
107
|
+
return (st.mode & 0o022) !== 0 ? 'refuse' : 'ok';
|
|
108
|
+
}
|
|
109
|
+
const run = promisify(execFile);
|
|
110
|
+
/**
|
|
111
|
+
* macOS ACLs: the mode bits do not govern them, so `chmod 0700` leaves an
|
|
112
|
+
* "everyone allow read" entry in place, and an inheritable one on the
|
|
113
|
+
* directory is copied onto every file created in it — the session file
|
|
114
|
+
* included, whatever mode it was created with. On darwin these strip and
|
|
115
|
+
* detect an extended ACL with the system tools (Node has no ACL API). Linux
|
|
116
|
+
* POSIX ACLs need nothing here: their named entries are capped by the mask,
|
|
117
|
+
* which `0700`/`0600` set to nothing. Paths are absolute (never read as flags).
|
|
118
|
+
*/
|
|
119
|
+
const MACOS_ACLS = process.platform === 'darwin';
|
|
120
|
+
/**
|
|
121
|
+
* Whether `ls -led` output lists an ACL entry (` 0: group:everyone allow …` on
|
|
122
|
+
* a line after the first). The mode string's `+` is not enough: with extended
|
|
123
|
+
* attributes as well, macOS prints `@` there instead.
|
|
124
|
+
*/
|
|
125
|
+
export function lsShowsAcl(stdout) {
|
|
126
|
+
return stdout
|
|
127
|
+
.split('\n')
|
|
128
|
+
.slice(1)
|
|
129
|
+
.some((line) => /^\s*\d+: /.test(line));
|
|
130
|
+
}
|
|
131
|
+
async function hasAcl(path) {
|
|
132
|
+
const { stdout } = await run('/bin/ls', ['-led', path]);
|
|
133
|
+
return lsShowsAcl(stdout);
|
|
134
|
+
}
|
|
135
|
+
/** Remove every ACL entry from `path` (ours), then refuse unless none is left. */
|
|
136
|
+
async function stripAcl(path, what) {
|
|
137
|
+
await run('/bin/chmod', ['-N', path]);
|
|
138
|
+
if (await hasAcl(path)) {
|
|
139
|
+
throw new McpToolError(`Refusing to store the war.app session: could not remove the access control list on ${what}.`, {
|
|
140
|
+
hint: 'Point WARAPP_SESSION_FILE (or MCP_DATA_DIR) at a directory only you can read, with no ACL (`ls -le`).',
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** A fresh, unpredictable tmp file name next to `path`. */
|
|
145
|
+
export function sessionTmpPath(path) {
|
|
146
|
+
return `${path}.${randomBytes(16).toString('hex')}.tmp`;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Store a session: the directory is created `0700` (an existing one of ours is
|
|
150
|
+
* tightened to `0700`; one others can write to that is not ours is refused),
|
|
151
|
+
* and the file is written `0600` atomically — a randomly named tmp file,
|
|
152
|
+
* created exclusively (it never opens an existing file or follows a symlink),
|
|
153
|
+
* then renamed over the target; the tmp file is removed if anything fails.
|
|
154
|
+
* Unlike {@link readSession} this propagates I/O errors, so a deliberate link
|
|
155
|
+
* action can report that it failed.
|
|
156
|
+
*/
|
|
157
|
+
export async function writeSession(session, env = process.env) {
|
|
158
|
+
if (isHosted(env)) {
|
|
159
|
+
throw new McpToolError('The direct transport is local-only: a hosted server never stores a war.app session.', {
|
|
160
|
+
kind: 'unknown',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
const path = resolve(sessionFilePath(env));
|
|
164
|
+
const dir = dirname(path);
|
|
165
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
166
|
+
// mkdir's mode applies only to a directory it creates.
|
|
167
|
+
const st = await stat(dir);
|
|
168
|
+
const myUid = process.getuid?.();
|
|
169
|
+
const verdict = sessionDirVerdict(st, myUid);
|
|
170
|
+
if (verdict === 'refuse') {
|
|
171
|
+
throw new McpToolError(`Refusing to store the war.app session in ${dir}: it is not yours, and others could reach files in it.`, {
|
|
172
|
+
hint: 'Point WARAPP_SESSION_FILE (or MCP_DATA_DIR) at a directory of your own that only you can write to.',
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (verdict === 'tighten')
|
|
176
|
+
await chmod(dir, 0o700);
|
|
177
|
+
if (MACOS_ACLS) {
|
|
178
|
+
// Before the file exists: an inheritable entry would be copied onto it at
|
|
179
|
+
// creation, and a reader that opened it then would keep that access.
|
|
180
|
+
if (st.uid === myUid)
|
|
181
|
+
await stripAcl(dir, dir);
|
|
182
|
+
else if (await hasAcl(dir)) {
|
|
183
|
+
throw new McpToolError(`Refusing to store the war.app session in ${dir}: it carries an access control list and is not yours.`, {
|
|
184
|
+
hint: 'Point WARAPP_SESSION_FILE (or MCP_DATA_DIR) at a directory of your own that only you can read.',
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const tmp = sessionTmpPath(path);
|
|
189
|
+
// 'wx' = O_CREAT | O_EXCL: fails on any existing entry, a symlink included.
|
|
190
|
+
const file = await open(tmp, 'wx', 0o600);
|
|
191
|
+
try {
|
|
192
|
+
try {
|
|
193
|
+
// Still empty: make sure it carries nothing but 0600 before the token goes in.
|
|
194
|
+
await file.chmod(0o600);
|
|
195
|
+
if (MACOS_ACLS)
|
|
196
|
+
await stripAcl(tmp, 'the session file');
|
|
197
|
+
await file.writeFile(JSON.stringify(session));
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
await file.close();
|
|
201
|
+
}
|
|
202
|
+
await rename(tmp, path);
|
|
203
|
+
}
|
|
204
|
+
catch (e) {
|
|
205
|
+
await rm(tmp, { force: true }).catch(() => { });
|
|
206
|
+
throw e;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** Remove the stored session. A no-op (never throws) when none is linked. */
|
|
210
|
+
export async function clearSession(env = process.env) {
|
|
211
|
+
await rm(sessionFilePath(env), { force: true });
|
|
212
|
+
}
|
|
213
|
+
/** The marker a scrubbed AuthToken leaves behind. */
|
|
214
|
+
const SCRUBBED = '[warapp-session]';
|
|
215
|
+
/**
|
|
216
|
+
* Remove `token` from `s` by value — the raw value and its
|
|
217
|
+
* {@link stripBearer}-normalised form (so a `Cookie: AuthToken=…` or
|
|
218
|
+
* `Bearer …` slice is caught), each in every spelling src/redact.ts knows
|
|
219
|
+
* (JSON-escaped, percent-encoded, form-encoded), so a token echoed in a URL,
|
|
220
|
+
* a form body or a JSON string is caught. Wire this around any upstream string
|
|
221
|
+
* that could carry the session, the way the GameFeed client scrubs the email
|
|
222
|
+
* and API token. A falsy token leaves `s` unchanged.
|
|
223
|
+
*/
|
|
224
|
+
export function scrubToken(s, token) {
|
|
225
|
+
if (!token)
|
|
226
|
+
return s;
|
|
227
|
+
return scrubSecrets(s, [token, stripBearer(token)], SCRUBBED);
|
|
228
|
+
}
|
|
229
|
+
/** Every string an error carries that could reach a caller: message, hint, stack, and its cause chain. */
|
|
230
|
+
function errorStrings(err, depth = 0) {
|
|
231
|
+
if (depth > 4 || typeof err !== 'object' || err === null)
|
|
232
|
+
return typeof err === 'string' ? [err] : [];
|
|
233
|
+
const e = err;
|
|
234
|
+
const own = [e.message, e.hint, e.stack].filter((v) => typeof v === 'string');
|
|
235
|
+
return [...own, ...errorStrings(e.cause, depth + 1)];
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Whether any of `tokens` (in any {@link scrubToken} form) appears anywhere in
|
|
239
|
+
* `err` — its message, hint, stack, or a short `cause` chain.
|
|
240
|
+
*/
|
|
241
|
+
function errorCarriesToken(err, tokens) {
|
|
242
|
+
const strings = errorStrings(err);
|
|
243
|
+
return tokens.some((t) => !!t && strings.some((s) => scrubToken(s, t) !== s));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* `err` with every form of each of `tokens` removed: returned unchanged when
|
|
247
|
+
* none appears anywhere in it (message, hint, stack, cause chain); otherwise
|
|
248
|
+
* rebuilt as an {@link McpToolError} carrying the scrubbed message and hint and
|
|
249
|
+
* the original `kind`/`status`, with NO `cause` (the cause is where a token
|
|
250
|
+
* could still hide) and a fresh stack.
|
|
251
|
+
*/
|
|
252
|
+
export function scrubError(err, tokens) {
|
|
253
|
+
if (!errorCarriesToken(err, tokens))
|
|
254
|
+
return err;
|
|
255
|
+
// Longest first, so a token inside a longer one (the AuthToken inside the
|
|
256
|
+
// whole captured Cookie header) never leaves the longer one half-scrubbed.
|
|
257
|
+
const ordered = tokens.filter((t) => !!t).sort((a, b) => b.length - a.length);
|
|
258
|
+
const scrub = (s) => ordered.reduce((acc, t) => scrubToken(acc, t), s);
|
|
259
|
+
const e = (typeof err === 'object' && err !== null ? err : {});
|
|
260
|
+
const message = scrub(typeof e.message === 'string' ? e.message : String(err));
|
|
261
|
+
const kind = err instanceof McpToolError && err.kind ? err.kind : 'transport';
|
|
262
|
+
return new McpToolError(message, {
|
|
263
|
+
kind,
|
|
264
|
+
...(typeof e.status === 'number' ? { status: e.status } : {}),
|
|
265
|
+
...(typeof e.hint === 'string' ? { hint: scrub(e.hint) } : {}),
|
|
266
|
+
});
|
|
267
|
+
}
|