pi-harness-runtime 0.9.7 → 0.9.10
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 +28 -12
- package/README.md +42 -5
- package/footer-status.ts +108 -11
- package/harness/e2e/minimax-quota-scraper.ts +46 -4
- package/index.ts +224 -16
- package/mirror.ts +194 -18
- package/package.json +1 -1
- package/packages/a2a-adapter/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-refactor/package.json +1 -1
- package/packages/cache-strategy/package.json +1 -1
- package/packages/capability-registry/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/cli-plugin-sdk/package.json +1 -1
- package/packages/code-generation/package.json +1 -1
- package/packages/code-review/package.json +1 -1
- package/packages/codex-adapter/package.json +1 -1
- package/packages/context-compiler/package.json +1 -1
- package/packages/context-manager/package.json +1 -1
- package/packages/cookie-sanitizer/README.md +141 -0
- package/packages/cookie-sanitizer/dist/atomic-write.d.ts +38 -0
- package/packages/cookie-sanitizer/dist/atomic-write.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/atomic-write.js +67 -0
- package/packages/cookie-sanitizer/dist/atomic-write.js.map +1 -0
- package/packages/cookie-sanitizer/dist/detect-format.d.ts +36 -0
- package/packages/cookie-sanitizer/dist/detect-format.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/detect-format.js +88 -0
- package/packages/cookie-sanitizer/dist/detect-format.js.map +1 -0
- package/packages/cookie-sanitizer/dist/index.d.ts +41 -0
- package/packages/cookie-sanitizer/dist/index.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/index.js +39 -0
- package/packages/cookie-sanitizer/dist/index.js.map +1 -0
- package/packages/cookie-sanitizer/dist/infer-provider.d.ts +29 -0
- package/packages/cookie-sanitizer/dist/infer-provider.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/infer-provider.js +96 -0
- package/packages/cookie-sanitizer/dist/infer-provider.js.map +1 -0
- package/packages/cookie-sanitizer/dist/normalize.d.ts +26 -0
- package/packages/cookie-sanitizer/dist/normalize.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/normalize.js +75 -0
- package/packages/cookie-sanitizer/dist/normalize.js.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-json.d.ts +45 -0
- package/packages/cookie-sanitizer/dist/parse-json.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-json.js +108 -0
- package/packages/cookie-sanitizer/dist/parse-json.js.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-netscape.d.ts +43 -0
- package/packages/cookie-sanitizer/dist/parse-netscape.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-netscape.js +99 -0
- package/packages/cookie-sanitizer/dist/parse-netscape.js.map +1 -0
- package/packages/cookie-sanitizer/dist/sync.d.ts +62 -0
- package/packages/cookie-sanitizer/dist/sync.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/sync.js +266 -0
- package/packages/cookie-sanitizer/dist/sync.js.map +1 -0
- package/packages/cookie-sanitizer/dist/types.d.ts +100 -0
- package/packages/cookie-sanitizer/dist/types.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/types.js +13 -0
- package/packages/cookie-sanitizer/dist/types.js.map +1 -0
- package/packages/cookie-sanitizer/dist/watcher.d.ts +51 -0
- package/packages/cookie-sanitizer/dist/watcher.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/watcher.js +148 -0
- package/packages/cookie-sanitizer/dist/watcher.js.map +1 -0
- package/packages/cookie-sanitizer/package.json +27 -0
- package/packages/cookie-sanitizer/src/atomic-write.ts +79 -0
- package/packages/cookie-sanitizer/src/detect-format.ts +102 -0
- package/packages/cookie-sanitizer/src/index.ts +89 -0
- package/packages/cookie-sanitizer/src/infer-provider.ts +103 -0
- package/packages/cookie-sanitizer/src/normalize.ts +92 -0
- package/packages/cookie-sanitizer/src/parse-json.ts +122 -0
- package/packages/cookie-sanitizer/src/parse-netscape.ts +118 -0
- package/packages/cookie-sanitizer/src/sync.ts +304 -0
- package/packages/cookie-sanitizer/src/types.ts +101 -0
- package/packages/cookie-sanitizer/src/watcher.ts +180 -0
- package/packages/cookie-sanitizer/test/infer.test.ts +110 -0
- package/packages/cookie-sanitizer/test/normalize.test.ts +113 -0
- package/packages/cookie-sanitizer/test/parse.test.ts +204 -0
- package/packages/cookie-sanitizer/test/sync.test.ts +231 -0
- package/packages/cookie-sanitizer/test/watcher.test.ts +27 -0
- package/packages/cookie-sanitizer/tsconfig.json +8 -0
- package/packages/cost-optimizer/package.json +1 -1
- package/packages/django-plugin/package.json +1 -1
- package/packages/doc-generator/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/evaluation-runner/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/feedback-collector/package.json +1 -1
- package/packages/framework-detector/package.json +1 -1
- package/packages/framework-plugin-sdk/package.json +1 -1
- package/packages/frappe-plugin/package.json +1 -1
- package/packages/generic-web-plugin/package.json +1 -1
- package/packages/health-monitor/package.json +1 -1
- package/packages/laravel-plugin/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/mcp-adapter/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/model-registry/package.json +1 -1
- package/packages/nextjs-plugin/package.json +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/observability/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/project-analyzer/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/prompt-versioning/package.json +1 -1
- package/packages/provider-adapter-sdk/package.json +1 -1
- package/packages/provider-router/package.json +1 -1
- package/packages/provider-selector/package.json +1 -1
- package/packages/providers/dist/index.d.ts +1 -0
- package/packages/providers/dist/index.d.ts.map +1 -1
- package/packages/providers/dist/index.js +1 -0
- package/packages/providers/dist/index.js.map +1 -1
- package/packages/providers/dist/provider-id.d.ts +39 -0
- package/packages/providers/dist/provider-id.d.ts.map +1 -0
- package/packages/providers/dist/provider-id.js +96 -0
- package/packages/providers/dist/provider-id.js.map +1 -0
- package/packages/providers/package.json +1 -1
- package/packages/providers/src/index.ts +1 -0
- package/packages/providers/src/provider-id.ts +110 -0
- package/packages/quota-manager/package.json +1 -1
- package/packages/rate-limiter/package.json +1 -1
- package/packages/react-vite-plugin/package.json +1 -1
- package/packages/release-manager/package.json +1 -1
- package/packages/requirement-compiler/package.json +1 -1
- package/packages/runtime/package.json +1 -1
- package/packages/scheduler/package.json +1 -1
- package/packages/session/package.json +1 -1
- package/packages/shared-context/package.json +1 -1
- package/packages/skill-registry/package.json +1 -1
- package/packages/sprint-planner/package.json +1 -1
- package/packages/task-compiler/package.json +1 -1
- package/packages/test-data-generator/package.json +1 -1
- package/packages/test-generator/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/token-optimizer/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/worktree/package.json +1 -1
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Atomic file write — write to temp, fsync, then rename.
|
|
3
|
+
*
|
|
4
|
+
* POSIX `rename(2)` is atomic — on partial-write crash, the prior target
|
|
5
|
+
* file remains valid. We pair this with `fsync(tempfile)` so the temp
|
|
6
|
+
* file's data is durable on disk before the rename.
|
|
7
|
+
*
|
|
8
|
+
* Permissions: `0o600` (owner R/W only) to keep the canonical cache
|
|
9
|
+
* owner-readable.
|
|
10
|
+
*
|
|
11
|
+
* Sec-001: Cookie values go into the file content. The caller is
|
|
12
|
+
* responsible for value-handling hygiene; this module just writes
|
|
13
|
+
* bytes. We do not log the content.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
mkdirSync,
|
|
18
|
+
openSync,
|
|
19
|
+
writeSync,
|
|
20
|
+
fsyncSync,
|
|
21
|
+
closeSync,
|
|
22
|
+
renameSync,
|
|
23
|
+
chmodSync,
|
|
24
|
+
} from "node:fs";
|
|
25
|
+
import { dirname, join } from "node:path";
|
|
26
|
+
import { randomBytes } from "node:crypto";
|
|
27
|
+
import type { ProviderId } from "./types.js";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Atomically write `content` to `finalPath`.
|
|
31
|
+
*
|
|
32
|
+
* Steps:
|
|
33
|
+
* 1. mkdir -p the parent directory
|
|
34
|
+
* 2. write to <finalPath>.tmp.<rand> with mode 0o600
|
|
35
|
+
* 3. fsync the temp file
|
|
36
|
+
* 4. rename temp → final (atomic on POSIX)
|
|
37
|
+
* 5. chmod final to 0o600 (in case rename preserved looser perms)
|
|
38
|
+
*
|
|
39
|
+
* Throws if any step fails. The original `finalPath` is unchanged
|
|
40
|
+
* until step 4 succeeds.
|
|
41
|
+
*/
|
|
42
|
+
export function atomicWriteFileSync(finalPath: string, content: string): void {
|
|
43
|
+
mkdirSync(dirname(finalPath), { recursive: true });
|
|
44
|
+
|
|
45
|
+
const rand = randomBytes(6).toString("hex");
|
|
46
|
+
const tempPath = `${finalPath}.tmp.${rand}`;
|
|
47
|
+
|
|
48
|
+
let fd: number | undefined;
|
|
49
|
+
try {
|
|
50
|
+
fd = openSync(tempPath, "w", 0o600);
|
|
51
|
+
writeSync(fd, Buffer.from(content, "utf8"));
|
|
52
|
+
fsyncSync(fd);
|
|
53
|
+
} finally {
|
|
54
|
+
if (fd !== undefined) closeSync(fd);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
renameSync(tempPath, finalPath);
|
|
58
|
+
|
|
59
|
+
// Belt-and-braces in case rename preserved a looser mode from an
|
|
60
|
+
// existing file on some systems.
|
|
61
|
+
try {
|
|
62
|
+
chmodSync(finalPath, 0o600);
|
|
63
|
+
} catch {
|
|
64
|
+
// Filesystems that don't support chmod (e.g. FAT32) are not a
|
|
65
|
+
// target platform here — drop the error silently.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Compute the default cache path for a given provider, e.g.:
|
|
71
|
+
* defaultCachePathFor("minimax") → "~/.config/minimax-cookies.txt"
|
|
72
|
+
*
|
|
73
|
+
* The home directory is read via `process.env.HOME` so tests can
|
|
74
|
+
* override it.
|
|
75
|
+
*/
|
|
76
|
+
export function defaultCachePathFor(provider: ProviderId): string {
|
|
77
|
+
const home = process.env.HOME ?? "/tmp";
|
|
78
|
+
return join(home, ".config", `${provider}-cookies.txt`);
|
|
79
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format detection — does the input look like Netscape or EditThisCookie JSON?
|
|
3
|
+
*
|
|
4
|
+
* Detection rule:
|
|
5
|
+
* - File extension `.json` AND first non-blank byte is `[` or `{` → JSON
|
|
6
|
+
* - File content has `# Netscape HTTP Cookie File` header OR
|
|
7
|
+
* ≥ 1 tab-separated 7-column row → Netscape
|
|
8
|
+
* - Otherwise → unknown
|
|
9
|
+
*
|
|
10
|
+
* Cookie VALUES are never copied into return values that get logged.
|
|
11
|
+
* Only the basename and a 64-char prefix of the first non-blank line.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { basename } from "node:path";
|
|
15
|
+
import type { CookieFileFormat, DetectedFile } from "./types.js";
|
|
16
|
+
|
|
17
|
+
const HEADER_RE = /^# Netscape HTTP Cookie File/im;
|
|
18
|
+
const MIN_TAB_COLS = 7;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Returns the basename only, never a path that could leak directory
|
|
22
|
+
* structure. Safe to log.
|
|
23
|
+
*/
|
|
24
|
+
export function safeBasename(filePath: string): string {
|
|
25
|
+
return basename(filePath);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Safely truncated preview of a file's content (for diagnostics).
|
|
30
|
+
* Strips cookie values to avoid leaking secrets.
|
|
31
|
+
*
|
|
32
|
+
* Heuristic: any line that has a tab-separated name=value pair,
|
|
33
|
+
* replace the value column with `<redacted>`.
|
|
34
|
+
*/
|
|
35
|
+
export function safePreview(text: string, maxChars = 64): string {
|
|
36
|
+
const firstLine =
|
|
37
|
+
text
|
|
38
|
+
.split("\n")
|
|
39
|
+
.map((l) => l.trim())
|
|
40
|
+
.find((l) => l.length > 0) ?? "";
|
|
41
|
+
const redacted = firstLine
|
|
42
|
+
.split("\t")
|
|
43
|
+
.map((col, i) => (i >= 5 ? "<redacted>" : col))
|
|
44
|
+
.join("\t");
|
|
45
|
+
return redacted.length > maxChars
|
|
46
|
+
? `${redacted.slice(0, maxChars)}…`
|
|
47
|
+
: redacted;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Detect the format of cookie file text.
|
|
52
|
+
*/
|
|
53
|
+
export function detectFormat(text: string): CookieFileFormat | null {
|
|
54
|
+
if (typeof text !== "string" || text.length === 0) return null;
|
|
55
|
+
|
|
56
|
+
const trimmed = text.trimStart();
|
|
57
|
+
const firstByte = trimmed[0];
|
|
58
|
+
|
|
59
|
+
// JSON heuristic — array or object at the top.
|
|
60
|
+
if (firstByte === "[" || firstByte === "{") {
|
|
61
|
+
return "edit-this-cookie-json";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Netscape heuristic — header OR a tab-separated 7-column row.
|
|
65
|
+
if (HEADER_RE.test(text)) {
|
|
66
|
+
return "netscape";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const lines = text.split(/\r?\n/);
|
|
70
|
+
for (const line of lines) {
|
|
71
|
+
if (!line || (line.startsWith("#") && !line.startsWith("#HttpOnly_"))) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const trimmedLine = line.startsWith("#HttpOnly_")
|
|
75
|
+
? line.slice("#HttpOnly_".length)
|
|
76
|
+
: line;
|
|
77
|
+
const cols = trimmedLine.split("\t");
|
|
78
|
+
if (cols.length >= MIN_TAB_COLS) {
|
|
79
|
+
return "netscape";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Build a DetectedFile from a path and its text contents.
|
|
88
|
+
* Used by sync.ts after reading the file from disk.
|
|
89
|
+
*/
|
|
90
|
+
export function buildDetectedFile(
|
|
91
|
+
filePath: string,
|
|
92
|
+
text: string,
|
|
93
|
+
): DetectedFile {
|
|
94
|
+
const format = detectFormat(text);
|
|
95
|
+
return {
|
|
96
|
+
path: filePath,
|
|
97
|
+
basename: safeBasename(filePath),
|
|
98
|
+
format,
|
|
99
|
+
text,
|
|
100
|
+
// No error if format is null — unknown format is a normal case.
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie Sanitizer — public API.
|
|
3
|
+
*
|
|
4
|
+
* Forgiving input → strict canonical cache for downstream consumers
|
|
5
|
+
* (e.g. MiniMaxQuotaScraper reads only `~/.config/minimax-cookies.txt`).
|
|
6
|
+
*
|
|
7
|
+
* The package is provider-agnostic but currently shipping with:
|
|
8
|
+
* - filename hints: minimax, anthropic, openai, glm, openrouter
|
|
9
|
+
* - domain hints : the matching provider domains
|
|
10
|
+
*
|
|
11
|
+
* Inputs accepted (auto-detected per file):
|
|
12
|
+
* - Netscape HTTP Cookie File
|
|
13
|
+
* - EditThisCookie / Chrome devtools JSON export
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { sync, hasAnyCookieSource, CookieWatcher } from "@pi-harness/cookie-sanitizer";
|
|
18
|
+
*
|
|
19
|
+
* // One-shot sync.
|
|
20
|
+
* const result = sync();
|
|
21
|
+
* console.log(result.provider, result.cachePath, result.totalCookies);
|
|
22
|
+
*
|
|
23
|
+
* // Live updates.
|
|
24
|
+
* const watcher = new CookieWatcher({
|
|
25
|
+
* dropDir: "~/.pi-harness-runtime/cookies",
|
|
26
|
+
* onEvent: (e) => console.log(e),
|
|
27
|
+
* });
|
|
28
|
+
* watcher.start();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
sync,
|
|
34
|
+
hasAnyCookieSource,
|
|
35
|
+
inferCachePath,
|
|
36
|
+
enumerateDropFiles,
|
|
37
|
+
readInputFile,
|
|
38
|
+
DEFAULT_DROP_DIR,
|
|
39
|
+
} from "./sync.js";
|
|
40
|
+
|
|
41
|
+
export {
|
|
42
|
+
parseNetscape,
|
|
43
|
+
serializeNetscape,
|
|
44
|
+
} from "./parse-netscape.js";
|
|
45
|
+
|
|
46
|
+
export { parseEditThisCookieJson } from "./parse-json.js";
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
inferProvider,
|
|
50
|
+
inferProviderFromFilename,
|
|
51
|
+
inferProviderFromCookies,
|
|
52
|
+
knownProviders,
|
|
53
|
+
} from "./infer-provider.js";
|
|
54
|
+
|
|
55
|
+
export {
|
|
56
|
+
normalize,
|
|
57
|
+
domainMatchesProvider,
|
|
58
|
+
isExpired,
|
|
59
|
+
} from "./normalize.js";
|
|
60
|
+
|
|
61
|
+
export {
|
|
62
|
+
detectFormat,
|
|
63
|
+
buildDetectedFile,
|
|
64
|
+
safeBasename,
|
|
65
|
+
safePreview,
|
|
66
|
+
} from "./detect-format.js";
|
|
67
|
+
|
|
68
|
+
export {
|
|
69
|
+
atomicWriteFileSync,
|
|
70
|
+
defaultCachePathFor,
|
|
71
|
+
} from "./atomic-write.js";
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
CookieWatcher,
|
|
75
|
+
createWatcher,
|
|
76
|
+
defaultDropDir,
|
|
77
|
+
} from "./watcher.js";
|
|
78
|
+
|
|
79
|
+
export type {
|
|
80
|
+
CanonicalCookie,
|
|
81
|
+
CookieFileFormat,
|
|
82
|
+
DetectedFile,
|
|
83
|
+
ProviderId,
|
|
84
|
+
SyncOptions,
|
|
85
|
+
SyncResult,
|
|
86
|
+
WatcherEvent,
|
|
87
|
+
} from "./types.js";
|
|
88
|
+
|
|
89
|
+
export type { CookieWatcherConfig } from "./watcher.js";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider inference — figure out which provider a cookie dump belongs to
|
|
3
|
+
* without the user having to organize by subfolder.
|
|
4
|
+
*
|
|
5
|
+
* Three inference passes, first hit wins:
|
|
6
|
+
* 1. Filename hint (e.g. `minimax-*.txt`, `*minimax.json` → "minimax")
|
|
7
|
+
* 2. Dominant domain (≥ 80% of cookies on `*.minimax.io` → "minimax")
|
|
8
|
+
* 3. Nothing → null (caller will surface a friendly error)
|
|
9
|
+
*
|
|
10
|
+
* Provider hint from SyncOptions overrides all of the above.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { CanonicalCookie, ProviderId } from "./types.js";
|
|
14
|
+
|
|
15
|
+
const FILENAME_HINTS: Array<{ pattern: RegExp; provider: ProviderId }> = [
|
|
16
|
+
{ pattern: /minimax/i, provider: "minimax" },
|
|
17
|
+
{ pattern: /anthropic|claude/i, provider: "anthropic" },
|
|
18
|
+
{ pattern: /openai|gpt|chatgpt/i, provider: "openai" },
|
|
19
|
+
{ pattern: /glm|z\.ai|zhipuai/i, provider: "glm" },
|
|
20
|
+
{ pattern: /openrouter/i, provider: "openrouter" },
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const DOMAIN_HINTS: Array<{ pattern: RegExp; provider: ProviderId }> = [
|
|
24
|
+
{ pattern: /\.minimax\.io$|^minimax\.io$/i, provider: "minimax" },
|
|
25
|
+
{ pattern: /\.anthropic\.com$|^anthropic\.com$/i, provider: "anthropic" },
|
|
26
|
+
{
|
|
27
|
+
pattern: /\.openai\.com$|^openai\.com$|\.chatgpt\.com$|^chatgpt\.com$/i,
|
|
28
|
+
provider: "openai",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
pattern: /\.z\.ai$|^z\.ai$|\.zhipuai\.cn$|^zhipuai\.cn$/i,
|
|
32
|
+
provider: "glm",
|
|
33
|
+
},
|
|
34
|
+
{ pattern: /\.openrouter\.ai$|^openrouter\.ai$/i, provider: "openrouter" },
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** Returns the first provider whose filename pattern matches, or null. */
|
|
38
|
+
export function inferProviderFromFilename(filename: string): ProviderId | null {
|
|
39
|
+
if (!filename) return null;
|
|
40
|
+
for (const { pattern, provider } of FILENAME_HINTS) {
|
|
41
|
+
if (pattern.test(filename)) return provider;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Returns the provider whose domain is the dominant (≥ 80%) match. */
|
|
47
|
+
export function inferProviderFromCookies(
|
|
48
|
+
cookies: CanonicalCookie[],
|
|
49
|
+
): ProviderId | null {
|
|
50
|
+
if (!Array.isArray(cookies) || cookies.length === 0) return null;
|
|
51
|
+
|
|
52
|
+
const counts = new Map<ProviderId, number>();
|
|
53
|
+
let total = 0;
|
|
54
|
+
for (const c of cookies) {
|
|
55
|
+
if (!c.domain) continue;
|
|
56
|
+
total += 1;
|
|
57
|
+
for (const { pattern, provider } of DOMAIN_HINTS) {
|
|
58
|
+
if (pattern.test(c.domain.toLowerCase())) {
|
|
59
|
+
counts.set(provider, (counts.get(provider) ?? 0) + 1);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (total === 0) return null;
|
|
66
|
+
const threshold = Math.ceil(total * 0.8); // ≥ 80 %
|
|
67
|
+
let best: { provider: ProviderId; count: number } | null = null;
|
|
68
|
+
for (const [provider, count] of counts) {
|
|
69
|
+
if (count >= threshold && (!best || count > best.count)) {
|
|
70
|
+
best = { provider, count };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return best?.provider ?? null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Combined inference: hint → filename → dominant domain → null.
|
|
78
|
+
*
|
|
79
|
+
* If `filename` is provided it takes priority over domain inference
|
|
80
|
+
* (filename hints are explicit). Domain inference is the fallback.
|
|
81
|
+
*/
|
|
82
|
+
export function inferProvider(
|
|
83
|
+
filename: string | undefined,
|
|
84
|
+
cookies: CanonicalCookie[],
|
|
85
|
+
hint?: ProviderId,
|
|
86
|
+
): ProviderId | null {
|
|
87
|
+
if (hint) return hint;
|
|
88
|
+
if (filename) {
|
|
89
|
+
const fromName = inferProviderFromFilename(filename);
|
|
90
|
+
if (fromName) return fromName;
|
|
91
|
+
}
|
|
92
|
+
return inferProviderFromCookies(cookies);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Build a list of provider rules used by other parts of the package.
|
|
97
|
+
* Exposed for UI display (e.g. "which providers does this sanitizer know?").
|
|
98
|
+
*/
|
|
99
|
+
export function knownProviders(): ProviderId[] {
|
|
100
|
+
const set = new Set<ProviderId>();
|
|
101
|
+
for (const { provider } of FILENAME_HINTS) set.add(provider);
|
|
102
|
+
return Array.from(set);
|
|
103
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize step — dedupe, drop invalid/expired, set provider.
|
|
3
|
+
*
|
|
4
|
+
* Rules (applied in order):
|
|
5
|
+
* 1. Drop cookies with empty `name` or `value`.
|
|
6
|
+
* 2. Drop cookies whose domain doesn't match the inferred provider.
|
|
7
|
+
* 3. Drop expired cookies (expires in the past, not session).
|
|
8
|
+
* 4. Dedupe by `(domain, name)` — keep the LAST entry (most recent wins).
|
|
9
|
+
* 5. Mark each surviving cookie with the provider id.
|
|
10
|
+
*
|
|
11
|
+
* SEC-001: Cookie values are not touched here, but never logged.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { CanonicalCookie, ProviderId } from "./types.js";
|
|
15
|
+
|
|
16
|
+
/** Returns true if the cookie's domain matches the given provider's domain scope. */
|
|
17
|
+
export function domainMatchesProvider(
|
|
18
|
+
domain: string,
|
|
19
|
+
provider: ProviderId,
|
|
20
|
+
): boolean {
|
|
21
|
+
if (!domain) return false;
|
|
22
|
+
const d = domain.toLowerCase();
|
|
23
|
+
switch (provider) {
|
|
24
|
+
case "minimax":
|
|
25
|
+
return d === "minimax.io" || d.endsWith(".minimax.io");
|
|
26
|
+
case "anthropic":
|
|
27
|
+
return d === "anthropic.com" || d.endsWith(".anthropic.com");
|
|
28
|
+
case "openai":
|
|
29
|
+
return (
|
|
30
|
+
d === "openai.com" ||
|
|
31
|
+
d.endsWith(".openai.com") ||
|
|
32
|
+
d === "chatgpt.com" ||
|
|
33
|
+
d.endsWith(".chatgpt.com")
|
|
34
|
+
);
|
|
35
|
+
case "glm":
|
|
36
|
+
return (
|
|
37
|
+
d === "z.ai" ||
|
|
38
|
+
d.endsWith(".z.ai") ||
|
|
39
|
+
d === "zhipuai.cn" ||
|
|
40
|
+
d.endsWith(".zhipuai.cn")
|
|
41
|
+
);
|
|
42
|
+
case "openrouter":
|
|
43
|
+
return d === "openrouter.ai" || d.endsWith(".openrouter.ai");
|
|
44
|
+
default:
|
|
45
|
+
// Unknown provider — accept all and let downstream reject.
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Returns true if the cookie is expired (in the past). Session cookies never expire. */
|
|
51
|
+
export function isExpired(
|
|
52
|
+
cookie: CanonicalCookie,
|
|
53
|
+
nowSec: number = Math.floor(Date.now() / 1000),
|
|
54
|
+
): boolean {
|
|
55
|
+
if (cookie.expires == null) return false; // session cookie
|
|
56
|
+
if (cookie.expires <= 0) return false; // 0 = session in Netscape
|
|
57
|
+
return cookie.expires < nowSec;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Normalize the union of all parsed cookies.
|
|
62
|
+
*
|
|
63
|
+
* @param cookies Raw cookies from all parsed files
|
|
64
|
+
* @param provider Inferred (or hinted) provider; used for domain filtering and tagging
|
|
65
|
+
* @param nowSec Override for the current Unix seconds (test seam)
|
|
66
|
+
*/
|
|
67
|
+
export function normalize(
|
|
68
|
+
cookies: CanonicalCookie[],
|
|
69
|
+
provider: ProviderId,
|
|
70
|
+
nowSec: number = Math.floor(Date.now() / 1000),
|
|
71
|
+
): CanonicalCookie[] {
|
|
72
|
+
if (!Array.isArray(cookies) || cookies.length === 0) return [];
|
|
73
|
+
|
|
74
|
+
const dedup = new Map<string, CanonicalCookie>();
|
|
75
|
+
|
|
76
|
+
for (const c of cookies) {
|
|
77
|
+
// Rule 1: empty fields.
|
|
78
|
+
if (!c.name || !c.value) continue;
|
|
79
|
+
|
|
80
|
+
// Rule 2: domain must match provider (skip for unknown provider).
|
|
81
|
+
if (provider && !domainMatchesProvider(c.domain, provider)) continue;
|
|
82
|
+
|
|
83
|
+
// Rule 3: expired.
|
|
84
|
+
if (isExpired(c, nowSec)) continue;
|
|
85
|
+
|
|
86
|
+
// Rule 4 + 5: dedupe by (domain, name); last wins. Tag with provider.
|
|
87
|
+
const key = `${c.domain}::${c.name}`;
|
|
88
|
+
dedup.set(key, { ...c, provider });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return Array.from(dedup.values());
|
|
92
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EditThisCookie / Chrome devtools JSON cookie export parser.
|
|
3
|
+
*
|
|
4
|
+
* Browser extension "EditThisCookie" and Chrome devtools both export
|
|
5
|
+
* cookies in this canonical shape (Chrome/Edge/Chromium cookie store):
|
|
6
|
+
*
|
|
7
|
+
* ```json
|
|
8
|
+
* [
|
|
9
|
+
* {
|
|
10
|
+
* "domain": ".minimax.io",
|
|
11
|
+
* "expirationDate": 1760000000.123,
|
|
12
|
+
* "hostOnly": false,
|
|
13
|
+
* "httpOnly": true,
|
|
14
|
+
* "name": "_token",
|
|
15
|
+
* "path": "/",
|
|
16
|
+
* "sameSite": "no_restriction",
|
|
17
|
+
* "secure": true,
|
|
18
|
+
* "session": false,
|
|
19
|
+
* "storeId": "0",
|
|
20
|
+
* "value": "...",
|
|
21
|
+
* "id": 1
|
|
22
|
+
* }
|
|
23
|
+
* ]
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* Some tools may export a single object (not an array). Handle both.
|
|
27
|
+
*
|
|
28
|
+
* SEC-001: Cookie VALUES are returned as strings in-memory. Never logged.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import type { CanonicalCookie } from "./types.js";
|
|
32
|
+
|
|
33
|
+
interface EditThisCookieRecord {
|
|
34
|
+
domain?: unknown;
|
|
35
|
+
expirationDate?: unknown;
|
|
36
|
+
hostOnly?: unknown;
|
|
37
|
+
httpOnly?: unknown;
|
|
38
|
+
name?: unknown;
|
|
39
|
+
path?: unknown;
|
|
40
|
+
secure?: unknown;
|
|
41
|
+
session?: unknown;
|
|
42
|
+
value?: unknown;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parse EditThisCookie / Chrome devtools JSON export.
|
|
47
|
+
*
|
|
48
|
+
* Accepts:
|
|
49
|
+
* - top-level array of records
|
|
50
|
+
* - top-level single object
|
|
51
|
+
*
|
|
52
|
+
* Skips:
|
|
53
|
+
* - non-object entries
|
|
54
|
+
* - records without non-empty name+value
|
|
55
|
+
*
|
|
56
|
+
* Never throws; returns empty array on parse failure.
|
|
57
|
+
*/
|
|
58
|
+
export function parseEditThisCookieJson(text: string): CanonicalCookie[] {
|
|
59
|
+
if (typeof text !== "string" || text.length === 0) return [];
|
|
60
|
+
|
|
61
|
+
let data: unknown;
|
|
62
|
+
try {
|
|
63
|
+
data = JSON.parse(text);
|
|
64
|
+
} catch {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const records: EditThisCookieRecord[] = [];
|
|
69
|
+
if (Array.isArray(data)) {
|
|
70
|
+
for (const item of data) {
|
|
71
|
+
if (item && typeof item === "object" && !Array.isArray(item)) {
|
|
72
|
+
records.push(item as EditThisCookieRecord);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else if (data && typeof data === "object" && !Array.isArray(data)) {
|
|
76
|
+
records.push(data as EditThisCookieRecord);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const cookies: CanonicalCookie[] = [];
|
|
80
|
+
for (const r of records) {
|
|
81
|
+
const name = asString(r.name);
|
|
82
|
+
const value = asString(r.value);
|
|
83
|
+
if (!name || !value) continue;
|
|
84
|
+
|
|
85
|
+
const expires = asExpires(r.expirationDate, r.session);
|
|
86
|
+
|
|
87
|
+
cookies.push({
|
|
88
|
+
name,
|
|
89
|
+
value,
|
|
90
|
+
domain: asString(r.domain) ?? "",
|
|
91
|
+
path: asString(r.path) ?? "/",
|
|
92
|
+
expires,
|
|
93
|
+
secure: asBool(r.secure) ?? false,
|
|
94
|
+
httpOnly: asBool(r.httpOnly) ?? false,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return cookies;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function asString(v: unknown): string | undefined {
|
|
102
|
+
if (typeof v === "string" && v.length > 0) return v;
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function asBool(v: unknown): boolean | undefined {
|
|
107
|
+
if (typeof v === "boolean") return v;
|
|
108
|
+
if (v === 1 || v === "1" || v === "true" || v === "TRUE") return true;
|
|
109
|
+
if (v === 0 || v === "0" || v === "false" || v === "FALSE") return false;
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function asExpires(v: unknown, session: unknown): number | undefined {
|
|
114
|
+
if (session === true || session === "true") return undefined;
|
|
115
|
+
if (typeof v === "number" && Number.isFinite(v)) {
|
|
116
|
+
// EditThisCookie uses unix-seconds (with millisecond decimals sometimes).
|
|
117
|
+
// If v > 1e12, it's milliseconds — convert.
|
|
118
|
+
const seconds = v > 1e12 ? Math.floor(v / 1000) : Math.floor(v);
|
|
119
|
+
return seconds > 0 ? seconds : undefined;
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Netscape-format cookie file parser.
|
|
3
|
+
*
|
|
4
|
+
* Format reference: https://curl.haxx.se/rfc/cookie_spec.html
|
|
5
|
+
*
|
|
6
|
+
* Each row is tab-separated, 7 columns:
|
|
7
|
+
* domain flag path secure expires name value
|
|
8
|
+
*
|
|
9
|
+
* - Lines starting with `# ` are comments (skipped).
|
|
10
|
+
* - Lines starting with `#HttpOnly_` are HTTP-only cookies
|
|
11
|
+
* (the leading `#HttpOnly_` is stripped, the rest is parsed normally).
|
|
12
|
+
* - Empty lines are skipped.
|
|
13
|
+
* - `expires` is Unix seconds; `0` or unparseable → session cookie.
|
|
14
|
+
* - Lines with fewer than 7 columns are skipped (malformed).
|
|
15
|
+
*
|
|
16
|
+
* SEC-001: Cookie values are returned as strings in-memory. They MUST
|
|
17
|
+
* never be logged. The scrubber in cookie-sanitizer applies value
|
|
18
|
+
* redaction before any path-touching log line.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { CanonicalCookie } from "./types.js";
|
|
22
|
+
|
|
23
|
+
/** Raw row as parsed from a Netscape file. */
|
|
24
|
+
interface NetscapeRow {
|
|
25
|
+
domain: string;
|
|
26
|
+
path: string;
|
|
27
|
+
secure: boolean;
|
|
28
|
+
httpOnly: boolean;
|
|
29
|
+
expires: number; // 0 = session
|
|
30
|
+
name: string;
|
|
31
|
+
value: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Parse a Netscape-format cookie string into canonical records.
|
|
36
|
+
*
|
|
37
|
+
* Skips:
|
|
38
|
+
* - blank lines
|
|
39
|
+
* - comments (lines starting with `#` but not `#HttpOnly_`)
|
|
40
|
+
* - malformed rows (< 7 columns)
|
|
41
|
+
*
|
|
42
|
+
* Never throws; returns empty array if input is invalid.
|
|
43
|
+
*/
|
|
44
|
+
export function parseNetscape(text: string): CanonicalCookie[] {
|
|
45
|
+
if (typeof text !== "string" || text.length === 0) return [];
|
|
46
|
+
|
|
47
|
+
const rows: NetscapeRow[] = [];
|
|
48
|
+
const lines = text.split(/\r?\n/);
|
|
49
|
+
|
|
50
|
+
for (const raw of lines) {
|
|
51
|
+
if (!raw) continue;
|
|
52
|
+
|
|
53
|
+
let line = raw;
|
|
54
|
+
let httpOnly = false;
|
|
55
|
+
if (line.startsWith("#HttpOnly_")) {
|
|
56
|
+
httpOnly = true;
|
|
57
|
+
line = line.slice("#HttpOnly_".length);
|
|
58
|
+
} else if (line.startsWith("#")) {
|
|
59
|
+
// Plain comment line (Netscape / curl spec).
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const cols = line.split("\t");
|
|
64
|
+
if (cols.length < 7) continue;
|
|
65
|
+
|
|
66
|
+
const [domain, _flag, cookiePath, secure, expires, name, value] = cols;
|
|
67
|
+
if (!name || !value) continue;
|
|
68
|
+
|
|
69
|
+
rows.push({
|
|
70
|
+
domain: (domain ?? "").trim(),
|
|
71
|
+
path: (cookiePath ?? "/").trim() || "/",
|
|
72
|
+
secure: (secure ?? "FALSE").toUpperCase() === "TRUE",
|
|
73
|
+
httpOnly,
|
|
74
|
+
expires: parseInt(expires ?? "0", 10) || 0,
|
|
75
|
+
name: name.trim(),
|
|
76
|
+
value, // intentional: keep as-is for in-memory pipeline; never log
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Map to CanonicalCookie shape.
|
|
81
|
+
return rows.map((r) => ({
|
|
82
|
+
name: r.name,
|
|
83
|
+
value: r.value,
|
|
84
|
+
domain: r.domain,
|
|
85
|
+
path: r.path,
|
|
86
|
+
expires: r.expires === 0 ? undefined : r.expires,
|
|
87
|
+
secure: r.secure,
|
|
88
|
+
httpOnly: r.httpOnly,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Serialize a list of canonical cookies back to Netscape format.
|
|
94
|
+
* Used by atomic-write.ts to produce the canonical cache.
|
|
95
|
+
*
|
|
96
|
+
* Output uses the canonical column order:
|
|
97
|
+
* domain flag path secure expires name value
|
|
98
|
+
*
|
|
99
|
+
* `flag` is FALSE per curl spec (HttpOnly is encoded as `#HttpOnly_`).
|
|
100
|
+
* Cookie values are NEVER logged during the round-trip.
|
|
101
|
+
*/
|
|
102
|
+
export function serializeNetscape(cookies: CanonicalCookie[]): string {
|
|
103
|
+
const out: string[] = ["# Netscape HTTP Cookie File", ""];
|
|
104
|
+
|
|
105
|
+
for (const c of cookies) {
|
|
106
|
+
if (!c.name || !c.value) continue;
|
|
107
|
+
const domain = c.domain ?? "";
|
|
108
|
+
const path = c.path ?? "/";
|
|
109
|
+
const secure = c.secure ? "TRUE" : "FALSE";
|
|
110
|
+
const expires = c.expires != null ? String(c.expires) : "0";
|
|
111
|
+
const line = [domain, "FALSE", path, secure, expires, c.name, c.value].join(
|
|
112
|
+
"\t",
|
|
113
|
+
);
|
|
114
|
+
out.push(c.httpOnly ? `#HttpOnly_${line}` : line);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return out.join("\n") + "\n";
|
|
118
|
+
}
|