Skip to content

[pkgdef 2/6] feat(pkgdef): add package definition library - #3001

Open
mikeland73 wants to merge 14 commits into
mikeland73/pkgdef-specfrom
mikeland73/pkgdef-lib
Open

mikeland73 wants to merge 14 commits into
mikeland73/pkgdef-specfrom
mikeland73/pkgdef-lib

Conversation

@mikeland73

@mikeland73 mikeland73 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Stack (merge in order):

  1. [pkgdef 1/6] docs(specs): add package definition spec and Go example #3000 — spec + Go example
  2. [pkgdef 2/6] feat(pkgdef): add package definition library #3001 — internal/pkgdef library 👈 this PR
  3. [pkgdef 3/6] feat(pkgdef): wire package definitions into add, lock, install and env #3002 — wiring into add/lock/install/env (behind DEVBOX_FEATURE_PKG_DEFS)
  4. [pkgdef 4/6] feat(pkgs): add the initial set of package definitions #3034 — initial package set (15 definitions)
  5. [pkgdef 5/6] feat(pkgdef): support the initial package set #3035 — format features for the initial package set
  6. [pkgdef 6/6] test(pkgs): install and switch versions of every shipped package #3036 — per-package install/switch tests + CI

Summary

Second PR of the package definitions stack (spec: specs/package-definitions.md, added in the previous PR). Adds internal/pkgdef, a leaf package with no dependency on devpkg, lock or plugin:

  • Parse/Validate for *.pkg.json (hujson), with every error reported at once.
  • ParseRef for pkg:<ref>@<version>. The version is split at the last @, but only when that @ comes after the last /.
  • Client.Load for path, https:// and file:// refs. Remote definitions are cached by content hash and can be pinned to a hash from the lockfile (ErrDefinitionChanged when the server now serves something else).
  • Version sources: static, http_json (jq via gojq), github_releases (drafts and pre-releases skipped, token only ever sent to the GitHub API host).
  • Client.Resolve/Lock: resolve latest, a prefix or an exact version, then render the artifact URL and fetch its sha256 (http_json or sha256_url, which handles bare digests and SHASUMS-style lists) for every supported system.
  • Client.Install: idempotent install into $XDG_CACHE_HOME/devbox/pkgs/<name>/<version>/<system>-<key>.
    • Installs are serialized across processes with a file lock and verified against the lockfile digest.
    • Archives are extracted with path and symlink sanitization (tar.{gz,xz,bz2,zst} and zip via mholt/archives, which the repo already uses).
    • post_install hooks keep their output for error messages.
    • binary installs place the file at bin/<bin_name>. bin_name must be a plain file name.
  • manager and script strategies return ErrNotImplemented for now, as the spec marks them planned.

New direct dependency: github.com/itchyny/gojq (vendor-hash refreshed). The spec's template section now also says which argument order the template functions use (string last, as in sprig).

Audit fixes

These come from the cross-stack audit recorded in the spec.

  • Extraction stays inside the destination, even through symlinks planted by earlier entries. For example, s1 -> ., then s1/s2 -> .., then s1/s2/evil used to write outside the destination. Tar hard links are now linked instead of extracting as empty files.
  • Vendor version strings are validated (^[0-9A-Za-z][0-9A-Za-z.+_-]*$). Before, 99+/../../x could move the install dir out of the cache, and a version could inject shell into hooks. Install also checks that the install dir is inside the cache before removing anything.
  • Remote definitions are https:// only. They may not use file:// URLs anywhere, which also covers URLs built by templates. Before, a remote definition could read a local file and send it to its own host.
  • @1.20 resolves to the newest 1.20.x. go.dev names 1.20.0 1.20, and an exact match used to win.
  • Content-keyed, immutable install dirs, which addresses Greptile's comment on [pkgdef 1/6] docs(specs): add package definition spec and Go example #3000.
    • The key covers the fingerprint, the system and the URL. Two projects share an install only when they would install the same artifact the same way. A finished install is never deleted or replaced.
    • The digest is left out of the key, so recording it on first use doesn't move the dir.
    • Each install downloads into its own .downloads/<sha256(url)>-<sha256(install dir)>/, which is always removed afterwards.
    • An existing install is reused, and its digest recorded on first use, only when its URL matches the locked URL.

