bushido-tradingview-mcp 0.1.2 → 0.1.3

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/build/cdp.js CHANGED
@@ -11,6 +11,46 @@ export class TradingViewNotAvailableError extends Error {
11
11
  this.name = "TradingViewNotAvailableError";
12
12
  }
13
13
  }
14
+ /**
15
+ * `localhost`, `127.0.0.1` and `[::1]` name the same machine, and which of them the
16
+ * endpoint echoes back is not ours to decide — Chrome answers with whatever Host it was
17
+ * asked on. Treating them as interchangeable keeps a working setup working; it does not
18
+ * widen the check, because all three stay on this host.
19
+ */
20
+ const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "[::1]"]);
21
+ const defaultPort = (protocol) => protocol === "wss:" || protocol === "https:" ? "443" : "80";
22
+ /**
23
+ * The `/json` response is whatever answered the CDP port, so `webSocketDebuggerUrl` is
24
+ * untrusted input. `findChartTarget` validates the *page* URL, but the socket opened next
25
+ * is a separate address that nothing checked: an endpoint that squats the port can pass
26
+ * the page check with a genuine-looking tradingview.com/chart URL and still point the
27
+ * debugger session at a host of its choosing, carrying everything sent over it — Pine
28
+ * source, alert payloads — off this machine. That defeats the operational rule of opening
29
+ * the debug port only while the server is in use, since a closed port is exactly what
30
+ * another process is free to take.
31
+ */
32
+ export function assertDebuggerWebSocketUrl(candidate, baseUrl) {
33
+ let socket;
34
+ try {
35
+ socket = new URL(candidate);
36
+ }
37
+ catch {
38
+ throw new TradingViewNotAvailableError("the chart target did not supply a usable webSocketDebuggerUrl");
39
+ }
40
+ if (socket.protocol !== "ws:" && socket.protocol !== "wss:") {
41
+ throw new TradingViewNotAvailableError("the chart target's webSocketDebuggerUrl is not a WebSocket URL");
42
+ }
43
+ // The candidate is never echoed: it is attacker-shaped by assumption and may carry a
44
+ // query string of its own.
45
+ const endpoint = new URL(baseUrl);
46
+ const sameHost = socket.hostname === endpoint.hostname ||
47
+ (LOOPBACK_HOSTNAMES.has(socket.hostname) && LOOPBACK_HOSTNAMES.has(endpoint.hostname));
48
+ const samePort = (socket.port || defaultPort(socket.protocol)) === (endpoint.port || defaultPort(endpoint.protocol));
49
+ if (!sameHost || !samePort) {
50
+ throw new TradingViewNotAvailableError("the chart target's webSocketDebuggerUrl points somewhere other than the CDP endpoint itself");
51
+ }
52
+ return socket.href;
53
+ }
14
54
  /**
15
55
  * Minimal CDP client bound to the TradingView chart page.
16
56
  * Connects lazily and reconnects automatically if the app restarts.
@@ -97,12 +137,29 @@ export class CdpClient {
97
137
  */
98
138
  async connect() {
99
139
  const target = await this.findChartTarget();
100
- const ws = new WebSocket(target.webSocketDebuggerUrl, {
140
+ const socketUrl = assertDebuggerWebSocketUrl(target.webSocketDebuggerUrl, this.baseUrl);
141
+ const ws = new WebSocket(socketUrl, {
101
142
  maxPayload: 256 * 1024 * 1024,
102
143
  });
103
144
  const pending = new Map();
104
145
  ws.on("message", (data) => {
105
- const msg = JSON.parse(data.toString());
146
+ // This handler is synchronous, so a throw here is an unhandled listener exception
147
+ // and the process exits — there is no uncaughtException handler. A frame that is
148
+ // not JSON is not a reply to anything we sent, so drop it and let the request it
149
+ // would have answered fail on its own timeout.
150
+ let msg;
151
+ try {
152
+ const parsed = JSON.parse(data.toString());
153
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
154
+ throw new Error("frame is not a CDP message object");
155
+ }
156
+ msg = parsed;
157
+ }
158
+ catch (err) {
159
+ console.error(`[tradingview-mcp] ignoring unparsable CDP frame: ` +
160
+ redactSecrets(err instanceof Error ? err.message : String(err)));
161
+ return;
162
+ }
106
163
  if (msg.id !== undefined && pending.has(msg.id)) {
107
164
  pending.get(msg.id).resolve(msg);
108
165
  pending.delete(msg.id);
package/build/redact.js CHANGED
@@ -3,10 +3,27 @@
3
3
  * local logs: URL userinfo (user:pass@host), URL query strings (which may
4
4
  * carry session ids) and bearer/token values. Surrounding text is kept so
5
5
  * error messages stay actionable.
6
+ *
7
+ * The scheme runs are bounded rather than open-ended. An unbounded
8
+ * `[\w+.-]*` before `://` costs one scan of the whole remaining string at
9
+ * every letter that turns out not to start a scheme, which is quadratic: a
10
+ * 40 KB message took 2.9 s and an 80 KB one 11.7 s. This function runs on
11
+ * every tool error and on every page exception, both of which can carry
12
+ * text the page chose, so a long enough message froze the single Node
13
+ * thread. `{0,64}` caps the per-position cost; no real scheme comes close.
6
14
  */
15
+ /**
16
+ * Nothing useful is lost by capping the result: this is diagnostic text, not
17
+ * data. Truncation happens after redaction, so a secret can never survive by
18
+ * straddling the cut.
19
+ */
20
+ export const MAX_REDACTED_CHARS = 4096;
7
21
  export function redactSecrets(text) {
8
- return text
9
- .replace(/([a-z][\w+.-]*:\/\/)[^\s/@]+@/gi, "$1***@")
10
- .replace(/([a-z][\w+.-]*:\/\/[^\s?"'<>()[\]]+)\?[^\s"'<>()[\]]*/gi, "$1?***")
22
+ const redacted = text
23
+ .replace(/([a-z][\w+.-]{0,64}:\/\/)[^\s/@]+@/gi, "$1***@")
24
+ .replace(/([a-z][\w+.-]{0,64}:\/\/[^\s?"'<>()[\]]+)\?[^\s"'<>()[\]]*/gi, "$1?***")
11
25
  .replace(/\b(bearer|token|api[_-]?key|authorization)([=:]\s*|\s+)[\w.~+/-]+=*/gi, "$1$2***");
26
+ return redacted.length <= MAX_REDACTED_CHARS
27
+ ? redacted
28
+ : `${redacted.slice(0, MAX_REDACTED_CHARS)}… [truncated]`;
12
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bushido-tradingview-mcp",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "MCP server that lets AI analyze the TradingView desktop app via Chrome DevTools Protocol",
5
5
  "keywords": [
6
6
  "mcp",