thumbgate 1.40.5 → 1.40.6

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.
@@ -21,7 +21,7 @@
21
21
 
22
22
  const fs = require('fs');
23
23
  const path = require('path');
24
- const { execSync } = require('child_process');
24
+ const { execFileSync } = require('child_process');
25
25
  const http = require('http');
26
26
  const https = require('https');
27
27
  const { z } = require('zod');
@@ -77,7 +77,9 @@ function httpRequest(url, options = {}) {
77
77
 
78
78
  function runAppleScript(script) {
79
79
  try {
80
- return execSync('osascript -e ' + JSON.stringify(script), {
80
+ // argv, not a shell string: no shell layer for a value to reach. The
81
+ // AppleScript layer is guarded by applescript.js when the script is built.
82
+ return execFileSync('osascript', ['-e', script], {
81
83
  encoding: 'utf-8',
82
84
  timeout: 15000,
83
85
  env: { ...process.env },
@@ -202,24 +204,68 @@ function parseEmailObject(headers, body, msg) {
202
204
  }
203
205
 
204
206
  // ---- Apple Mail integration via AppleScript ----
207
+ //
208
+ // Tool arguments come from a model and can carry text from an attacker's email.
209
+ // They are placed inside double-quoted AppleScript literals, and AppleScript can
210
+ // run shell commands, so a bare double quote would turn a search term into code.
211
+ // execFileSync('osascript', ['-e', script]) leaves no shell layer; the helpers below
212
+ // guard the AppleScript layer. Every user-controlled value goes through
213
+ // appleScriptStringLiteral().
214
+
215
+ // C0 controls (includes NUL, tab, newline, carriage return), DEL, C1 NEL,
216
+ // and the Unicode line and paragraph separators. AppleScript ends a
217
+ // statement at a line break, so none of these belong in a search term or a
218
+ // mailbox name.
219
+ // eslint-disable-next-line no-control-regex
220
+ const FORBIDDEN_CHARS = new RegExp('[\\u0000-\\u001f\\u007f\\u0085\\u2028\\u2029]');
221
+
222
+ const DEFAULT_LIMIT = 10;
223
+ const MAX_LIMIT = 1000;
224
+
225
+ /** Backslash first, then quote, so the added backslashes are not doubled. */
226
+ function escapeAppleScriptString(value) {
227
+ return String(value).replace(/\\/g, '\\\\').replace(/"/g, '\\"');
228
+ }
205
229
 
206
- function readAppleMailMessages(opts = {}) {
207
- const { inbox = 'Inbox', senderContains = '', subjectContains = '', limit = 10 } = opts;
230
+ /** Throws on control characters or line breaks. Returns the value as a string. */
231
+ function assertSafeAppleScriptField(fieldName, value) {
232
+ const text = String(value);
233
+ if (FORBIDDEN_CHARS.test(text)) {
234
+ throw new Error(`${fieldName} must not contain control characters or line breaks`);
235
+ }
236
+ return text;
237
+ }
238
+
239
+ /** A complete, safely quoted AppleScript string literal for a user value. */
240
+ function appleScriptStringLiteral(fieldName, value) {
241
+ return `"${escapeAppleScriptString(assertSafeAppleScriptField(fieldName, value))}"`;
242
+ }
243
+
244
+ /** A whole number for a code position; never lets text reach the script. */
245
+ function boundedInteger(value, fallback = DEFAULT_LIMIT, max = MAX_LIMIT) {
246
+ const n = Number(value);
247
+ if (!Number.isFinite(n)) return fallback;
248
+ return Math.min(Math.max(Math.trunc(n), 0), max);
249
+ }
250
+
251
+ /** The Apple Mail read script. Throws if a field holds a control character. */
252
+ function buildAppleMailScript(opts = {}) {
253
+ const { inbox = 'Inbox', senderContains = '', subjectContains = '', limit = DEFAULT_LIMIT } = opts;
208
254
 
209
255
  let conditions = '';
210
256
  if (senderContains) {
211
- conditions = `whose sender contains "${senderContains}"`;
257
+ conditions = `whose sender contains ${appleScriptStringLiteral('senderContains', senderContains)}`;
212
258
  } else if (subjectContains) {
213
- conditions = `whose subject contains "${subjectContains}"`;
259
+ conditions = `whose subject contains ${appleScriptStringLiteral('subjectContains', subjectContains)}`;
214
260
  }
215
261
 
216
- const mailboxClause = inbox ? `of mailbox "${inbox}"` : '';
262
+ const mailboxClause = inbox ? `of mailbox ${appleScriptStringLiteral('mailbox', inbox)}` : '';
217
263
 
218
- const script = `
264
+ return `
219
265
  tell application "Mail"
220
266
  set theMessages to every message ${conditions} ${mailboxClause}
221
267
  set output to ""
222
- repeat with i from 1 to ${limit}
268
+ repeat with i from 1 to ${boundedInteger(limit)}
223
269
  if i > (count of theMessages) then exit repeat
224
270
  set m to item i of theMessages
225
271
  set output to output & "MESSAGE-ID: " & (message id of m) & linefeed
@@ -238,7 +284,18 @@ tell application "Mail"
238
284
  return output
239
285
  end tell
240
286
  `;
287
+ }
288
+
241
289
 
290
+ function readAppleMailMessages(opts = {}) {
291
+ let script;
292
+ try {
293
+ script = buildAppleMailScript(opts);
294
+ } catch (err) {
295
+ // A field with a control character or line break never reaches osascript.
296
+ // Same error shape runAppleScript uses, so both callers already handle it.
297
+ return JSON.stringify({ error: err.message });
298
+ }
242
299
  return runAppleScript(script);
243
300
  }
244
301
 
@@ -797,7 +854,7 @@ server.tool(
797
854
  process.env.HOME || '/Users/iganapolsky',
798
855
  'workspace/secure-yolo/scripts/verify-human-paste-anti-slop.sh',
799
856
  );
800
- execSync(`bash "${verifyScript}" check --file "${filepath}"`, {
857
+ execFileSync('bash', [verifyScript, 'check', '--file', filepath], {
801
858
  encoding: 'utf-8',
802
859
  timeout: 5000,
803
860
  stdio: ['pipe', 'pipe', 'pipe'],
@@ -966,8 +1023,29 @@ server.tool(
966
1023
 
967
1024
  // ---- Start the server ----
968
1025
 
969
- const transport = new StdioServerTransport();
970
- server.connect(transport);
1026
+ // Start only when run as the program (including through the npm bin symlink),
1027
+ // so tests can require this file for the AppleScript helpers without a server.
1028
+ const isEntrypoint = (() => {
1029
+ try {
1030
+ return fs.realpathSync(process.argv[1]) === fs.realpathSync(__filename);
1031
+ } catch {
1032
+ return false;
1033
+ }
1034
+ })();
1035
+
1036
+ if (isEntrypoint) {
1037
+ const transport = new StdioServerTransport();
1038
+ server.connect(transport);
1039
+
1040
+ process.on('SIGINT', () => process.exit(0));
1041
+ process.on('SIGTERM', () => process.exit(0));
1042
+ }
971
1043
 
972
- process.on('SIGINT', () => process.exit(0));
973
- process.on('SIGTERM', () => process.exit(0));
1044
+ module.exports = {
1045
+ escapeAppleScriptString,
1046
+ assertSafeAppleScriptField,
1047
+ appleScriptStringLiteral,
1048
+ boundedInteger,
1049
+ buildAppleMailScript,
1050
+ readAppleMailMessages,
1051
+ };