Repository navigation
[pkgdef 2/6] feat(pkgdef): add package definition library - #3001
mikeland73 wants to merge 14 commits into
Conversation
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]>
63a0202 to
e28622f
Compare
# Conflicts: # go.mod # go.sum # vendor-hash
[Medium impact] The PR appears safe to merge; the three numbered findings are fixed and no new blocking issue was found.
|
# Conflicts: # go.mod # go.sum # vendor-hash
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
|
@greptileai review Generated by Claude Code |
|
@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
|
@greptileai review Generated by Claude Code |
# Conflicts: # vendor-hash
Dismissed because a newer commit was pushed; Greptile will re-review the current head.
|
@greptileai review Generated by Claude Code |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code |
… 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
…/devbox into mikeland73/pkgdef-lib
Stack (merge in order):
internal/pkgdeflibrary 👈 this PRDEVBOX_FEATURE_PKG_DEFS)Summary
Second PR of the package definitions stack (spec:
specs/package-definitions.md, added in the previous PR). Addsinternal/pkgdef, a leaf package with no dependency ondevpkg,lockorplugin:Parse/Validatefor*.pkg.json(hujson), with every error reported at once.ParseRefforpkg:<ref>@<version>. The version is split at the last@, but only when that@comes after the last/.Client.Loadfor path,https://andfile://refs. Remote definitions are cached by content hash and can be pinned to a hash from the lockfile (ErrDefinitionChangedwhen the server now serves something else).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: resolvelatest, a prefix or an exact version, then render the artifact URL and fetch its sha256 (http_jsonorsha256_url, which handles bare digests andSHASUMS-style lists) for every supported system.Client.Install: idempotent install into$XDG_CACHE_HOME/devbox/pkgs/<name>/<version>/<system>-<key>.post_installhooks keep their output for error messages.binaryinstalls place the file atbin/<bin_name>.bin_namemust be a plain file name.managerandscriptstrategies returnErrNotImplementedfor 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.
s1 -> ., thens1/s2 -> .., thens1/s2/evilused to write outside the destination. Tar hard links are now linked instead of extracting as empty files.^[0-9A-Za-z][0-9A-Za-z.+_-]*$). Before,99+/../../xcould 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.https://only. They may not usefile://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.20resolves to the newest1.20.x. go.dev names 1.20.01.20, and an exact match used to win..downloads/<sha256(url)>-<sha256(install dir)>/, which is always removed afterwards.Review fixes
These address Greptile and Claude review comments on this PR.
bin_namemust be a plain file name. Validation rejects separators,.and.., including in overrides, and install checks it again. Before,../../victimcould create or truncate a file outside the install..devbox-pkgdef.jsonthat the archive ships is removed before the install is published, and the marker is removed again if apost_installhook fails.https://refuses any redirect to a non-https URL, so a network attacker cannot swap a definition, checksum or artifact.lockDirpolls a non-blockingflockand stops when the context is done.API:
InstallDir(def, version, system, artifact) (string, error)andIsInstalled(def, version, system, artifact).Test plan
go test ./internal/pkgdef/(with-race). Unit tests run against an httptest "vendor":file://rejection, keyed/immutable installs, per-install downloadsbin_namevalidation, archive-supplied markers, cancelled and unbounded jqgolangci-lintand 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