diff --git a/README.md b/README.md index a9d8e9b..cff0717 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # `>_ pkgdash_` **One dashboard for every package manager on your Mac**: inventory, CVE audits, malware checks and -updates for uv, npm, Homebrew, pip, Go, Cargo, Docker images, and the lockfiles of your own projects. +updates for uv, npm, Homebrew, pip, Go, Cargo, Ruby gems, Docker images, and the lockfiles of your own projects. [![CI](https://github.com/adamtty/pkgdash/actions/workflows/ci.yml/badge.svg)](https://github.com/adamtty/pkgdash/actions/workflows/ci.yml) [![CodeQL](https://github.com/adamtty/pkgdash/actions/workflows/codeql.yml/badge.svg)](https://github.com/adamtty/pkgdash/actions/workflows/codeql.yml) @@ -111,11 +111,12 @@ screenshots are made. | pip | Homebrew Pythons + `--user` installs in macOS python; flags strays | `pip-audit` | disabled by design (use uv) | | go | `go version -m` + `go list -m @latest` | `govulncheck -mode=binary` | `go install @latest` (rebuilds with current Go) | | cargo | `cargo install --list` + crates.io | OSV (top-level crate only) | `cargo install --locked` | -| docker | `docker save` per image (cached by image id; nothing runs) | OSV, incl. Debian/Ubuntu/Alpine | — | +| gem | Homebrew Ruby's gems + `gem outdated`; the ones that ship with Ruby only count as outdated when OSV knows a vulnerability in them | OSV, every installed version | `gem install --version --conservative` | +| docker | `docker save` per image (cached by image id; nothing runs): OS packages, Python, npm and Ruby gems | OSV, incl. Debian/Ubuntu/Alpine | — | | other | ~/.local/bin, /usr/local/bin, extra managers | — | — | Single packages: each outdated package has its own `> update` button (`brew upgrade --formula/--cask `, -`uv tool upgrade `, `npm i -g @latest`, `go install …@latest`, `cargo install --locked `), +`uv tool upgrade `, `npm i -g @latest`, `go install …@latest`, `cargo install --locked `, `gem install --version …`), followed by a re-scan. The server only accepts names its last scan found. ### Supply-chain guards @@ -124,9 +125,9 @@ followed by a re-scan. The server only accepts names its last scan found. Cooldown (cooldown_days, default 5) Updates wait until a release is that old, since hijacked releases are usually caught and pulled within days. -uv uses `--exclude-newer` and npm `--before`, which also cover their dependencies; brew/go/cargo skip packages -still cooling (cargo is pinned to the scanned version). Release dates come from PyPI, the npm registry, the -Go proxy and crates.io; Homebrew has none, so formulae use the build time of the version's first bottle on +uv uses `--exclude-newer` and npm `--before`, which also cover their dependencies; brew/go/cargo/gem skip packages +still cooling (cargo and gem are pinned to the scanned version). Release dates come from PyPI, the npm registry, the +Go proxy, crates.io and rubygems.org; Homebrew has none, so formulae use the build time of the version's first bottle on ghcr.io, and casks (no bottles) start the clock when a scan first sees the version. pip packages show no cooldown, since pkgdash doesn't update them. @@ -157,7 +158,7 @@ free plan's quota is small (500, 100 per lookup); the settings page shows what's ### Projects The **projects** page checks the lockfiles of every project in the folder chosen on the setup page -(`package-lock.json`, `yarn.lock`, `uv.lock`, `poetry.lock`, `Cargo.lock`, pinned `requirements.txt`). It +(`package-lock.json`, `yarn.lock`, `uv.lock`, `poetry.lock`, `Cargo.lock`, `Gemfile.lock`, pinned `requirements.txt`). It must be inside your home folder. launchd agents can't read protected folders like `~/Documents`, so the menu bar app (which macOS asks you to allow) finds the lockfiles, skipping `node_modules`/`.git`/venvs/ `target`, and sends new or changed ones to the service hourly or when a scan asks; copies live in @@ -168,7 +169,8 @@ menu bar app (which macOS asks you to allow) finds the lockfiles, skipping `node - **fix lockfile** (npm, uv, cargo): the menu bar app shows what it's about to run and asks first, then runs `npm audit fix --package-lock-only --ignore-scripts`, `uv lock --no-build --upgrade-package …` or `cargo update …`. These rewrite the lockfile only: nothing is installed and no package code runs. Review - the result with `git diff` in the repo. + the result with `git diff` in the repo. `Gemfile.lock` isn't fixed: Bundler would run the project's + `Gemfile`, which is Ruby code, so run `bundle update ` there yourself. - **ignore:** for deliberate targets and other people's code. Their CVEs and malware stop counting. Project counts stay out of the dashboard's headline totals. `install.sh` only rebuilds the menu bar app when diff --git a/demo.py b/demo.py index 10b37e4..813dd4e 100644 --- a/demo.py +++ b/demo.py @@ -61,6 +61,8 @@ def row(name, ver, latest=None, age=30, new=False): row("urllib3", "1.26.15"), row("jinja2", "3.1.2"), row("certifi", "2024.8.30"), row("six", "1.16.0")], "python3.12"), "go": pkgs("go", [row("gopls", "v0.16.2"), row("golangci-lint", "v1.61.0"), row("staticcheck", "2024.1.1"), row("dlv", "v1.23.1")], "bin"), "cargo": pkgs("cargo", [row("cargo-edit", "0.13.0"), row("cargo-watch", "8.5.3"), row("tokei", "12.1.2"), row("bottom", "0.10.2")], "crate"), + "gem": pkgs("gem", [row("rubocop", "1.66.1", "1.67.0", 4), row("bundler", "2.5.21"), row("cocoapods", "1.15.2"), + row("rake", "13.2.1"), row("json", "2.7.2")], "gem"), "other": pkgs("other", [row("bun", "1.1.30"), row("deno", "2.0.2"), row("ollama", "0.3.14")], "bin"), } @@ -71,7 +73,8 @@ def row(name, ver, latest=None, age=30, new=False): PROJECTS = [("npm", "web/storefront", 642, "package-lock.json"), ("npm", "web/admin-panel", 588, "package-lock.json"), ("uv", "api/orders-service", 74, "uv.lock"), ("uv", "tools/report-builder", 31, "uv.lock"), ("cargo", "cli/logtail", 112, "Cargo.lock"), ("npm", "experiments/chat-widget", 391, "yarn.lock"), - ("uv", "scripts/backup", 9, "requirements.txt"), ("cargo", "games/tetris-rs", 48, "Cargo.lock")] + ("uv", "scripts/backup", 9, "requirements.txt"), ("cargo", "games/tetris-rs", 48, "Cargo.lock"), + ("bundler", "web/marketing-site", 87, "Gemfile.lock")] proj = [] for kind, rel, deps, lock in PROJECTS: p = S.pkg(rel, "%d deps" % deps, None, "yarn" if lock == "yarn.lock" else kind, lock) @@ -92,6 +95,7 @@ def row(name, ver, latest=None, age=30, new=False): F("jinja2", "3.1.2", "CVE-2024-22195", "medium", "HTML attribute injection when passing user input as keys to xmlattr", "3.1.3", "python3.12")], "go": [F("golang.org/x/net", "v0.15.0", "CVE-2023-44487", "high", "HTTP/2 rapid reset can cause excessive work", "v0.17.0", "dlv")], "cargo": [], + "gem": [F("rexml", "3.3.8", "CVE-2024-49761", "high", "ReDoS when parsing a hex numeric character reference", "3.3.9", "rexml")], "other": [], "projects": [F("lodash", "4.17.15", "CVE-2020-8203", "high", "Prototype pollution in zipObjectDeep", "4.17.19", "web/admin-panel"), F("minimist", "1.2.5", "CVE-2021-44906", "critical", "Prototype pollution", "1.2.6", "web/admin-panel"), diff --git a/index.html b/index.html index 5093f90..a515508 100644 --- a/index.html +++ b/index.html @@ -278,6 +278,7 @@ beer: '', go: '', rs: '', + rb: '', bin: '', alert: '', up: '', @@ -374,7 +375,7 @@ // seconds until a package's new version is past its cooldown period (matches cooldown_left in server.py) const coolLeft = p => { const days = Number(S.settings.cooldown_days) || 0; - return days > 0 && canUpdate(p) && p.outdated && p.latest && p.released ? Math.max(0, p.released + days * 86400 - S.now) : 0; + return days > 0 && canUpdate(p) && (p.outdated || p.newer) && p.latest && p.released ? Math.max(0, p.released + days * 86400 - S.now) : 0; }; const leftText = s => s >= 86400 ? `${Math.ceil(s / 86400)} day${Math.ceil(s / 86400) === 1 ? "" : "s"}` : `${Math.max(1, Math.ceil(s / 3600))} hour${Math.ceil(s / 3600) <= 1 ? "" : "s"}`; // matches flag_text in server.py @@ -715,8 +716,10 @@ } function latestCell(p) { if (separate(p.mid)) return projCell(p); - if (!p.outdated) return '—'; + if (!p.outdated && !(p.newer && p.latest)) return '—'; if (p.blocked) return `↑ ${esc(p.latest)} blocked: ${esc(flagText(p.blocked))}`; + // gems that ship with Ruby (no known vulnerability): a newer release, not counted as outdated + if (!p.outdated) return `↑ ${esc(p.latest)}${updBtn(p)}`; const left = coolLeft(p); const since = p.released_src === "seen" ? "counting from when pkgdash first saw it" : "counting from its release"; return `↑ ${esc(p.latest || "rebuild")}${updBtn(p)}${left ? `in cooldown period, ${leftText(left)} remaining` : ""}${socketNote(p)}`; @@ -841,7 +844,7 @@ function setupProjects() { return `
- +
Projects folder
inside your home folder. The menu bar app reads it, so macOS asks once if it's a protected folder like ~/Documents
${ROOT_PRESETS.map(r => ``).join("")}
diff --git a/menubar/PkgdashMenu.swift b/menubar/PkgdashMenu.swift index 6c2a771..e9e5b44 100644 --- a/menubar/PkgdashMenu.swift +++ b/menubar/PkgdashMenu.swift @@ -56,7 +56,7 @@ final class App: NSObject, NSApplicationDelegate { } var rootLabel: String { projectsRoot.path.replacingOccurrences(of: FileManager.default.homeDirectoryForCurrentUser.path, with: "~") } - let lockNames: Set = ["package-lock.json", "yarn.lock", "uv.lock", "poetry.lock", "Cargo.lock", "requirements.txt"] + let lockNames: Set = ["package-lock.json", "yarn.lock", "uv.lock", "poetry.lock", "Cargo.lock", "requirements.txt", "Gemfile.lock"] let skipDirs: Set = ["node_modules", "Library", "venv", "env", "target", "dist", "build", "__pycache__", "Pods", "vendor"] let syncQueue = DispatchQueue(label: "pkgdash.projects") var lastSync = Date.distantPast diff --git a/server.py b/server.py index 602e189..4130430 100644 --- a/server.py +++ b/server.py @@ -111,7 +111,7 @@ def parse_iso(s): def cooldown_left(p): """Seconds until a package's new version is old enough to install (0 = go ahead).""" days = float(store["settings"].get("cooldown_days") or 0) - if days <= 0 or not p.get("outdated") or not p.get("latest") or not p.get("released"): + if days <= 0 or not (p.get("outdated") or p.get("newer")) or not p.get("latest") or not p.get("released"): return 0 return max(0, p["released"] + days * 86400 - now()) @@ -1378,9 +1378,119 @@ def update(self, job, only=None): return "cargo install --locked " + (only[1] if only else "for each crate") +# each installed gem as [name, newest version, [every version installed], origin]; origin is "default" (part of +# Ruby itself), "bundled" (ships with Ruby, in its own gem dir) or "gem" (installed with `gem install`) +RUBY_GEMS = """ +require "json" +own = File.join(RbConfig::CONFIG["rubylibprefix"], "gems", RbConfig::CONFIG["ruby_version"]) +puts JSON.generate(Gem::Specification.group_by(&:name).sort.map { |n, ss| + [n, ss.max_by(&:version).version.to_s, ss.map { |s| s.version.to_s }.uniq, + ss.any?(&:default_gem?) ? "default" : ss.any? { |s| s.base_dir == own } ? "bundled" : "gem"] }) +""" +GEM_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$") +GEM_VERSION = re.compile(r"^[0-9][0-9A-Za-z.]*$") + + +class Gem(Manager): + id, label, icon, color = "gem", "ruby gems", "rb", "amber" + desc = "Gems in Homebrew's Ruby (`gem install`), including the ones that ship with Ruby" + osv_eco = "RubyGems" + + def ruby(self): + r = which("ruby") + return r if r and not r.startswith("/usr/bin/") else None # macOS's own Ruby belongs to Apple + + def gem(self): + return os.path.join(os.path.dirname(os.path.realpath(self.ruby())), "gem") # the one that goes with it + + def available(self): + return bool(self.ruby()) + + def gems(self, job): + rc, out = job.run([self.ruby(), "-e", RUBY_GEMS], quiet=True, timeout=120) + data = loads(out, None) + if rc != 0 or not isinstance(data, list): + raise RuntimeError("couldn't list the installed gems: " + out.strip()[-200:]) + return [x for x in data if isinstance(x, list) and len(x) == 4] + + def dep_tree(self, job): + return [(n, v, n) for n, _, vers, _ in self.gems(job) for v in vers] + + def scan(self, job): + rc, out = job.run([self.gem(), "outdated"], quiet=True, timeout=300) + if rc != 0: + job.log("! gem outdated failed (exit %s); versions only" % rc) + latest = dict(re.findall(r"^(\S+) \(\S+ < (\S+)\)$", out, re.M)) + res = [] + for n, v, vers, origin in self.gems(job): + notes = [{"default": "default gem (part of Ruby)", "bundled": "bundled with Ruby"}.get(origin, "")] + older = [x for x in vers if x != v] + if older: + notes.append("also installed: " + ", ".join(older[:5])) + p = pkg(n, v, latest.get(n), "gem", " · ".join(x for x in notes if x)) + p["origin"] = origin + res.append(p) + self.uncount_rubys_own(job, res) + return res + + def uncount_rubys_own(self, job, rows): + """Gems that ship with Ruby also get newer with `brew upgrade ruby`, so a newer release of one only counts + as outdated when OSV knows a vulnerability in the installed version. The rest are marked `newer`: still + shown, and updatable from their row, but not in the outdated counts or an update-all.""" + own = [p for p in rows if p["outdated"] and p["origin"] != "gem"] + if not own: + return + try: + data = http_json("https://api.osv.dev/v1/querybatch", {"queries": [ + {"package": {"name": p["name"], "ecosystem": "RubyGems"}, "version": p["version"]} for p in own]}, timeout=90) + except Exception as e: + job.log("! OSV check of Ruby's own gems failed, counting them all as outdated: %s" % e) + return + for p, r in zip(own, data.get("results", [])): + if not any(not x["id"].startswith("MAL-") for x in r.get("vulns") or []): + p["outdated"], p["newer"] = False, True + + def released(self, job, p): + data = http_json("https://rubygems.org/api/v1/versions/%s.json" % quote(p["name"])) + times = [parse_iso(x.get("created_at")) for x in (data if isinstance(data, list) else []) + if isinstance(x, dict) and x.get("number") == p["latest"]] + return min((t for t in times if t), default=None) # platform builds of one version come out together + + def audit(self, job): + res = osv_audit(job, [("gems", "RubyGems", [(n, v) for n, v, _ in self.dep_tree(job)], "installed gems")]) + for f in res: + f["context"] = f["pkg"] + return res + + def update(self, job, only=None): + rows = [p for key, p in sorted(inv_pkgs(self.id).items()) if not only or key == only] + if only and not rows: + raise RuntimeError("%s isn't installed with gem" % only[1]) + gate, bad = self.gate(job), [] + if only and gate.get(only): + raise RuntimeError("%s not updated: %s" % (only[1], gate[only])) + for p in rows: + # `newer` rows (Ruby's own gems, nothing known wrong) only when asked for by name + wanted = p.get("outdated") or (only and p.get("newer")) + if ("gem", p["name"]) in gate or not wanted or not p.get("latest"): + continue + if not (GEM_NAME.match(p["name"]) and GEM_VERSION.match(p["latest"])): + job.log("skip %r: not a gem name and version" % p["name"]) + continue + # the exact version the scan saw (and the cooldown dated); --conservative leaves dependencies + # that already fit alone instead of pulling their newest releases too + if job.stream([self.gem(), "install", p["name"], "--version", p["latest"], "--conservative", + "--no-document"], timeout=1800) != 0: + bad.append(p["name"]) + if bad: + raise RuntimeError("gem install failed for: " + ", ".join(bad)) + return "gem install " + (only[1] if only else "for each outdated gem") + + # lockfile name → (kind shown on the projects page, OSV ecosystem) LOCKFILES = {"package-lock.json": ("npm", "npm"), "yarn.lock": ("yarn", "npm"), "uv.lock": ("uv", "PyPI"), - "poetry.lock": ("poetry", "PyPI"), "Cargo.lock": ("cargo", "crates.io"), "requirements.txt": ("pip", "PyPI")} + "poetry.lock": ("poetry", "PyPI"), "Cargo.lock": ("cargo", "crates.io"), "requirements.txt": ("pip", "PyPI"), + "Gemfile.lock": ("bundler", "RubyGems")} def lock_deps(fname, text): @@ -1423,6 +1533,16 @@ def lock_deps(fname, text): m = re.match(r"\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*(?:\[[^\]]*\])?\s*===?\s*([^\s;#,]+)", line) if m: res.add(m.groups()) + elif fname == "Gemfile.lock": # GEM sections only: GIT and PATH sources aren't on rubygems.org + section = None + for line in text.splitlines(): + if line and not line[0].isspace(): + section = line.strip() + elif section == "GEM": + # " nokogiri (1.16.0-arm64-darwin)": specs are indented 4, their dependencies 6 + m = re.match(r" ([A-Za-z0-9][A-Za-z0-9._-]*) \(([0-9][^)\s]*)\)\s*$", line) + if m: + res.add((m.group(1), m.group(2).split("-", 1)[0])) # drop the platform else: # TOML [[package]] tables: uv.lock, poetry.lock, Cargo.lock for block in re.split(r"(?m)^\[\[package\]\]\s*$", text)[1:]: n = re.search(r'(?m)^name = "([^"]+)"', block) @@ -1541,6 +1661,9 @@ def update(self, job, only=None): if not only: raise RuntimeError("projects are fixed one at a time, from their row") kind, rel = only + if kind == "bundler": + raise RuntimeError("pkgdash doesn't fix Gemfile.lock: Bundler would run the project's Gemfile, which is Ruby " + "code. Run `bundle update ` in %s yourself" % rel) if kind not in self.fixable: raise RuntimeError("pkgdash can't fix %s lockfiles yet; update %s by hand" % (kind, rel)) proj = self.projects().get(only) @@ -1709,8 +1832,10 @@ def osv_audit(job, groups): # the files inside an image that say what's installed: OS package databases, Python dist-info, node_modules DOCKER_WANT = re.compile(r"^(?:etc/os-release|usr/lib/os-release|var/lib/dpkg/status|var/lib/dpkg/status\.d/[^/]+" r"|lib/apk/db/installed|.*/(?:site|dist)-packages/[^/]+\.dist-info/METADATA" - r"|(?:.*/)?node_modules/(?:@[^/]+/)?[^/]+/package\.json)$") + r"|(?:.*/)?node_modules/(?:@[^/]+/)?[^/]+/package\.json" + r"|.*/specifications/(?:default/)?[^/]+\.gemspec)$") DOCKER_MAX_FILE = 16 << 20 +DOCKER_CACHE_VERSION = 2 # raised when image_packages learns a new kind of package (2: Ruby gems), so images get reread def layer_files(f): @@ -1818,6 +1943,10 @@ def image_packages(fs): v = re.search(r"(?m)^Version: *(\S+)", text) if n and v: res.add(("PyPI", n.group(1), v.group(1))) + elif path.endswith(".gemspec"): # "specifications/nokogiri-1.16.0-x86_64-linux.gemspec" + m = re.match(r"(.+)-([0-9][^-]*)(?:-[^/]+)?\.gemspec$", os.path.basename(path)) + if m: + res.add(("RubyGems", m.group(1), m.group(2))) elif path.endswith("package.json"): d = loads(text, {}) if isinstance(d, dict) and isinstance(d.get("name"), str) and isinstance(d.get("version"), str): @@ -1827,7 +1956,7 @@ def image_packages(fs): class Docker(Manager): id, label, icon, color = "docker", "docker images", "layers", "blue" - desc = ("Images in the local Docker engine (OrbStack / Docker Desktop): the OS packages, Python and npm " + desc = ("Images in the local Docker engine (OrbStack / Docker Desktop): the OS packages, Python, npm and Ruby " "packages inside each one, checked for malware and CVEs. Read from `docker save`, so no container is started") actions = ("scan", "audit") separate = True # images aren't installed tools; kept out of the headline totals (malware still counts) @@ -1870,14 +1999,14 @@ def contents(self, job, iid, ref): """(distro label, [(ecosystem, name, version)]) for an image id, read once and cached.""" path = DOCKER_DIR / (iid[7:] + ".json") cached = loads(path.read_text(), None) if path.exists() else None - if cached: + if cached and cached.get("v") == DOCKER_CACHE_VERSION: return cached["os"], [tuple(x) for x in cached["packages"]] job.log("# reading %s (%s)" % (ref, iid[7:19])) t0 = now() label, pkgs = image_packages(image_files(job, iid)) job.log(" %s: %s, %d packages (%.0fs)" % (ref, label, len(pkgs), now() - t0)) DOCKER_DIR.mkdir(parents=True, exist_ok=True) - path.write_text(json.dumps({"os": label, "packages": pkgs})) + path.write_text(json.dumps({"v": DOCKER_CACHE_VERSION, "os": label, "packages": pkgs})) return label, pkgs def scan(self, job): @@ -1901,7 +2030,7 @@ def scan(self, job): p["running"] = running res.append(p) for eco, n, v in pkgs: - if eco in ("PyPI", "npm"): # OSV's malicious-packages list only covers registries + if eco in ("PyPI", "npm", "RubyGems"): # OSV's malicious-packages list only covers registries by_eco.setdefault(eco, []).append((n, v, ref, "installed")) live = {iid[7:] + ".json" for _, iid, _ in imgs} for f in DOCKER_DIR.glob("*.json") if DOCKER_DIR.exists() else []: @@ -1986,7 +2115,7 @@ def scan(self, job): return res -MANAGERS = [Uv(), Npm(), Brew(), Pip(), Go(), Cargo(), Projects(), Docker(), Other()] +MANAGERS = [Uv(), Npm(), Brew(), Pip(), Go(), Cargo(), Gem(), Projects(), Docker(), Other()] MANAGER_BY_ID = {m.id: m for m in MANAGERS} runner = Runner() diff --git a/tests/fuzz-corpus/lockfiles/Gemfile.lock.seed b/tests/fuzz-corpus/lockfiles/Gemfile.lock.seed new file mode 100644 index 0000000..1dfda63 --- /dev/null +++ b/tests/fuzz-corpus/lockfiles/Gemfile.lock.seed @@ -0,0 +1,35 @@ +GIT + remote: https://github.com/example/forked-gem.git + revision: 0123456789abcdef0123456789abcdef01234567 + specs: + forked-gem (0.9.0) + +PATH + remote: vendor/local-gem + specs: + local-gem (1.0.0) + +GEM + remote: https://rubygems.org/ + specs: + nokogiri (1.16.0-arm64-darwin) + racc (~> 1.4) + nokogiri (1.16.0-x86_64-linux) + racc (~> 1.4) + racc (1.7.3) + rails (7.1.3) + actionpack (= 7.1.3) + rspec-core (3.13.0.pre.beta1) + +PLATFORMS + arm64-darwin + x86_64-linux + +DEPENDENCIES + forked-gem! + local-gem! + nokogiri + rails (~> 7.1) + +BUNDLED WITH + 2.5.4 diff --git a/tests/test_server.py b/tests/test_server.py index 563bec8..bce7fb6 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -79,6 +79,132 @@ def deps(seed, fname): self.assertEqual(deps("Cargo.lock", "Cargo.lock"), [("serde", "1.0.190")]) self.assertEqual(deps("uv.lock", "uv.lock"), [("requests", "2.31.0")]) self.assertEqual(deps("poetry.lock", "poetry.lock"), [("click", "8.1.7")]) + # registry gems only (not GIT/PATH ones), platform builds folded into one version + self.assertEqual(deps("Gemfile.lock", "Gemfile.lock"), + [("nokogiri", "1.16.0"), ("racc", "1.7.3"), ("rails", "7.1.3"), ("rspec-core", "3.13.0.pre.beta1")]) + + def test_hostile_gemfile_locks_parse_to_nothing(self): + for text in ("", "GEM\n specs:\n (1.0)\n", "GEM\n -rf (1.0)\n", "GEM\n x (../../etc)\n", + "GEM\n x (1.0:evil)\n", "GIT\n x (1.0)\n", "GEM\n deep (1.0)\n", "GEM\n x ()\n"): + self.assertEqual(server.lock_deps("Gemfile.lock", text), [], repr(text)) + self.assertEqual(server.lock_deps("Gemfile.lock", "GEM\r\n x (1.0)\r\n"), [("x", "1.0")]) + + +class Gems(unittest.TestCase): + """The ruby gems manager, with a stand-in for running ruby and gem.""" + + class Job: + only, force, id = None, False, "t" + + def __init__(self, outputs): + self.outputs, self.ran, self.logged = outputs, [], [] + + def log(self, text): + self.logged.append(text) + + def run(self, cmd, cwd=None, timeout=0, quiet=False): + self.ran.append([str(c) for c in cmd]) + return self.outputs["ruby" if cmd[1] == "-e" else "outdated"] + + def stream(self, cmd, cwd=None, timeout=0): + self.ran.append([str(c) for c in cmd]) + return 0 + + GEMS = json.dumps([["json", "2.18.0", ["2.18.0", "2.9.1"], "default"], ["rake", "13.3.0", ["13.3.0"], "bundled"], + ["rubocop", "1.80.0", ["1.80.0"], "gem"], ["tidy", "1.0", ["1.0"], "gem"]]) + OUTDATED = "json (2.18.0 < 2.19.9)\nrake (13.3.0 < 13.4.2)\nrubocop (1.80.0 < 1.81.2)\n" + + def osv(self, url, body=None, timeout=0): + """OSV's querybatch, knowing one advisory: in json 2.18.0.""" + self.osv_asked.append([q["package"]["name"] for q in body["queries"]]) + return {"results": [{"vulns": [{"id": "GHSA-3m6g-2423-7cp3"}]} if q["package"]["name"] == "json" else {} + for q in body["queries"]]} + + def setUp(self): + self.osv_asked = [] + self.orig_http = server.http_json + server.http_json = self.osv + self.addCleanup(setattr, server, "http_json", self.orig_http) + self.gem = server.Gem() + self.gem.ruby = lambda: "/opt/homebrew/bin/ruby" + self.gem.gem = lambda: "/opt/homebrew/Cellar/ruby/4.0.7/bin/gem" + self.job = self.Job({"ruby": (0, self.GEMS), "outdated": (0, self.OUTDATED)}) + + def test_scan(self): + rows = {r["name"]: r for r in self.gem.scan(self.job)} + self.assertEqual(sorted(rows), ["json", "rake", "rubocop", "tidy"]) + self.assertEqual((rows["json"]["latest"], rows["rubocop"]["latest"], rows["tidy"]["latest"]), ("2.19.9", "1.81.2", None)) + self.assertTrue(rows["rubocop"]["outdated"] and not rows["tidy"]["outdated"]) + # Ruby's own gems: json has a known vulnerability so it counts; rake has none, so it's only `newer` + self.assertEqual(self.osv_asked, [["json", "rake"]]) # gems you installed aren't asked about + self.assertTrue(rows["json"]["outdated"]) + self.assertEqual((rows["rake"]["outdated"], rows["rake"].get("newer"), rows["rake"]["latest"]), (False, True, "13.4.2")) + self.assertIn("default gem", rows["json"]["note"]) + self.assertIn("also installed: 2.9.1", rows["json"]["note"]) + + def test_osv_unreachable_counts_rubys_own_gems_as_outdated(self): + def down(*a, **k): + raise OSError("no network") + server.http_json = down + rows = {r["name"]: r for r in self.gem.scan(self.job)} + self.assertTrue(rows["rake"]["outdated"] and not rows["rake"].get("newer")) + + def test_every_installed_version_is_checked(self): + self.assertIn(("json", "2.9.1", "json"), self.gem.dep_tree(self.job)) + + def test_ruby_that_fails_is_an_error_not_an_empty_list(self): + self.job.outputs["ruby"] = (1, "ruby: cannot load such file") + with self.assertRaises(RuntimeError): + self.gem.gems(self.job) + + def test_macos_ruby_is_left_alone(self): + orig = server.which + server.which = lambda name: "/usr/bin/ruby" + try: + self.assertFalse(server.Gem().available()) + finally: + server.which = orig + + def installs(self, only=None): + inv = {("gem", r["name"]): r for r in self.gem.scan(self.job)} + inv[("gem", "rubocop")]["latest"] = "1.81.2 --source=https://evil.example" + orig_inv, orig_gate = server.inv_pkgs, server.Manager.gate + server.inv_pkgs = lambda mid: inv + server.Manager.gate = lambda self, job: {} + try: + self.job.ran.clear() + self.gem.update(self.job, only) + finally: + server.inv_pkgs, server.Manager.gate = orig_inv, orig_gate + return [c[2:5] for c in self.job.ran if "install" in c] + + def test_update_installs_the_scanned_version(self): + self.assertEqual(self.installs(), [["json", "--version", "2.19.9"]]) # rake is only `newer`; rubocop's odd "version" is skipped + cmd = [c for c in self.job.ran if "install" in c][0] + self.assertEqual(cmd, ["/opt/homebrew/Cellar/ruby/4.0.7/bin/gem", "install", "json", "--version", "2.19.9", + "--conservative", "--no-document"]) + + def test_a_newer_gem_updates_when_asked_for_by_name(self): + self.assertEqual(self.installs(("gem", "rake")), [["rake", "--version", "13.4.2"]]) + + def test_newer_gems_still_wait_out_the_cooldown(self): + p = {"newer": True, "outdated": False, "latest": "13.4.2", "released": server.now() - 3600} + self.addCleanup(server.store["settings"].__setitem__, "cooldown_days", server.store["settings"].get("cooldown_days")) + server.store["settings"]["cooldown_days"] = 5 + self.assertGreater(server.cooldown_left(p), 0) + + +class DockerGems(unittest.TestCase): + def test_gems_in_an_image(self): + files = {"usr/local/bundle/specifications/nokogiri-1.16.0-x86_64-linux.gemspec": b"", + "usr/local/lib/ruby/gems/3.3.0/specifications/default/json-2.7.1.gemspec": b"", + "usr/local/bundle/specifications/net-http-0.4.1.gemspec": b"", + "usr/local/bundle/specifications/not a gemspec.gemspec": b""} + self.assertTrue(all(server.DOCKER_WANT.match(p) for p in files)) + self.assertFalse(server.DOCKER_WANT.match("app/mygem.gemspec")) + _, pkgs = server.image_packages(files) + self.assertEqual(pkgs, [("RubyGems", "json", "2.7.1"), ("RubyGems", "net-http", "0.4.1"), + ("RubyGems", "nokogiri", "1.16.0")]) class Migration(unittest.TestCase):