From bbe8bf9e8f9dd9bce0eacfb1076af64f14de644f Mon Sep 17 00:00:00 2001 From: Ranjan Mohanty Date: Sun, 15 Nov 2026 23:39:16 +0100 Subject: [PATCH] Codacy: fix and annotate findings, exclude demo tooling, add the grade badge Fixes: the local App server's async request handler (an error now ends the flow instead of becoming an unhandled rejection); the browser-open error says to open the link; shuffle uses crypto.randomInt; an unused test import; log_message matches its base signature. Annotated with reasons (intended behaviour): the agent's subprocess use, the hook server on 0.0.0.0, /tmp on Lambda, the MicroVM running as root, the CloudFormation response URL, and async functions whose errors propagate to their callers. Co-Authored-By: Claude Opus 5.5 --- .codacy.yml | 3 +++ README.md | 2 +- agent/runway_agent.py | 14 ++++++++------ agent/test_runway_agent.py | 6 +++--- images/microvm/Dockerfile | 3 +++ src/cli/aws.ts | 1 + src/cli/create-app.ts | 11 ++++++++--- src/cli/deploy.ts | 1 + src/cli/github-app.ts | 5 +++-- src/core/images.ts | 1 + src/lambda/bootstrap.ts | 4 ++++ src/lambda/sweeper.ts | 1 + src/providers/ec2.ts | 3 ++- 13 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 .codacy.yml diff --git a/.codacy.yml b/.codacy.yml new file mode 100644 index 0000000..89cfc52 --- /dev/null +++ b/.codacy.yml @@ -0,0 +1,3 @@ +# Codacy: paths that don't ship to users (demo tooling). +exclude_paths: + - "scripts/demo-gif.mjs" diff --git a/README.md b/README.md index e5f3e86..96ec64c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Lambda MicroVMs · EC2 (on-demand and Spot) · ECS Fargate · Lambda functions · one `runs-on` line · MIT -[![CI](https://github.com/mintwork/runway/actions/workflows/ci.yml/badge.svg)](https://github.com/mintwork/runway/actions/workflows/ci.yml) [![npm](https://img.shields.io/npm/v/@mintwork/runway?color=cb3837&logo=npm&label=npm)](https://www.npmjs.com/package/@mintwork/runway) [![npm downloads](https://img.shields.io/npm/dm/@mintwork/runway?color=cb3837&logo=npm&label=downloads)](https://www.npmjs.com/package/@mintwork/runway) [![npm provenance](https://img.shields.io/badge/provenance-signed-7EE2B8?labelColor=0D1714&logo=sigstore&logoColor=white)](https://www.npmjs.com/package/@mintwork/runway#provenance) [![Node 22+](https://img.shields.io/badge/node-%E2%89%A522-339933?logo=nodedotjs&logoColor=white)](package.json) [![license: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE) [![GitHub stars](https://img.shields.io/github/stars/mintwork/runway?style=social)](https://github.com/mintwork/runway/stargazers) +[![CI](https://github.com/mintwork/runway/actions/workflows/ci.yml/badge.svg)](https://github.com/mintwork/runway/actions/workflows/ci.yml) [![Codacy grade](https://app.codacy.com/project/badge/Grade/dd55188dca4c423cbcebcb0362c33f2b)](https://app.codacy.com/gh/mintwork/runway/dashboard) [![npm](https://img.shields.io/npm/v/@mintwork/runway?color=cb3837&logo=npm&label=npm)](https://www.npmjs.com/package/@mintwork/runway) [![npm downloads](https://img.shields.io/npm/dm/@mintwork/runway?color=cb3837&logo=npm&label=downloads)](https://www.npmjs.com/package/@mintwork/runway) [![npm provenance](https://img.shields.io/badge/provenance-signed-7EE2B8?labelColor=0D1714&logo=sigstore&logoColor=white)](https://www.npmjs.com/package/@mintwork/runway#provenance) [![Node 22+](https://img.shields.io/badge/node-%E2%89%A522-339933?logo=nodedotjs&logoColor=white)](package.json) [![license: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE) [![GitHub stars](https://img.shields.io/github/stars/mintwork/runway?style=social)](https://github.com/mintwork/runway/stargazers) runway setup deploying the stack, creating the GitHub App in the browser and building images (a real install, sped up), then a workflow using four compute types, then runway status listing a runner on each diff --git a/agent/runway_agent.py b/agent/runway_agent.py index 112c224..a1a2d6c 100644 --- a/agent/runway_agent.py +++ b/agent/runway_agent.py @@ -17,7 +17,7 @@ import os import shutil import signal -import subprocess +import subprocess # nosec B404 - the agent exists to run the runner and the container tools import sys import threading import time @@ -141,7 +141,8 @@ def _wait_for(check, timeout, interval=0.5, clock=time.monotonic): def _quiet(cmd): - return subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0 + # cmd is a fixed argument list built in this file, never a shell string. + return subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0 # nosec B603 # nosemgrep def start_docker(storage=None, containerd_timeout=120, popen=subprocess.Popen): @@ -233,7 +234,7 @@ class HookServer(BaseHTTPRequestHandler): warmup = None lock = threading.Lock() - def log_message(self, fmt, *args): # keep stdout for structured logs + def log_message(self, format, *args): # pylint: disable=redefined-builtin # keep stdout for structured logs pass def _reply(self, status): @@ -303,7 +304,8 @@ def _run_job(payload): def serve(): port = env_int("RUNWAY_HOOK_PORT", 9000) - server = ThreadingHTTPServer(("0.0.0.0", port), HookServer) + # Lambda MicroVMs call the hooks over the VM's network interface, so loopback isn't enough. + server = ThreadingHTTPServer(("0.0.0.0", port), HookServer) # nosec B104 log("hook server listening", port=port) server.serve_forever() @@ -324,7 +326,7 @@ def oneshot(): return job.run() -def prepare_lambda_runner(source="/opt/actions-runner", target="/tmp/actions-runner"): +def prepare_lambda_runner(source="/opt/actions-runner", target="/tmp/actions-runner"): # nosec B108 - /tmp is Lambda's only writable path """The Lambda filesystem is read-only outside /tmp, and the runner writes next to itself.""" if not os.path.exists(os.path.join(target, "run.sh")): started = time.monotonic() @@ -338,7 +340,7 @@ def lambda_handler(event, context): if not jit: log("invocation has no jitConfig") return {"ok": False} - os.environ.setdefault("HOME", "/tmp") + os.environ.setdefault("HOME", "/tmp") # nosec B108 - /tmp is Lambda's only writable path runner_dir = prepare_lambda_runner() remaining = int(context.get_remaining_time_in_millis() / 1000) - 20 if context else 880 job = Job(runner_dir, jit, max_seconds=max(60, remaining), idle_seconds=min(env_int("RUNWAY_IDLE_SECONDS", 300), max(60, remaining))) diff --git a/agent/test_runway_agent.py b/agent/test_runway_agent.py index 9628522..43630bf 100644 --- a/agent/test_runway_agent.py +++ b/agent/test_runway_agent.py @@ -2,7 +2,6 @@ import json import os import threading -import time import unittest import urllib.error import urllib.request @@ -185,7 +184,7 @@ def tearDown(self): def post(self, hook, body=None): req = urllib.request.Request(self.base + agent.HOOK_PREFIX + hook, data=json.dumps(body or {}).encode(), method="POST") try: - return urllib.request.urlopen(req).status + return urllib.request.urlopen(req).status # nosec B310 - a local http:// test server except urllib.error.HTTPError as e: return e.code @@ -248,7 +247,8 @@ class LambdaTest(unittest.TestCase): def test_runs_the_job_then_discards_the_environment(self): ctx = mock.Mock() ctx.get_remaining_time_in_millis.return_value = 900_000 - with mock.patch.object(agent, "prepare_lambda_runner", return_value="/tmp/actions-runner"), \ + LAMBDA_RUNNER_DIR = "/tmp/actions-runner" # nosec B108 - Lambda's only writable path + with mock.patch.object(agent, "prepare_lambda_runner", return_value=LAMBDA_RUNNER_DIR), \ mock.patch.object(agent.Job, "run", return_value=0) as run, \ mock.patch.object(agent.os, "_exit") as exit_: agent.lambda_handler({"jitConfig": "J", "runnerName": "runway-a"}, ctx) diff --git a/images/microvm/Dockerfile b/images/microvm/Dockerfile index a71be03..b19f3e3 100644 --- a/images/microvm/Dockerfile +++ b/images/microvm/Dockerfile @@ -28,4 +28,7 @@ ENV HOME=/root \ DOTNET_NOLOGO=1 EXPOSE 9000 +# Runs as root on purpose: the agent starts containerd and dockerd, and each MicroVM runs one job +# and is then discarded. +# nosemgrep: dockerfile.security.missing-user.missing-user CMD ["python3", "/opt/runway/runway_agent.py", "serve"] diff --git a/src/cli/aws.ts b/src/cli/aws.ts index 5688190..49737ba 100644 --- a/src/cli/aws.ts +++ b/src/cli/aws.ts @@ -144,6 +144,7 @@ async function waitForStack( since: Date, ): Promise { const p = progress(label); + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them for (;;) { const stack = await describeStack(cf, name); const status = stack?.StackStatus ?? 'DELETE_COMPLETE'; diff --git a/src/cli/create-app.ts b/src/cli/create-app.ts index 3d19ed4..497ca91 100644 --- a/src/cli/create-app.ts +++ b/src/cli/create-app.ts @@ -3,7 +3,7 @@ // GitHub sends the browser back to it with a one-time code, which the CLI redeems for the App's // credentials. The key goes from GitHub to this process to SSM, and nowhere else. import { randomBytes, timingSafeEqual } from 'node:crypto'; -import { createServer, type Server } from 'node:http'; +import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'; import type { AddressInfo } from 'node:net'; import { convertManifestCode, @@ -64,7 +64,12 @@ export function createApp(opts: CreateAppOptions): Promise { else resolve(app as StoredApp); }; - server = createServer(async (req, res) => { + // A plain (non-async) listener, so an unexpected error ends the flow instead of becoming an + // unhandled rejection. + server = createServer((req, res) => { + handle(req, res).catch((e: unknown) => finish(e instanceof Error ? e : new Error(String(e)))); + }); + const handle = async (req: IncomingMessage, res: ServerResponse): Promise => { const url = new URL(req.url ?? '/', base); const send = (status: number, html: string, headers: Record = {}) => { res.writeHead(status, { @@ -116,7 +121,7 @@ export function createApp(opts: CreateAppOptions): Promise { return; } send(404, page('Not found', '