Review fixes

These address Greptile and Claude review comments on this PR.

  • bin_name must be a plain file name. Validation rejects separators, . and .., including in overrides, and install checks it again. Before, ../../victim could create or truncate a file outside the install.
  • A failed install can no longer look complete. A .devbox-pkgdef.json that the archive ships is removed before the install is published, and the marker is removed again if a post_install hook fails.
  • Downloads are keyed by the whole install dir. The install key leaves out the version, so two versions with a fixed URL used to share a download dir under different locks, and one install could delete the other's download.
  • jq evaluation follows the caller's context and fails after 100,000 results.
  • No https downgrades. A request that starts on https:// refuses any redirect to a non-https URL, so a network attacker cannot swap a definition, checksum or artifact.
  • Metadata reads are capped at 64 MiB (definitions, version lists, checksums). Artifacts are streamed to disk as before.
  • Waiting for an install lock honors cancellation. lockDir polls a non-blocking flock and stops when the context is done.

API: InstallDir(def, version, system, artifact) (string, error) and IsInstalled(def, version, system, artifact).

Test plan

  • go test ./internal/pkgdef/ (with -race). Unit tests run against an httptest "vendor":
    • resolve, lock, archive and binary installs, concurrent installs
    • checksum mismatch, trust on first use, GitHub releases, remote pinning
    • extraction safety, including chained symlinks and hard links
    • version validation, remote file:// rejection, keyed/immutable installs, per-install downloads
    • bin_name validation, archive-supplied markers, cancelled and unbounded jq
    • https-to-http redirects, oversized responses, cancelled lock waits
  • golangci-lint and gofumpt clean (gofmt only for the review-fix commit; the local golangci-lint is built with an older Go than the module needs)

https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH


Generated by Claude Code

Add internal/pkgdef, the library behind package definitions (see
specs/package-definitions.md): parsing and validation of *.pkg.json
files, pkg:<ref>@<version> reference parsing, loading local and remote
definitions (remote ones cached and pinned by content hash), version
resolution from static lists, JSON indexes (via gojq) and GitHub
releases, per-system artifact locking with checksums, and idempotent
installs into $XDG_CACHE_HOME/devbox/pkgs/<name>/<version>/<system>.

The package is a leaf with no dependency on devpkg, lock or plugin so
the wiring in a follow-up PR can build on it. The manager and script
strategies defined by the spec return ErrNotImplemented for now.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mikeland73
mikeland73 force-pushed the mikeland73/pkgdef-lib branch from 63a0202 to e28622f Compare October 26, 2026 00:57
# Conflicts:
#	go.mod
#	go.sum
#	vendor-hash
Comment thread internal/pkgdef/extract.go Resolved
Comment thread internal/pkgdef/extract.go Resolved
@mikeland73 mikeland73 closed this Nov 17, 2026
@mikeland73 mikeland73 reopened this Nov 17, 2026
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the three numbered findings are fixed and no new blocking issue was found.

Summary

Adds internal/pkgdef to load, validate, resolve, lock, and install package definitions.

This update addresses all three numbered findings from the previous review:

  • Package definitions describe how a tool is found and installed.
  • Version requests resolve to a concrete release from the definition's source.
  • The client loads definitions and metadata from local files or HTTPS sources.
  • Resolved versions get a locked artifact URL and checksum for each supported system.
  • Archive and binary packages install into keyed, reusable cache directories.

No new actionable issues were found in the changes since that review. Earlier fixes for archive links, binary names, version paths, completion markers, jq cancellation, and separate download directories remain present. Tests were inspected, not run.

Reviews (6) · Last reviewed commit: "Merge branch 'mikeland73/pkgdef-lib' of ..." · Reviewed by Greptile

Comment thread internal/pkgdef/extract.go Resolved
Comment thread internal/pkgdef/install.go Resolved
Comment thread internal/pkgdef/client.go Resolved
Comment thread internal/pkgdef/install.go Resolved
Comment thread internal/pkgdef/client.go Resolved
# Conflicts:
#	go.mod
#	go.sum
#	vendor-hash
Comment thread internal/pkgdef/extract.go Resolved
Extraction checked symlink targets only as strings, and MkdirAll/OpenFile
then followed links already on disk, so a tar with `s1 -> .`,
`s1/s2 -> ..` and `s1/s2/evil` wrote outside the destination.

