Documentation
¶
Overview ¶
Package versionmeta resolves installed-tool versions from on-disk metadata — npm package manifests, version-encoded install layouts (<tool>/versions/<v>, Homebrew Cellar/Caskroom), and macOS app bundles — so detectors can avoid launching third-party binaries.
Running `<tool> --version` is the detection path that popped macOS Gatekeeper "could not verify … free of malware" dialogs on customer machines: cursor-agent eagerly dlopens an un-notarized native addon (merkle-tree-napi.darwin-arm64.node) on any invocation. Metadata reads can't trigger that class of problem, so callers try FromBinary first and fall back to exec'ing the binary only when it returns "".
Index ¶
- func DpkgPackageVersion(exec executor.Executor, pkg string, paths ...string) string
- func FromBinary(ctx context.Context, exec executor.Executor, binaryPath string) string
- func IsVersionLike(s string) bool
- func NPMPackageName(exec executor.Executor, binaryPath string) string
- func NPMShimPackageRoot(exec executor.Executor, path string) string
- func NodeModulesPackageRoot(path string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DpkgPackageVersion ¶ added in v1.17.0
DpkgPackageVersion returns the installed version of the Debian package pkg when that package's own file manifest lists one of paths, "" otherwise. It is versionFromDpkg with the package name supplied by the caller instead of derived from the binary's basename, for ladders that already know which package they are proving (kiro-cli, whose binary and package share a name but whose PATH aliases `kiro` and `q` do not).
func FromBinary ¶
FromBinary returns the version of the tool installed at binaryPath, derived purely from filesystem metadata, or "" when no trustworthy source exists. It never executes the target binary; the only subprocess it may spawn is Apple's own PlistBuddy (an OS utility) to read an app-bundle Info.plist.
A "" result is not a failure — it means the caller should fall back to its existing version command. Sources are tried most- to least-authoritative:
- npm package manifest: binary resolves into node_modules/<pkg>/, and the manifest's package name matches the tool (rejects e.g. corepack's yarn shim, whose manifest version would be corepack's, not yarn's).
- <tool>/versions/<version>/ install layouts (cursor-agent, claude native installer). The directory holding "versions" must match the tool name so runtime-manager layouts (~/.pyenv/versions/3.12.1/bin/poetry) don't misattribute the runtime's version to the tool.
- Homebrew Cellar/Caskroom: physical containment in Cellar/<formula>/<v>/ means the formula owns the binary, so <v> is its version (npm-installed binaries under a brew node live in node_modules and are claimed or rejected by rule 1 before this can misfire).
- dpkg (Linux): the version of the package whose own file manifest lists this path. A binary dpkg still owns but that self-updated in place reports the packaged version; rules 1-3 claim the layouts where that happens before this is reached.
- snap (Linux): the `version` field of the snap manifest, which the path rules can't reach (/snap/bin/<name> symlinks to the snap wrapper).
- AppImage (Linux): the version in the filename, the only static source a single-file install has.
- macOS app bundle: CFBundleShortVersionString of the enclosing .app.
func IsVersionLike ¶ added in v1.17.0
IsVersionLike reports whether s looks like a version: optional "v", then a digit, at least one dot, and only [0-9A-Za-z.+_-] throughout. Rejects Caskroom "version,build" composites — callers fall back to exec for those.
func NPMPackageName ¶
NPMPackageName returns the full npm package name (e.g. "@github/copilot") that owns binaryPath, or "" when the binary doesn't resolve into an npm package. Lets detectors verify a tool's identity without executing it.
func NPMShimPackageRoot ¶
NPMShimPackageRoot reads a Windows-style npm shim (`<bin>.cmd`, `<bin>.ps1`, `<bin>.bat`) and returns the install root of the package the shim invokes, by locating the first `node_modules\<...>` reference in the shim body. Returns "" when path isn't a shim, can't be read, or contains no node_modules reference.
npm's Windows shim, generated by cmd-shim, looks like:
"%_prog%" "%dp0%\node_modules\@anthropic-ai\claude-code\cli.js" %*
We extract the node_modules path, resolve it relative to the shim's own directory (cmd-shim's `%dp0%`), and feed the absolute path through NodeModulesPackageRoot.
func NodeModulesPackageRoot ¶
NodeModulesPackageRoot walks up `path` looking for a `node_modules` directory; if found, returns the package root (one or two levels deeper depending on whether the package is scoped). Returns "" when the path isn't inside a node_modules tree.
Separator-agnostic: handles both `/` and `\` paths regardless of the host OS. The returned path uses the same separator style as the input — Windows paths preserve backslashes, Unix paths preserve forward slashes.
Examples:
/usr/local/lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe -> /usr/local/lib/node_modules/@anthropic-ai/claude-code C:\Users\u\AppData\Roaming\npm\node_modules\@scope\name\cli.js -> C:\Users\u\AppData\Roaming\npm\node_modules\@scope\name
Types ¶
This section is empty.