Not found

')); - }); + }; server.on('error', (e) => finish(e)); // Loopback only: nothing else on the network can reach it. server.listen(0, '127.0.0.1', () => { diff --git a/src/cli/deploy.ts b/src/cli/deploy.ts index b28b084..6130913 100644 --- a/src/cli/deploy.ts +++ b/src/cli/deploy.ts @@ -189,6 +189,7 @@ async function startBuilds(t: Target, maintainer: string): Promise { Payload: Buffer.from(JSON.stringify({ force: true })), }), ); + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them if (res.FunctionError) { const detail = Buffer.from(res.Payload ?? []).toString(); throw new CliError( diff --git a/src/cli/github-app.ts b/src/cli/github-app.ts index 969de46..5a2df79 100644 --- a/src/cli/github-app.ts +++ b/src/cli/github-app.ts @@ -2,7 +2,7 @@ import { spawn } from 'node:child_process'; import type { AppCredentials } from '../core/github.js'; import { appJwt } from '../core/github.js'; -import { CliError } from './ui.js'; +import { CliError, info } from './ui.js'; const API = 'https://api.github.com'; const headers = { @@ -17,7 +17,7 @@ export function openBrowser(url: string): void { const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]; try { spawn(cmd, args, { stdio: 'ignore', detached: true }) - .on('error', () => {}) + .on('error', () => info(`Couldn't open a browser; open the link above.`)) .unref(); } catch { // The URL is printed too. @@ -68,6 +68,7 @@ export async function waitForInstallation( timeoutMs = 15 * 60_000, ): Promise { const start = Date.now(); + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them for (;;) { const found = (await installations(creds)).find( (i) => i.account.login.toLowerCase() === owner.toLowerCase(), diff --git a/src/core/images.ts b/src/core/images.ts index e9d307d..7c0b40f 100644 --- a/src/core/images.ts +++ b/src/core/images.ts @@ -343,6 +343,7 @@ export async function waitForSettledImage( sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)), ): Promise { const deadline = Date.now() + timeoutMs; + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them for (;;) { const { state } = await microvmImageState(client, arn); if (state && SETTLED.has(state)) return; diff --git a/src/lambda/bootstrap.ts b/src/lambda/bootstrap.ts index 7b1bb37..d87251a 100644 --- a/src/lambda/bootstrap.ts +++ b/src/lambda/bootstrap.ts @@ -66,6 +66,7 @@ async function exists(name: string): Promise { /** EC2 Spot and ECS create these on first use, but only for callers allowed to create IAM roles. */ async function ensureServiceLinkedRoles(): Promise { const iam = new IAMClient({}); + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them for (const AWSServiceName of ['spot.amazonaws.com', 'ecs.amazonaws.com']) { await iam.send(new CreateServiceLinkedRoleCommand({ AWSServiceName })).catch((e: Error) => { if (!/has been taken|already exists/i.test(e.message)) throw e; @@ -142,6 +143,7 @@ async function cleanUp(): Promise { for (const Name of [...n.params, n.legacySetupToken]) await ssm.send(new DeleteParameterCommand({ Name })).catch(() => {}); let ContinuationToken: string | undefined; + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them do { const page = await s3.send(new ListObjectsV2Command({ Bucket: n.bucket, ContinuationToken })); const keys = (page.Contents ?? []).map((o) => ({ Key: o.Key as string })); @@ -170,6 +172,8 @@ async function respond( LogicalResourceId: event.LogicalResourceId, Data: data, }); + // CloudFormation's presigned response URL for this custom resource; it's meant to be called. + // nosemgrep await fetch(event.ResponseURL, { method: 'PUT', body, headers: { 'content-type': '' } }); } diff --git a/src/lambda/sweeper.ts b/src/lambda/sweeper.ts index 7249f24..ba3b926 100644 --- a/src/lambda/sweeper.ts +++ b/src/lambda/sweeper.ts @@ -5,6 +5,7 @@ import { log, runtimeDeps } from './runtime.js'; export async function handler(): Promise { const result = await sweep(await runtimeDeps()); + // eslint-disable-next-line security-node/detect-unhandled-async-errors -- errors propagate to the caller, which reports them if (result.terminated.length || result.orphans.length || result.relaunched.length) log('sweep', { ...result }); } diff --git a/src/providers/ec2.ts b/src/providers/ec2.ts index 63925d6..450ab44 100644 --- a/src/providers/ec2.ts +++ b/src/providers/ec2.ts @@ -1,6 +1,7 @@ // EC2: one instance per job, on-demand or Spot, any instance type. The user-data script // installs Docker and the runner on Amazon Linux 2023, runs the agent for one job, and // shuts down; InstanceInitiatedShutdownBehavior=terminate turns that into termination. +import { randomInt } from 'node:crypto'; import { DescribeInstancesCommand, EC2Client, @@ -201,7 +202,7 @@ export class Ec2Provider implements Provider { function shuffle(items: T[]): T[] { const a = [...items]; for (let i = a.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); + const j = randomInt(i + 1); [a[i], a[j]] = [a[j], a[i]]; } return a;