camoucore 0.2.0 → 0.6.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.
Files changed (175) hide show
  1. package/LICENSE +4 -3
  2. package/dist/camoufox/__main__.d.ts +10 -0
  3. package/dist/camoufox/__main__.js +1175 -0
  4. package/dist/camoufox/__version__.d.ts +31 -0
  5. package/dist/camoufox/__version__.js +32 -0
  6. package/dist/camoufox/_humanize_playwright.d.ts +54 -0
  7. package/dist/camoufox/_humanize_playwright.js +429 -0
  8. package/dist/camoufox/addons.d.ts +21 -0
  9. package/dist/camoufox/addons.js +56 -0
  10. package/dist/camoufox/async_api.d.ts +9 -0
  11. package/dist/camoufox/async_api.js +9 -0
  12. package/dist/camoufox/browser-pin.d.ts +46 -0
  13. package/dist/camoufox/browser-pin.js +96 -0
  14. package/dist/camoufox/cli.d.ts +7 -0
  15. package/dist/camoufox/cli.js +20 -0
  16. package/dist/camoufox/coherence.d.ts +141 -0
  17. package/dist/camoufox/coherence.js +708 -0
  18. package/dist/camoufox/data-files/audio-output-latency.json +15 -0
  19. package/dist/camoufox/data-files/browser-pin.json +7 -0
  20. package/dist/camoufox/data-files/display-profiles.json +6 -0
  21. package/dist/camoufox/data-files/essential-fonts.json +5 -0
  22. package/dist/camoufox/data-files/fingerprint-presets-v150.json +14544 -0
  23. package/dist/camoufox/data-files/fingerprint-presets.json +5192 -0
  24. package/dist/camoufox/data-files/font-bases.json +1888 -0
  25. package/dist/camoufox/data-files/font-groups.json +716 -0
  26. package/dist/camoufox/data-files/font-prefs.json +1046 -0
  27. package/dist/camoufox/data-files/fonts.json +240 -0
  28. package/dist/camoufox/data-files/fpgen.yml +44 -0
  29. package/dist/camoufox/data-files/launcher-constants.json +136 -0
  30. package/dist/camoufox/data-files/media-devices.json +180 -0
  31. package/dist/camoufox/data-files/repos.yml +40 -0
  32. package/dist/camoufox/data-files/voice-manifests.json +1864 -0
  33. package/dist/camoufox/data-files/voice-uris.json +188 -0
  34. package/dist/camoufox/data-files/warnings.yml +146 -0
  35. package/dist/camoufox/display_profile.d.ts +21 -0
  36. package/dist/camoufox/display_profile.js +129 -0
  37. package/dist/camoufox/driver_defaults.d.ts +27 -0
  38. package/dist/camoufox/driver_defaults.js +119 -0
  39. package/dist/camoufox/exceptions.d.ts +68 -0
  40. package/dist/camoufox/exceptions.js +122 -0
  41. package/dist/camoufox/fingerprints.d.ts +434 -0
  42. package/dist/camoufox/fingerprints.js +1936 -0
  43. package/dist/camoufox/gpudisplay.d.ts +158 -0
  44. package/dist/camoufox/gpudisplay.js +695 -0
  45. package/dist/camoufox/host_rendering.d.ts +38 -0
  46. package/dist/camoufox/host_rendering.js +180 -0
  47. package/dist/camoufox/humanize.d.ts +89 -0
  48. package/dist/camoufox/humanize.js +143 -0
  49. package/dist/camoufox/index.d.ts +21 -0
  50. package/dist/camoufox/index.js +21 -0
  51. package/dist/camoufox/ip.d.ts +45 -0
  52. package/dist/camoufox/ip.js +114 -0
  53. package/dist/camoufox/multiversion.d.ts +131 -0
  54. package/dist/camoufox/multiversion.js +490 -0
  55. package/dist/camoufox/node-floor.d.ts +5 -0
  56. package/dist/camoufox/node-floor.js +17 -0
  57. package/dist/camoufox/paths.d.ts +45 -0
  58. package/dist/camoufox/paths.js +62 -0
  59. package/dist/camoufox/pkgman.d.ts +361 -0
  60. package/dist/camoufox/pkgman.js +1066 -0
  61. package/dist/camoufox/product.d.ts +8 -0
  62. package/dist/camoufox/product.js +8 -0
  63. package/dist/camoufox/pycompat.d.ts +97 -0
  64. package/dist/camoufox/pycompat.js +458 -0
  65. package/dist/camoufox/server.d.ts +36 -0
  66. package/dist/camoufox/server.js +82 -0
  67. package/dist/camoufox/sync_api.d.ts +77 -0
  68. package/dist/camoufox/sync_api.js +207 -0
  69. package/dist/camoufox/utils.d.ts +638 -0
  70. package/dist/camoufox/utils.js +2507 -0
  71. package/dist/camoufox/warnings.d.ts +16 -0
  72. package/dist/camoufox/warnings.js +65 -0
  73. package/dist/camoufox/webgl.d.ts +52 -0
  74. package/dist/camoufox/webgl.js +264 -0
  75. package/dist/camouwolf/__main__.d.ts +10 -0
  76. package/dist/camouwolf/__main__.js +1180 -0
  77. package/dist/camouwolf/__version__.d.ts +31 -0
  78. package/dist/camouwolf/__version__.js +32 -0
  79. package/dist/camouwolf/_humanize_playwright.d.ts +53 -0
  80. package/dist/camouwolf/_humanize_playwright.js +427 -0
  81. package/dist/camouwolf/addons.d.ts +42 -0
  82. package/dist/camouwolf/addons.js +113 -0
  83. package/dist/camouwolf/async_api.d.ts +9 -0
  84. package/dist/camouwolf/async_api.js +9 -0
  85. package/dist/camouwolf/browser-pin.d.ts +46 -0
  86. package/dist/camouwolf/browser-pin.js +96 -0
  87. package/dist/camouwolf/cli.d.ts +7 -0
  88. package/dist/camouwolf/cli.js +20 -0
  89. package/dist/camouwolf/clienthints.d.ts +61 -0
  90. package/dist/camouwolf/clienthints.js +176 -0
  91. package/dist/camouwolf/coherence.d.ts +152 -0
  92. package/dist/camouwolf/coherence.js +554 -0
  93. package/dist/camouwolf/data-files/browser-pin.json +7 -0
  94. package/dist/camouwolf/data-files/camouwolf.defaults.json +118 -0
  95. package/dist/camouwolf/data-files/data/bundle-fonts.json +1983 -0
  96. package/dist/camouwolf/data-files/data/chrome-languages.json +517 -0
  97. package/dist/camouwolf/data-files/data/properties.json +321 -0
  98. package/dist/camouwolf/data-files/data/system-colors.json +35 -0
  99. package/dist/camouwolf/data-files/data/ubo.json +6 -0
  100. package/dist/camouwolf/data-files/fpgen.yml +41 -0
  101. package/dist/camouwolf/data-files/launcher-constants.json +26 -0
  102. package/dist/camouwolf/data-files/repos.yml +23 -0
  103. package/dist/camouwolf/data-files/warnings.yml +96 -0
  104. package/dist/camouwolf/driver_defaults.d.ts +25 -0
  105. package/dist/camouwolf/driver_defaults.js +114 -0
  106. package/dist/camouwolf/exceptions.d.ts +95 -0
  107. package/dist/camouwolf/exceptions.js +150 -0
  108. package/dist/camouwolf/fingerprints.d.ts +227 -0
  109. package/dist/camouwolf/fingerprints.js +753 -0
  110. package/dist/camouwolf/gpudisplay.d.ts +171 -0
  111. package/dist/camouwolf/gpudisplay.js +711 -0
  112. package/dist/camouwolf/host_rendering.d.ts +18 -0
  113. package/dist/camouwolf/host_rendering.js +114 -0
  114. package/dist/camouwolf/humanize.d.ts +84 -0
  115. package/dist/camouwolf/humanize.js +128 -0
  116. package/dist/camouwolf/index.d.ts +22 -0
  117. package/dist/camouwolf/index.js +21 -0
  118. package/dist/camouwolf/multiversion.d.ts +131 -0
  119. package/dist/camouwolf/multiversion.js +491 -0
  120. package/dist/camouwolf/node-floor.d.ts +5 -0
  121. package/dist/camouwolf/node-floor.js +17 -0
  122. package/dist/camouwolf/paths.d.ts +34 -0
  123. package/dist/camouwolf/paths.js +58 -0
  124. package/dist/camouwolf/pkgman.d.ts +331 -0
  125. package/dist/camouwolf/pkgman.js +972 -0
  126. package/dist/camouwolf/product.d.ts +3 -0
  127. package/dist/camouwolf/product.js +3 -0
  128. package/dist/camouwolf/pycompat.d.ts +92 -0
  129. package/dist/camouwolf/pycompat.js +461 -0
  130. package/dist/camouwolf/server.d.ts +36 -0
  131. package/dist/camouwolf/server.js +81 -0
  132. package/dist/camouwolf/switches.d.ts +77 -0
  133. package/dist/camouwolf/switches.js +222 -0
  134. package/dist/camouwolf/sync_api.d.ts +84 -0
  135. package/dist/camouwolf/sync_api.js +173 -0
  136. package/dist/camouwolf/utils.d.ts +644 -0
  137. package/dist/camouwolf/utils.js +2057 -0
  138. package/dist/camouwolf/warnings.d.ts +14 -0
  139. package/dist/camouwolf/warnings.js +59 -0
  140. package/dist/camouwolf/webgl.d.ts +41 -0
  141. package/dist/camouwolf/webgl.js +198 -0
  142. package/dist/data-files/antibot-rules.json +146 -0
  143. package/dist/data.d.ts +1 -1
  144. package/dist/data.js +4 -3
  145. package/dist/download.js +1 -1
  146. package/dist/exceptions.d.ts +1 -1
  147. package/dist/exceptions.js +1 -1
  148. package/dist/geolocation.js +1 -1
  149. package/dist/host.d.ts +2 -0
  150. package/dist/host.js +17 -1
  151. package/dist/ip.js +1 -1
  152. package/dist/locales.js +1 -1
  153. package/dist/mcp/cli.d.ts +15 -0
  154. package/dist/mcp/cli.js +108 -0
  155. package/dist/mcp/index.d.ts +8 -0
  156. package/dist/mcp/index.js +8 -0
  157. package/dist/mcp/main.d.ts +2 -0
  158. package/dist/mcp/main.js +8 -0
  159. package/dist/mcp/secrets.d.ts +50 -0
  160. package/dist/mcp/secrets.js +193 -0
  161. package/dist/mcp/server.d.ts +48 -0
  162. package/dist/mcp/server.js +653 -0
  163. package/dist/mcp/tree.d.ts +10 -0
  164. package/dist/mcp/tree.js +36 -0
  165. package/dist/passthrough.d.ts +16 -0
  166. package/dist/passthrough.js +50 -0
  167. package/dist/product.js +1 -1
  168. package/dist/settings.d.ts +12 -0
  169. package/dist/settings.js +95 -0
  170. package/dist/telemetry.d.ts +127 -0
  171. package/dist/telemetry.js +426 -0
  172. package/dist/virtdisplay.d.ts +1 -1
  173. package/dist/virtdisplay.js +1 -1
  174. package/dist/warnings.js +1 -1
  175. package/package.json +57 -13