Every entry's parent directory is now resolved on disk and must stay
inside the destination. Symlink targets may only climb with leading
`..` components, measured from the link's real parent, so every link in
the tree resolves inside it regardless of entry order. Files are created
with O_EXCL after removing whatever non-directory was there.

Tar hard links (TypeLink) used to extract as empty files. They now link
to (or copy) the earlier, strip_components-adjusted entry, which must be
a regular file inside the destination.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH
…nstalls

Versions: version sources drop versions that do not match
^[0-9A-Za-z][0-9A-Za-z.+_-]*$ (they become path components and template
input), and ParseRef, MatchVersion, InstallDir and Install reject them.
Install also checks that the install dir is under the cache before it
removes anything.

MatchVersion treats an exact match as one candidate among the prefix
matches and picks the highest, so @1.20 against go.dev resolves to
1.20.14 rather than 1.20.

ParseRef splits off the version only when the last "@" comes after the
last "/", so pkg:https://cdn/npm/@acme/tool.pkg.json has no version.

Remote definitions must be https://; http:// refs are rejected. A remote
definition may not use file:// URLs (absolute or relative) for versions,
install or checksum URLs, checked at load and again after rendering.

Installs go to <cache>/<name>/<version>/<system>-<key>, where key is the
first 12 hex digits of sha256 over the install fingerprint, system,
artifact URL and sha256, so different definitions or artifacts never
share or replace each other's installs. A completed install is never
deleted, and a marker whose URL differs from the locked URL is rejected,
so trust on first use can only record a digest observed for its own URL.
Downloads are keyed by sha256(url) and always removed after unpacking.

API: InstallDir(def, version, system, artifact) (string, error) and
IsInstalled(def, version, system, artifact); Installed gains URL; new
Definition.InstallFingerprint, IsValidVersion and
ErrFileURLInRemoteDefinition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH
Downloads were keyed only by URL, so two installs of the same URL under
different install keys (which take different locks) could delete each
other's archive mid-install. The download dir is now
.downloads/<sha256(url)[:12]>-<install key>, used only under that
install's lock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH
Trust on first use records the digest after installing. With the digest
in the key, the recorded lock entry pointed at a new, empty directory in
the same run (PATH used the old one, InstallDir the new one). A locked
digest that differs from the install's is still a checksum error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92oAnwydcLrbSE7nkVH

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

Comment thread internal/pkgdef/install.go Resolved
Comment thread internal/pkgdef/install.go Resolved
Comment thread internal/pkgdef/install.go Resolved

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

…and jq

- Reject install.bin_name values that are not a plain file name (in
  validation and again before placing the binary), so a definition cannot
  write outside the staging bin directory.
- Remove any .devbox-pkgdef.json the archive supplies before publishing the
  install, and remove the marker again when a post_install hook fails, so a
  failed install is never taken as complete.
- Key the download directory by the full install directory, so two
  versions sharing a fixed URL (same install key) never remove each other's
  download.
- Run jq with the caller's context and cap the number of results.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92n3Hivd38RfT8QezDK

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

greptile-apps[bot]
greptile-apps Bot previously approved these changes Nov 23, 2026
@greptile-apps
greptile-apps Bot dismissed their stale review November 23, 2026 21:59

Dismissed because a newer commit was pushed; Greptile will re-review the current head.

Copy link
Copy Markdown
Collaborator Author

@greptileai review


Generated by Claude Code

@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

Comment thread internal/pkgdef/client.go Resolved
Comment thread internal/pkgdef/client.go Resolved
Comment thread internal/pkgdef/install.go Resolved
… waits

- Reject redirects that leave https once a request started there, so a
  network attacker cannot swap a definition, version list, checksum or
  artifact. A caller's CheckRedirect still applies.
- Read definitions, version lists and checksums with a 64 MiB cap instead
  of io.ReadAll without a limit.
- lockDir takes the install context and polls a non-blocking flock, so a
  cancelled Install stops waiting for a lock held by another process.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Bqy92n3Hivd38RfT8QezDK

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants