@skaleagents/swarm 0.6.19 → 0.6.20

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/README.md CHANGED
@@ -113,9 +113,10 @@ in every result. An empty finding list is not proof that a system is secure.
113
113
  The web app hosts `https://skaleagents.com/mcp` using `handleMcpRequest` from
114
114
  `@skaleagents/swarm/http`. Audit workers import `scanIac` from
115
115
  `@skaleagents/swarm/scan`. It validates each bearer credential with the API's
116
- `/api/oauth/mcp-token` endpoint before running a tool. Tokens are bound to the
117
- MCP resource and cannot access unrelated API routes. Credentials are not
118
- forwarded to the public bot directory.
116
+ `/api/oauth/mcp-token` endpoint before running a tool. A successful check is
117
+ reused for up to 60 seconds, so a revoked token can run tools for up to a
118
+ minute. Tokens are bound to the MCP resource and cannot access unrelated API
119
+ routes. Credentials are not forwarded to the public bot directory.
119
120
 
120
121
  `protectedResourceMetadata` exports the discovery response for
121
122
  `/.well-known/oauth-protected-resource/mcp`.
@@ -60,7 +60,8 @@ export function registerConsultation(server, remote) {
60
60
  server.registerTool("review_application_architecture", {
61
61
  title: "Review whole-application architecture",
62
62
  description: "Review related source files as an application. Builds a JS/TS import graph, checks Next.js server/client and runtime boundaries, maps routes and mutations, and returns evidence-backed findings plus questions covering data, auth, reliability, tests, deployment and product fit. Pass answers in context on subsequent calls. No repository fetch or execution; the connected assistant reasons about system-level tradeoffs using the returned evidence.",
63
- annotations,
63
+ // Saves a review to the account when authorized, so it is not read-only.
64
+ annotations: { ...annotations, readOnlyHint: false },
64
65
  _meta: security,
65
66
  inputSchema: {
66
67
  files: z
package/dist/http.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createHash } from "node:crypto";
1
2
  import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
2
3
  import { createServer } from "./server.js";
3
4
  import { getApiUrl } from "./config.js";
@@ -37,6 +38,16 @@ export function protectedResourceMetadata() {
37
38
  resource_documentation: "https://skaleagents.com/mcp-config",
38
39
  });
39
40
  }
41
+ // Revocation can take up to one TTL to take effect for an already-validated token.
42
+ const validationTtlMs = 60_000;
43
+ const validationCacheLimit = 10_000;
44
+ const validatedUntil = new Map();
45
+ function rememberValidation(key, until) {
46
+ // Map keeps insertion order, so the first key is the oldest entry to evict.
47
+ if (!validatedUntil.has(key) && validatedUntil.size >= validationCacheLimit)
48
+ validatedUntil.delete(validatedUntil.keys().next().value);
49
+ validatedUntil.set(key, until);
50
+ }
40
51
  export async function handleMcpRequest(request) {
41
52
  const origin = request.headers.get("origin");
42
53
  if (origin && !allowedOrigins.has(origin))
@@ -46,30 +57,35 @@ export async function handleMcpRequest(request) {
46
57
  const authorization = request.headers.get("authorization");
47
58
  if (!authorization || !/^Bearer [^\s]+$/i.test(authorization))
48
59
  return challenge();
49
- try {
50
- // Validate the opaque token with its issuer, including the MCP audience.
51
- const validation = await fetch(`${getApiUrl()}/api/oauth/mcp-token`, {
52
- headers: { Authorization: authorization, Accept: "application/json" },
53
- signal: AbortSignal.timeout(10_000),
54
- redirect: "error",
55
- cache: "no-store",
56
- });
57
- if (validation.status === 401 || validation.status === 403)
58
- return challenge();
59
- if (!validation.ok)
60
+ // Cache only successful validations, keyed by a digest so raw tokens are not kept.
61
+ const tokenKey = createHash("sha256").update(authorization.slice(7)).digest("hex");
62
+ if ((validatedUntil.get(tokenKey) ?? 0) <= Date.now()) {
63
+ try {
64
+ // Validate the opaque token with its issuer, including the MCP audience.
65
+ const validation = await fetch(`${getApiUrl()}/api/oauth/mcp-token`, {
66
+ headers: { Authorization: authorization, Accept: "application/json" },
67
+ signal: AbortSignal.timeout(10_000),
68
+ redirect: "error",
69
+ cache: "no-store",
70
+ });
71
+ if (validation.status === 401 || validation.status === 403)
72
+ return challenge();
73
+ if (!validation.ok)
74
+ return json(503, { error: "authorization_unavailable" });
75
+ const token = (await validation.json());
76
+ if (!token.active ||
77
+ token.resource !== resource ||
78
+ token.scope !== "mcp" ||
79
+ !token.expiresAt ||
80
+ token.expiresAt * 1000 <= Date.now()) {
81
+ return challenge();
82
+ }
83
+ rememberValidation(tokenKey, Math.min(Date.now() + validationTtlMs, token.expiresAt * 1000));
84
+ }
85
+ catch {
60
86
  return json(503, { error: "authorization_unavailable" });
61
- const token = (await validation.json());
62
- if (!token.active ||
63
- token.resource !== resource ||
64
- token.scope !== "mcp" ||
65
- !token.expiresAt ||
66
- token.expiresAt * 1000 <= Date.now()) {
67
- return challenge();
68
87
  }
69
88
  }
70
- catch {
71
- return json(503, { error: "authorization_unavailable" });
72
- }
73
89
  if (request.method !== "POST")
74
90
  return json(405, { error: "method_not_allowed" }, { Allow: "POST, OPTIONS" });
75
91
  if (!request.headers.get("content-type")?.startsWith("application/json"))
package/dist/iac/parse.js CHANGED
@@ -39,8 +39,8 @@ export function looksLikeIac(content) {
39
39
  /* Other format detection still applies to incomplete input. */
40
40
  }
41
41
  }
42
- return (/^\s*(?:resource|module|terraform|variable|provider)\s+["{]/m.test(content) ||
43
- /^\s*(?:["']?Resources["']?\s*:|apiVersion\s*:)/m.test(content) ||
42
+ return (/^[^\S\n]*(?:resource|module|terraform|variable|provider)\s+["{]/m.test(content) ||
43
+ /^[^\S\n]*(?:["']?Resources["']?\s*:|apiVersion\s*:)/m.test(content) ||
44
44
  /^\s*\{\s*"(?:resource|Resources|apiVersion|AWSTemplateFormatVersion)"\s*:/.test(content) ||
45
45
  looksLikePulumiYaml(content) ||
46
46
  looksLikePulumiProgram(content) ||
@@ -57,13 +57,13 @@ export function looksLikeIac(content) {
57
57
  looksLikeIamPolicy(content));
58
58
  }
59
59
  export function looksLikePulumiProgram(content) {
60
- return /(?:^|\n)\s*(?:import\s+\*\s+as\s+pulumi\b|import\s+pulumi\b|from\s+["']@pulumi\/)/.test(content);
60
+ return /(?:^|\n)[^\S\n]*(?:import\s+\*\s+as\s+pulumi\b|import\s+pulumi\b|from\s+["']@pulumi\/)/.test(content);
61
61
  }
62
62
  export function looksLikeCompose(content) {
63
- return /^\s*services\s*:/m.test(content) && /^\s+image\s*:/m.test(content);
63
+ return /^[^\S\n]*services\s*:/m.test(content) && /^[^\S\n]+image\s*:/m.test(content);
64
64
  }
65
65
  export function looksLikeDockerfile(content) {
66
- return /^\s*FROM\s+\S+/m.test(content);
66
+ return /^[^\S\n]*FROM\s+\S+/m.test(content);
67
67
  }
68
68
  function topLevelKey(content, name) {
69
69
  return new RegExp(`^(?:"${name}"|'${name}'|${name})\\s*:`, "m").test(content);
@@ -71,7 +71,7 @@ function topLevelKey(content, name) {
71
71
  function playbookShape(content) {
72
72
  if (topLevelKey(content, "hosts") && topLevelKey(content, "tasks"))
73
73
  return true;
74
- return /^\s*-\s+hosts\s*:/m.test(content) && /^\s*tasks\s*:/m.test(content);
74
+ return /^[^\S\n]*-\s+hosts\s*:/m.test(content) && /^[^\S\n]*tasks\s*:/m.test(content);
75
75
  }
76
76
  export function looksLikeAnsible(content) {
77
77
  if (looksLikeGithubWorkflow(content))
@@ -86,27 +86,27 @@ export function looksLikeHelm(content) {
86
86
  const visible = maskNonCode(content);
87
87
  if (!visible.includes("{{"))
88
88
  return false;
89
- return /^\s*(?:apiVersion|kind)\s*:/m.test(visible);
89
+ return /^[^\S\n]*(?:apiVersion|kind)\s*:/m.test(visible);
90
90
  }
91
91
  export function looksLikeGithubWorkflow(content) {
92
92
  if (topLevelKey(content, "apiVersion") || topLevelKey(content, "kind"))
93
93
  return false;
94
94
  const topOn = topLevelKey(content, "on");
95
95
  const composeOnly = topLevelKey(content, "services") &&
96
- /^\s+image\s*:/m.test(content) &&
96
+ /^[^\S\n]+image\s*:/m.test(content) &&
97
97
  !topOn;
98
98
  if (composeOnly)
99
99
  return false;
100
100
  return topOn && topLevelKey(content, "jobs");
101
101
  }
102
102
  export function looksLikeTerraformHcl(content) {
103
- return /^\s*(?:(?:resource|module|variable|provider|data|output)\s+"|(?:terraform|locals)\s*\{)/m.test(content);
103
+ return /^[^\S\n]*(?:(?:resource|module|variable|provider|data|output)\s+"|(?:terraform|locals)\s*\{)/m.test(content);
104
104
  }
105
105
  export function looksLikeBicep(content) {
106
106
  const visible = maskIaCComments(content);
107
107
  if (looksLikeTerraformHcl(visible))
108
108
  return false;
109
- return /^\s*resource\s+[A-Za-z_][A-Za-z0-9_]*\s+['"]Microsoft\./m.test(visible);
109
+ return /^[^\S\n]*resource\s+[A-Za-z_][A-Za-z0-9_]*\s+['"]Microsoft\./m.test(visible);
110
110
  }
111
111
  export function looksLikeArm(content) {
112
112
  const visible = maskIaCComments(content).trim();
@@ -138,8 +138,8 @@ export function looksLikeArm(content) {
138
138
  return root.resources.some((item) => String(object(item).type ?? "").startsWith("Microsoft."));
139
139
  }
140
140
  export function looksLikePulumiYaml(content) {
141
- return (/^\s*resources\s*:/m.test(content) &&
142
- /^\s+type\s*:\s*[A-Za-z0-9_.-]+:\S+/m.test(content));
141
+ return (/^[^\S\n]*resources\s*:/m.test(content) &&
142
+ /^[^\S\n]+type\s*:\s*[A-Za-z0-9_.-]+:\S+/m.test(content));
143
143
  }
144
144
  const terraformExpressionWarning = "Variable and local literals from the submitted files are resolved. A string of those references, format of known literals, and join of known literals are resolved. Other functions and expressions are not evaluated.";
145
145
  export function parseIac(content, requested = "auto", shared) {
@@ -221,7 +221,7 @@ export function parseIac(content, requested = "auto", shared) {
221
221
  checkShape(data);
222
222
  // The HCL parser preserves expressions but does not expose source ranges.
223
223
  // Report the resource declaration line and the exact parsed property path.
224
- const searchable = content.replace(/\/\*[\s\S]*?\*\/|(?:#|\/\/)[^\n]*/g, (match) => match.replace(/[^\n]/g, " "));
224
+ const searchable = blankComments(content, /(?:#|\/\/)[^\n]*/.source);
225
225
  for (const [type, instances] of Object.entries(object(object(data).resource))) {
226
226
  for (const [name, blocks] of Object.entries(object(instances))) {
227
227
  const declaration = new RegExp(`\\bresource\\s+"${escapeRegex(type)}"\\s+"${escapeRegex(name)}"`).exec(searchable);
@@ -503,11 +503,35 @@ function programLocation(content, offset, path) {
503
503
  path,
504
504
  };
505
505
  }
506
+ // One left-to-right pass that blanks block comments and, when given, line comments
507
+ // (newlines kept). A lazy /\*[\s\S]*?\*\// rescans to the end for every unclosed opener,
508
+ // which is quadratic on hostile input. An unclosed "/*" stays code, as before.
509
+ function blankComments(text, lineSource) {
510
+ const pattern = new RegExp(lineSource ? `\\/\\*|${lineSource}` : "\\/\\*", "g");
511
+ let out = "";
512
+ let last = 0;
513
+ let closable = true;
514
+ for (let match = pattern.exec(text); match; match = pattern.exec(text)) {
515
+ let end = pattern.lastIndex;
516
+ if (match[0] === "/*") {
517
+ // No "*/" after one opener means none after any later opener either.
518
+ const close = closable ? text.indexOf("*/", end) : -1;
519
+ if (close < 0) {
520
+ closable = false;
521
+ continue;
522
+ }
523
+ end = close + 2;
524
+ pattern.lastIndex = end;
525
+ }
526
+ out += text.slice(last, match.index) + text.slice(match.index, end).replace(/[^\n]/g, " ");
527
+ last = end;
528
+ }
529
+ return out + text.slice(last);
530
+ }
506
531
  function stripProgramComments(content) {
507
- return content
508
- .replace(/\/\*[\s\S]*?\*\//g, (match) => match.replace(/[^\n]/g, " "))
532
+ return blankComments(content)
509
533
  .replace(/(^|[^:])\/\/[^\n]*/g, (match) => match.replace(/[^\n]/g, " "))
510
- .replace(/(^|\n)\s*#[^\n]*/g, (match) => match.replace(/[^\n]/g, " "));
534
+ .replace(/(^|\n)[^\S\n]*#[^\n]*/g, (match) => match.replace(/[^\n]/g, " "));
511
535
  }
512
536
  function parsePulumiProgram(content) {
513
537
  const searchable = stripProgramComments(content);
@@ -741,14 +765,14 @@ function gitlabShape(content) {
741
765
  return false;
742
766
  if (topLevelKey(content, "stages"))
743
767
  return true;
744
- return /^\s*script\s*:/m.test(content);
768
+ return /^[^\S\n]*script\s*:/m.test(content);
745
769
  }
746
770
  function cloudBuildShape(content) {
747
771
  if (!topLevelKey(content, "steps"))
748
772
  return false;
749
773
  if (topLevelKey(content, "on") && topLevelKey(content, "jobs"))
750
774
  return false;
751
- return /^\s*-\s*name\s*:/m.test(content);
775
+ return /^[^\S\n]*-\s*name\s*:/m.test(content);
752
776
  }
753
777
  function looksLikePipeline(content) {
754
778
  if (stolenPipelineInput(content) || looksLikeServerless(content))
@@ -918,11 +942,11 @@ function parsePipeline(content, format) {
918
942
  }
919
943
  function looksLikeTfvars(content) {
920
944
  const visible = maskIaCComments(content);
921
- if (/^\s*(?:resource|module|provider)\s+["']/m.test(visible))
945
+ if (/^[^\S\n]*(?:resource|module|provider)\s+["']/m.test(visible))
922
946
  return false;
923
- if (/^\s*(?:terraform|provider|module)\s*\{/m.test(visible))
947
+ if (/^[^\S\n]*(?:terraform|provider|module)\s*\{/m.test(visible))
924
948
  return false;
925
- return /^\s*[A-Za-z_][A-Za-z0-9_]*\s*=\s*\S/m.test(visible);
949
+ return /^[^\S\n]*[A-Za-z_][A-Za-z0-9_]*\s*=\s*\S/m.test(visible);
926
950
  }
927
951
  function parseTfvars(content) {
928
952
  let data;
@@ -1235,8 +1259,8 @@ function escapeRegex(value) {
1235
1259
  }
1236
1260
  function maskNonCode(content) {
1237
1261
  const withoutBlockComments = content
1238
- .replace(/\{\{-?\s*\/\*[\s\S]*?\*\/\s*-?\}\}/g, (match) => match.replace(/[^\n]/g, " "))
1239
- .replace(/\{#[\s\S]*?#\}/g, (match) => match.replace(/[^\n]/g, " "));
1262
+ .replace(/\{\{-?\s*\/\*[\s\S]{0,4096}?\*\/\s*-?\}\}/g, (match) => match.replace(/[^\n]/g, " "))
1263
+ .replace(/\{#[\s\S]{0,4096}?#\}/g, (match) => match.replace(/[^\n]/g, " "));
1240
1264
  return withoutBlockComments
1241
1265
  .split("\n")
1242
1266
  .map((line) => {
@@ -1314,7 +1338,7 @@ function parseHelm(content) {
1314
1338
  }
1315
1339
  function parseAnsible(content) {
1316
1340
  const visible = maskNonCode(content);
1317
- const become = firstLiteral(visible, /(?:^|\n)\s*(?:-\s+)?become\s*:\s*["']?(?:true|yes)["']?(?=\s|$)/);
1341
+ const become = firstLiteral(visible, /(?:^|\n)[^\S\n]*(?:-\s+)?become\s*:\s*["']?(?:true|yes)["']?(?=\s|$)/);
1318
1342
  const cidr = firstLiteral(visible, /(?:cidr|source)\s*:\s*(?:\|\s*)?(?:\n\s*-\s*)?["']?0\.0\.0\.0\/0\b/);
1319
1343
  return {
1320
1344
  format: "ansible",
@@ -1532,7 +1556,7 @@ function substituteKnownParameters(template, parameters) {
1532
1556
  let unresolved = false;
1533
1557
  let secret = false;
1534
1558
  const names = [];
1535
- const text = template.replace(/\$\{(!?)([^}]*)\}/g, (full, literalMark, rawName) => {
1559
+ const text = template.replace(/\$\{(!?)([^}]{0,200})\}/g, (full, literalMark, rawName) => {
1536
1560
  if (literalMark === "!")
1537
1561
  return `\${${rawName}}`;
1538
1562
  const name = rawName.trim();
@@ -1726,7 +1750,9 @@ export function looksLikePackage(content) {
1726
1750
  plainObject(root.scripts));
1727
1751
  }
1728
1752
  function scriptPipesDownload(command) {
1729
- return /\b(?:curl|wget)\b[\s\S]*\|\s*(?:ba)?sh\b/.test(command);
1753
+ // Search only after the first download word. A [\s\S]* prefix rescans per download word.
1754
+ const start = command.search(/\b(?:curl|wget)\b/);
1755
+ return start >= 0 && /\|\s*(?:ba)?sh\b/.test(command.slice(start));
1730
1756
  }
1731
1757
  function jsonKeyLine(content, key) {
1732
1758
  const match = new RegExp(`"${escapeRegex(key)}"\\s*:`).exec(content);
package/dist/review.js CHANGED
@@ -148,7 +148,7 @@ export function architectureFindings(content, focus) {
148
148
  });
149
149
  }
150
150
  if (shouldInclude(focusValue, "security") &&
151
- /^\s*USER\s+root\s*$/im.test(content)) {
151
+ /^[^\S\n]*USER\s+root\s*$/im.test(content)) {
152
152
  findings.push({
153
153
  severity: "medium",
154
154
  title: "Container runs as root",
@@ -175,7 +175,7 @@ export function architectureFindings(content, focus) {
175
175
  "Retry limit is unbounded": /(?:max_?retries|retry_?count|retries)\s*[:=]\s*(?:-1|Infinity)\b/i,
176
176
  "Database connection limit is unlimited": /(?:max_?open_?conns|maxOpenConns|SetMaxOpenConns)\s*(?:[:=]|\()\s*0\b/,
177
177
  "Memory limit is disabled": /(?:mem_limit|memory_limit)\s*[:=]\s*["']?0\b/i,
178
- "Container runs as root": /^\s*USER\s+root\s*$/im,
178
+ "Container runs as root": /^[^\S\n]*USER\s+root\s*$/im,
179
179
  };
180
180
  const pattern = matchers[finding.title];
181
181
  const matches = pattern
package/dist/server.js CHANGED
@@ -60,7 +60,7 @@ export function createServer(remote = false) {
60
60
  title: "Review architecture",
61
61
  description: "Review individual source snippets or parsed infrastructure for security, reliability, and cost risks. Application checks include infrastructure bottlenecks: a disabled timeout, a one-connection pool, and an unbounded retry limit. For a whole application or Next.js architecture consultation, use plan_architecture_review and review_application_architecture instead.",
62
62
  annotations: {
63
- readOnlyHint: true,
63
+ readOnlyHint: false,
64
64
  destructiveHint: false,
65
65
  openWorldHint: false,
66
66
  },
@@ -148,7 +148,7 @@ export function createServer(remote = false) {
148
148
  : "") +
149
149
  "Parse Terraform HCL/JSON, CloudFormation YAML/JSON, Kubernetes manifests, Pulumi YAML, Pulumi programs, or Docker Compose. Includes Dockerfile, GitHub Actions, Helm templates, Ansible playbooks, Bicep files, ARM templates, GitLab CI, Azure Pipelines, Cloud Build, tfvars, Serverless Framework, a standalone IAM policy, and package.json. Helm templates, Ansible facts, Bicep modules, ARM expressions, pipelines, Serverless Framework config, and IAM policies are not executed. Conditions are not evaluated. Terraform module sources are not fetched. Check security, reliability and cost rules with resource locations and remediation. Pulumi programs are scanned as text.",
150
150
  annotations: {
151
- readOnlyHint: true,
151
+ readOnlyHint: false,
152
152
  destructiveHint: false,
153
153
  openWorldHint: false,
154
154
  },
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.6.19";
1
+ export declare const VERSION = "0.6.20";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.6.19";
1
+ export const VERSION = "0.6.20";
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@skaleagents/swarm",
3
- "version": "0.6.19",
3
+ "version": "0.6.20",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "type": "module",
9
9
  "description": "SkaleAgents review tools with OAuth for local and hosted MCP clients",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/SkaleAgents/mcp-server.git"
13
+ },
10
14
  "exports": {
11
15
  "./http": {
12
16
  "types": "./dist/http.d.ts",