@@ -0,0 +1,108 @@
1
+ /**
2
+ * The MCP server as a command: serve(argv, options) runs the stdio server
3
+ * for one launcher. Each launcher's bin runs it as `<launcher> mcp`
4
+ * (src/<launcher>/cli.ts); main.ts runs it for any launcher module named on
5
+ * the command line.
6
+ */
7
+ import fs from "node:fs";
8
+ import path from "node:path";
9
+ import { Writable } from "node:stream";
10
+ import { parseArgs } from "node:util";
11
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
12
+ import { Secrets } from "./secrets.js";
13
+ import { createServer, loadLauncher } from "./server.js";
14
+ /**
15
+ * Serve a launcher's browser over MCP on stdio until the client goes away.
16
+ * Bad arguments print the usage and exit 2. Call it before the launcher is
17
+ * imported anywhere: launchers print as they load, and stdout is the protocol.
18
+ */
19
+ export async function serve(argv, options = {}) {
20
+ // stdout is the protocol, and launchers print to it, so everything but the
21
+ // transport goes to stderr. This comes before the launcher is imported, since
22
+ // importing it can print too.
23
+ const protocolWrite = process.stdout.write.bind(process.stdout);
24
+ process.stdout.write = process.stderr.write.bind(process.stderr);
25
+ const protocolStream = new Writable({
26
+ write: (chunk, encoding, done) => void protocolWrite(chunk, encoding, done),
27
+ });
28
+ const program = options.program ?? "camoucore-mcp";
29
+ const flags = "[--secrets <file>] [--allowed-domains <pattern,...>] [--profile <dir>] [--uploads <dir>]";
30
+ const USAGE = (options.launcher
31
+ ? `usage: ${program} ${flags}\n`
32
+ : `usage: ${program} <launcher> ${flags}\n` +
33
+ " <launcher> the launcher module to drive: camoucore/camoufox or camoucore/camouwolf\n") +
34
+ " --profile your own browser profile, which session_open { profile: true } runs a copy of\n" +
35
+ " --uploads the one directory upload_file may attach files from";
36
+ let parsed;
37
+ function parse() {
38
+ return parseArgs({
39
+ args: argv,
40
+ allowPositionals: true,
41
+ options: {
42
+ secrets: { type: "string" },
43
+ "allowed-domains": { type: "string" },
44
+ profile: { type: "string" },
45
+ uploads: { type: "string" },
46
+ },
47
+ });
48
+ }
49
+ try {
50
+ parsed = parse();
51
+ }
52
+ catch (error) {
53
+ console.error(`${error.message}\n${USAGE}`);
54
+ process.exit(2);
55
+ }
56
+ const positionals = options.launcher
57
+ ? [options.launcher, ...parsed.positionals]
58
+ : parsed.positionals;
59
+ const [specifier] = positionals;
60
+ if (!specifier || positionals.length > 1) {
61
+ console.error(USAGE);
62
+ process.exit(2);
63
+ }
64
+ // Read before anything launches, so a bad file stops the server at start.
65
+ const secrets = parsed.values.secrets
66
+ ? Secrets.load(parsed.values.secrets)
67
+ : undefined;
68
+ const profile = parsed.values.profile
69
+ ? path.resolve(parsed.values.profile)
70
+ : undefined;
71
+ if (profile &&
72
+ !fs.statSync(profile, { throwIfNoEntry: false })?.isDirectory()) {
73
+ console.error(`--profile ${profile} is not a directory.`);
74
+ process.exit(2);
75
+ }
76
+ const uploads = parsed.values.uploads
77
+ ? path.resolve(parsed.values.uploads)
78
+ : undefined;
79
+ if (uploads &&
80
+ !fs.statSync(uploads, { throwIfNoEntry: false })?.isDirectory()) {
81
+ console.error(`--uploads ${uploads} is not a directory.`);
82
+ process.exit(2);
83
+ }
84
+ const allowedDomains = parsed.values["allowed-domains"]
85
+ ?.split(",")
86
+ .map((pattern) => pattern.trim())
87
+ .filter(Boolean);
88
+ const { launcher, browserType } = await loadLauncher(specifier);
89
+ const { server, closeAll } = createServer(launcher, browserType, {
90
+ secrets,
91
+ allowedDomains,
92
+ profile,
93
+ uploads,
94
+ });
95
+ /** A client going away must not leave its browsers running. */
96
+ let shuttingDown = false;
97
+ async function shutdown() {
98
+ if (shuttingDown)
99
+ return;
100
+ shuttingDown = true;
101
+ await closeAll();
102
+ process.exit(0);
103
+ }
104
+ process.on("SIGINT", shutdown);
105
+ process.on("SIGTERM", shutdown);
106
+ process.stdin.on("close", shutdown);
107
+ await server.connect(new StdioServerTransport(process.stdin, protocolStream));
108
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * camoucore/mcp: an MCP server that drives a launcher's browser. serve() is
3
+ * the stdio server as each launcher's `mcp` command runs it; createServer()
4
+ * and loadLauncher() build one for a transport of your own.
5
+ */
6
+ export { type ServeOptions, serve } from "./cli.js";
7
+ export { Secrets } from "./secrets.js";
8
+ export { createServer, type Launcher, loadLauncher, type ServerOptions, } from "./server.js";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * camoucore/mcp: an MCP server that drives a launcher's browser. serve() is
3
+ * the stdio server as each launcher's `mcp` command runs it; createServer()
4
+ * and loadLauncher() build one for a transport of your own.
5
+ */
6
+ export { serve } from "./cli.js";
7
+ export { Secrets } from "./secrets.js";
8
+ export { createServer, loadLauncher, } from "./server.js";
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `node dist/mcp/main.js <launcher> [options]`: the MCP server for any
4
+ * launcher module, named on the command line. Users run it through their
5
+ * launcher instead (`npx camoufox mcp`).
6
+ */
7
+ import { serve } from "./cli.js";
8
+ await serve(process.argv.slice(2));
@@ -0,0 +1,50 @@
1
+ /**
2
+ * A URL pattern: `[scheme://]host[:port][/path]`. The scheme is `https`,
3
+ * `http` or `http*` (both), and `https` when left out. `*.example.com` is
4
+ * example.com and its subdomains. A port must match when given. A path is a
5
+ * glob on the URL's path, where `*` is any run of characters.
6
+ */
7
+ export declare class UrlPattern {
8
+ readonly source: string;
9
+ private readonly schemes;
10
+ private readonly host;
11
+ private readonly subdomains;
12
+ private readonly port;
13
+ private readonly path;
14
+ constructor(source: string);
15
+ matches(url: string): boolean;
16
+ }
17
+ /** RFC 6238 with the defaults every authenticator app uses: SHA-1, 30 s, 6 digits. */
18
+ export declare function totp(seed: string, at?: number): string;
19
+ type Value = string | {
20
+ totp: string;
21
+ };
22
+ export declare class Secrets {
23
+ private readonly entries;
24
+ /** Each literal value with its name, longest first, for redaction. */
25
+ private readonly literals;
26
+ /**
27
+ * `byPattern` maps a URL pattern to that domain's secrets: a name to its
28
+ * value, or to `{ totp: "<base32 seed>" }` for a one-time code.
29
+ */
30
+ constructor(byPattern: Record<string, Record<string, Value>>);
31
+ /** Reads a JSON secrets file that only its owner can read. */
32
+ static load(file: string): Secrets;
33
+ get empty(): boolean;
34
+ /** Every pattern and the names under it; neither is secret. */
35
+ names(): Record<string, string[]>;
36
+ /** The names usable on a page at `url`. */
37
+ namesFor(url: string): string[];
38
+ /** The text to type for `name` in a frame at `url`; refuses anywhere else. */
39
+ valueFor(name: string, url: string): string;
40
+ /** Whether `text` holds any secret value, in any case. */
41
+ contains(text: string): boolean;
42
+ /** `text` with every secret value, in every form, as `<secret:name>`. */
43
+ redact(text: string): string;
44
+ }
45
+ /**
46
+ * `text` with `value`, and the escaped and encoded forms it takes in markup,
47
+ * JSON, URLs and headers, in any case, replaced by `replacement`.
48
+ */
49
+ export declare function redactValue(text: string, value: string, replacement: string): string;
50
+ export {};
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Secrets the agent can type but never read, after browser-use's
3
+ * `sensitive_data`: the model names a secret, the server types its value into
4
+ * a field whose frame is on the secret's domain, and every value is replaced
5
+ * by `<secret:name>` in whatever goes back to the model.
6
+ */
7
+ import { createHmac } from "node:crypto";
8
+ import fs from "node:fs";
9
+ /**
10
+ * A URL pattern: `[scheme://]host[:port][/path]`. The scheme is `https`,
11
+ * `http` or `http*` (both), and `https` when left out. `*.example.com` is
12
+ * example.com and its subdomains. A port must match when given. A path is a
13
+ * glob on the URL's path, where `*` is any run of characters.
14
+ */
15
+ export class UrlPattern {
16
+ source;
17
+ schemes;
18
+ host;
19
+ subdomains;
20
+ port;
21
+ path;
22
+ constructor(source) {
23
+ this.source = source;
24
+ const parts = /^(?:(https|http|http\*):\/\/)?(\*\.)?([^/:*?#]+)(?::(\d+))?(\/[^?#]*)?$/i.exec(source);
25
+ if (!parts) {
26
+ throw new Error(`${source} is not a URL pattern: [https|http|http*://][*.]host[:port][/path]`);
27
+ }
28
+ const [, scheme = "https", wildcard, host, port, path] = parts;
29
+ this.schemes =
30
+ scheme === "http*" ? ["http:", "https:"] : [`${scheme.toLowerCase()}:`];
31
+ // The URL parser's hostname: lowercase, IDN as punycode, as a URL's is.
32
+ this.host = new URL(`https://${host}`).hostname;
33
+ this.subdomains = Boolean(wildcard);
34
+ this.port = port;
35
+ this.path =
36
+ path === undefined
37
+ ? undefined
38
+ : new RegExp(`^${path
39
+ .split("*")
40
+ .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
41
+ .join(".*")}$`);
42
+ }
43
+ matches(url) {
44
+ let parsed;
45
+ try {
46
+ parsed = new URL(url);
47
+ }
48
+ catch {
49
+ return false;
50
+ }
51
+ const defaultPort = parsed.protocol === "https:" ? "443" : "80";
52
+ return (this.schemes.includes(parsed.protocol) &&
53
+ (parsed.hostname === this.host ||
54
+ (this.subdomains && parsed.hostname.endsWith(`.${this.host}`))) &&
55
+ (this.port === undefined || (parsed.port || defaultPort) === this.port) &&
56
+ (this.path === undefined || this.path.test(parsed.pathname)));
57
+ }
58
+ }
59
+ function base32Decode(text) {
60
+ const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
61
+ const clean = text.replace(/[\s=-]/g, "").toUpperCase();
62
+ let bits = "";
63
+ for (const char of clean) {
64
+ const value = alphabet.indexOf(char);
65
+ if (value < 0)
66
+ throw new Error("a TOTP seed is base32");
67
+ bits += value.toString(2).padStart(5, "0");
68
+ }
69
+ const bytes = bits.match(/.{8}/g) ?? [];
70
+ return Buffer.from(bytes.map((byte) => Number.parseInt(byte, 2)));
71
+ }
72
+ /** RFC 6238 with the defaults every authenticator app uses: SHA-1, 30 s, 6 digits. */
73
+ export function totp(seed, at = Date.now()) {
74
+ const counter = Buffer.alloc(8);
75
+ counter.writeBigUInt64BE(BigInt(Math.floor(at / 30_000)));
76
+ const hmac = createHmac("sha1", base32Decode(seed)).update(counter).digest();
77
+ const offset = hmac.at(-1) & 0xf;
78
+ const code = (hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000;
79
+ return code.toString().padStart(6, "0");
80
+ }
81
+ /** Below this a value would be redacted out of ordinary text too often. */
82
+ const MIN_LENGTH = 4;
83
+ export class Secrets {
84
+ entries = [];
85
+ /** Each literal value with its name, longest first, for redaction. */
86
+ literals;
87
+ /**
88
+ * `byPattern` maps a URL pattern to that domain's secrets: a name to its
89
+ * value, or to `{ totp: "<base32 seed>" }` for a one-time code.
90
+ */
91
+ constructor(byPattern) {
92
+ for (const [source, secrets] of Object.entries(byPattern)) {
93
+ if (typeof secrets !== "object" || secrets === null) {
94
+ throw new Error(`Secrets are scoped to a URL pattern: give ${source} an object of name to value.`);
95
+ }
96
+ const pattern = new UrlPattern(source);
97
+ for (const [name, value] of Object.entries(secrets)) {
98
+ const literal = typeof value === "string" ? value : value?.totp;
99
+ if (typeof literal !== "string") {
100
+ throw new Error(`Secret ${name} for ${source} is a string or { "totp": "<base32 seed>" }.`);
101
+ }
102
+ if (literal.length < MIN_LENGTH) {
103
+ throw new Error(`Secret ${name} for ${source} is shorter than ${MIN_LENGTH} characters, too short to keep out of page text.`);
104
+ }
105
+ if (typeof value !== "string")
106
+ totp(value.totp);
107
+ this.entries.push({ pattern, name, value });
108
+ }
109
+ }
110
+ this.literals = this.entries
111
+ .map((entry) => [
112
+ typeof entry.value === "string" ? entry.value : entry.value.totp,
113
+ entry.name,
114
+ ])
115
+ .sort(([a], [b]) => b.length - a.length);
116
+ }
117
+ /** Reads a JSON secrets file that only its owner can read. */
118
+ static load(file) {
119
+ if (process.platform !== "win32" && fs.statSync(file).mode & 0o077) {
120
+ throw new Error(`${file} can be read by other users. \`chmod 600 ${file}\` makes it the owner's alone.`);
121
+ }
122
+ return new Secrets(JSON.parse(fs.readFileSync(file, "utf8")));
123
+ }
124
+ get empty() {
125
+ return this.entries.length === 0;
126
+ }
127
+ /** Every pattern and the names under it; neither is secret. */
128
+ names() {
129
+ const names = {};
130
+ for (const { pattern, name } of this.entries) {
131
+ names[pattern.source] ??= [];
132
+ names[pattern.source].push(name);
133
+ }
134
+ return names;
135
+ }
136
+ /** The names usable on a page at `url`. */
137
+ namesFor(url) {
138
+ return [
139
+ ...new Set(this.entries.filter((e) => e.pattern.matches(url)).map((e) => e.name)),
140
+ ];
141
+ }
142
+ /** The text to type for `name` in a frame at `url`; refuses anywhere else. */
143
+ valueFor(name, url) {
144
+ const named = this.entries.filter((entry) => entry.name === name);
145
+ if (!named.length)
146
+ throw new Error(`There is no secret named ${name}.`);
147
+ const here = named.filter((entry) => entry.pattern.matches(url));
148
+ if (!here.length) {
149
+ throw new Error(`Secret ${name} is for ${named.map((e) => e.pattern.source).join(", ")}, and this field's frame is at ${url}.`);
150
+ }
151
+ const values = new Set(here.map((entry) => JSON.stringify(entry.value)));
152
+ if (values.size > 1) {
153
+ throw new Error(`Secret ${name} has different values for patterns that all match ${url}: ${here.map((e) => e.pattern.source).join(", ")}.`);
154
+ }
155
+ const { value } = here[0];
156
+ return typeof value === "string" ? value : totp(value.totp);
157
+ }
158
+ /** Whether `text` holds any secret value, in any case. */
159
+ contains(text) {
160
+ const lower = text.toLowerCase();
161
+ return this.literals.some(([value]) => lower.includes(value.toLowerCase()));
162
+ }
163
+ /** `text` with every secret value, in every form, as `<secret:name>`. */
164
+ redact(text) {
165
+ return this.literals.reduce((redacted, [value, name]) => redactValue(redacted, value, `<secret:${name}>`), text);
166
+ }
167
+ }
168
+ /**
169
+ * `text` with `value`, and the escaped and encoded forms it takes in markup,
170
+ * JSON, URLs and headers, in any case, replaced by `replacement`.
171
+ */
172
+ export function redactValue(text, value, replacement) {
173
+ const forms = new Set([
174
+ value,
175
+ JSON.stringify(value).slice(1, -1),
176
+ encodeURIComponent(value),
177
+ value
178
+ .replace(/&/g, "&amp;")
179
+ .replace(/</g, "&lt;")
180
+ .replace(/>/g, "&gt;")
181
+ .replace(/"/g, "&quot;")
182
+ .replace(/'/g, "&#39;"),
183
+ Buffer.from(value).toString("base64"),
184
+ Buffer.from(value).toString("base64url"),
185
+ Buffer.from(value).toString("hex"),
186
+ ]);
187
+ let redacted = text;
188
+ for (const form of forms) {
189
+ const escaped = form.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
190
+ redacted = redacted.replace(new RegExp(escaped, "gi"), replacement);
191
+ }
192
+ return redacted;
193
+ }
@@ -0,0 +1,48 @@
1
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { Browser, BrowserContext, BrowserType } from "playwright-core";
3
+ import { Secrets } from "./secrets.js";
4
+ /** What a launcher package exports for this server to drive it. */
5
+ export interface Launcher {
6
+ PRODUCT: {
7
+ readonly name: string;
8
+ readonly title: string;
9
+ readonly engine: {
10
+ readonly name: "firefox" | "chromium";
11
+ };
12
+ };
13
+ /**
14
+ * True when NewBrowser takes `passthrough`. A launcher without it would
15
+ * ignore the option and launch with an injected fingerprint, so the server
16
+ * asks rather than finding out from the page.
17
+ */
18
+ PASSTHROUGH?: boolean;
19
+ /** A Browser, or with `persistent_context` the profile's BrowserContext. */
20
+ NewBrowser(browserType: BrowserType, options: Record<string, unknown>): Promise<Browser | BrowserContext>;
21
+ }
22
+ /**
23
+ * Imports a launcher by package name or path, and the Playwright browser type
24
+ * it launches, from the Playwright the launcher itself resolves.
25
+ */
26
+ export declare function loadLauncher(specifier: string): Promise<{
27
+ launcher: Launcher;
28
+ browserType: BrowserType;
29
+ }>;
30
+ export interface ServerOptions {
31
+ /**
32
+ * The person's own browser profile, which a session opened with
33
+ * `profile: true` runs on a copy of; without one, that is refused.
34
+ */
35
+ profile?: string;
36
+ secrets?: Secrets;
37
+ /** URL patterns, as for secrets, that pages may load; empty allows any. */
38
+ allowedDomains?: string[];
39
+ /**
40
+ * The one directory upload_file may attach files from; without one there
41
+ * is no upload_file, since a page could otherwise be sent any file here.
42
+ */
43
+ uploads?: string;
44
+ }
45
+ export declare function createServer(launcher: Launcher, browserType: BrowserType, { secrets, allowedDomains, profile, uploads, }?: ServerOptions): {
46
+ server: McpServer;
47
+ closeAll(): Promise<void>;
48
+ };