85 Commits

Author SHA1 Message Date
Tonis Tiigi
93999f4071 sourcepolicy: normalize parsed source identifiers
Render parsed source identifiers back to their canonical SourceOp form before
source policy evaluation. This lets Git subdir cleanup use the existing source
parser and avoids policy-specific Git parsing.

Add String methods for source identifiers and cover them with unit tests, plus
a client integration regression for canonical Git subdir policy matching.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-07-22 11:36:47 +02:00
Tonis Tiigi
fa1ba86389 source/http: fix resolver cache ref lookup
Use the resolver-cache ref ID directly when loading a cached HTTP snapshot.
This avoids dereferencing missing metadata for cache entries found during a
concurrent solve.

Reset the unauthorized checksum race request counter per solve iteration so
each retry applies the intended first-request delay.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-06-01 21:12:23 -07:00
Tonis Tiigi
e5cd9ab898 source/http: release resolver cache lock on errors
Move the HTTP snapshot resolver-cache lookup into a helper so the lock
release is deferred after acquisition. This keeps invalid cache entries
and digest mismatch errors from returning while the cache lock is held.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-05-28 17:46:00 -07:00
Tonis Tiigi
10872e1722 http: fix resolver cache nil race
Avoid storing typed nil HTTP metadata in the resolver cache after failed
metadata resolution. Also make resolver cache release functions consistently
callable when no error is returned.

Add a client integration test covering concurrent same-URL HTTP sources with
and without checksum when the server returns 401.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-05-20 12:48:07 -07:00
Bryce Gibson
74e64aa293 Ensure that a wrapped nil isn't returned as it causes a SIGSEGV.
Signed-off-by: Bryce Gibson <bgibson@limepoint.com>
2026-05-20 15:54:35 +10:00
Tonis Tiigi
5fde446dee solver: add compatibility-version support
Add solve-wide compatibility-version support for image and oci
exports, with historical goldens and release compatibility tests.

Backfill version 10 for v0.13-v0.14 git artifact behavior, keep
version 20 as current, and reject unsupported zstd on v10.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-04-10 09:24:11 -07:00
Tonis Tiigi
701cddc10b source/http: use os.Root for read-path file operations
Open the snapshot mount as an os.Root and perform file reads
through root-relative APIs in verifySignature and
computeChecksumResponse, consistent with the write path.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-03-25 08:14:30 -07:00
Tonis Tiigi
7cf63df2a7 source: extract SafeFileName into shared pathutil package
Move safeFileName from source/http to source/util/pathutil
and apply it to the containerblob source as well. Harden
containerblob/pull.go to use os.OpenRoot for file writes,
preventing path traversal via crafted filenames.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 3d6e587655d72c343f6fdc7268480a900ba45b0c)
2026-03-25 08:14:30 -07:00
Tonis Tiigi
be0dc4e898 source/http: use os.Root for saved file operations
Open the snapshot mount as an os.Root and perform file write/chown/chtimes
through root-relative APIs to keep operations constrained to the mount root.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit d568881c97278d87e4f6f01a1f8a67ad807152bb)
2026-03-25 08:14:30 -07:00
Tonis Tiigi
4f26c2023c source/http: sanitize downloaded filenames
Add safeFileName and route all getFileName sources through it.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 9d117af5ab1e1032f75658884384328fea440843)
2026-03-25 08:14:30 -07:00
Tonis Tiigi
e3fba18b63 source/http: add PGP verification and shared pgpsign
Add detached PGP verification for HTTP sources during metadata resolution
and expose LLB options/caps/attrs for signature validation.

Extract shared OpenPGP verification/parsing logic into util/pgpsign and
reuse it from git signing, plus add integration and source-level tests.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-02-20 08:13:38 -08:00
Tonis Tiigi
7f2cbc61ba http: add support for checksum requests for http sources
These requests allow computing additional checksum algorithms
for the payload of HTTP sources.

Optionally suffix can be passed that is added to payload. This
is needed to make validation of PGP signatures possible remotely.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-02-20 08:13:32 -08:00
Tonis Tiigi
a8b83196f3 ci: update golangci to v2.8.0
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2026-01-13 22:09:20 -08:00
Mikhail Dmitrichenko
739a8a766f fix metadata resp.Body leak
In (*httpSourceHandler).resolveMetadata method there is
a potential resp.Body leak in case when response status code is not
valid (< 200 or >= 400). Also resp.Body is not closed before exiting
if status code is okay.

This commit adds resp.Body closing before exiting from resolveMetadata
method.

Signed-off-by: Mikhail Dmitrichenko <m.dmitrichenko222@gmail.com>
2025-10-31 16:22:31 +03:00
Tonis Tiigi
df4d2aee3d solver: add ResolverCache support
New ResolverCache interface in JobContext allows build jobs to
memorize and synchronize accesses to mutable remote resources.

This is to make sure that when multiple parts of the same build
job, or build job and source metadata resolver access the same
remote resources, it remains the same for the duration of the
single build request, even if data happens to change on the remote side.

Fix such a possible case in the HTTP source. Even if the server
now returns completely different data, if the same URL was accessed
once for the ongoing build, then the initial contents are always
used until the build completes.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-10-17 10:29:55 -07:00
Tonis Tiigi
23a0dd7f7e solver: add jobCtx support to metadata resolver
Fixes issue where HTTP result can be released after metadata resolve
and rest of the build might pull new (potentially different) data in again.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-10-13 21:57:38 -07:00
Tonis Tiigi
80766cdd4b http: fix release race between cache and snapshot
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-10-13 19:56:53 -07:00
Tonis Tiigi
7bb92310aa solver: add cleanup function to jobctx and pass through sources
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-10-13 17:39:53 -07:00
Tonis Tiigi
b41ad2f121 http: add http support to ResolveSourceMeta
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-10-13 09:24:28 -07:00
Tonis Tiigi
7ed158dd2c http: avoid opaque hardcoded digests in tests
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit f38b1a135a7f03ef846a233a42704cc54b0adf9f)
2025-10-12 21:02:19 -07:00
Tonis Tiigi
58d2224960 cache: add cache debug plaintext for image manifest and http
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-08-12 12:10:17 +03:00
Tonis Tiigi
2f6fbdcfef http: allow authorization secrets per hostname
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-06-10 21:33:15 -07:00
Tonis Tiigi
69d3d44145 lint: add unconvert
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-05-02 12:44:01 -07:00
Tonis Tiigi
7e96e626e3 lint: add usestdlibvars
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2025-05-02 12:19:22 -07:00
Jonathan A. Sternberg
66016a8c63 vendor: switch from idtools to moby/sys/user
Convert usages of `github.com/docker/docker/pkg/idtools` to
`github.com/moby/sys/user` in order to break the dependency between
buildkit and docker.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
2025-04-02 10:22:02 -05:00
Dan Duvall
e19af64fe3 http: Support additional request headers
Support additional request header fields for HTTP sources, "Accept" and
"User-Agent" as a start.

The "Accept" header is required in some cases and some servers may vary
the response body based on the header value.

The "User-Agent" header may be useful in custom frontends and
potentially other cases.

 - llb: Add `llb.Header` and `HTTPInfo.Header` to allow `client/llb`
   users to set these header fields on HTTP sources. The argument to
   `llb.Header` is a struct to effectively limit header fields to a
   subset.
 - llb: Define and flag new `source.http.header` capability when
   `llb.Header` is used.
 - solver: Define new `http.header.` source attribute prefix. Giving
   each header field its own attribute (opposed to JSON encoding the
   header struct) will allow source policy to make assertions on
   individual header fields.
 - source/http: Parse `http.header.` attributes into a sorted slice and
   include them in cache key digest.
 - source/http: Set request headers accordingly.

Signed-off-by: Dan Duvall <dduvall@wikimedia.org>
2025-02-21 10:08:59 -08:00
Dan Duvall
ab1e99e19a http: Support authentication
Support authentication for HTTP sources.

 - llb: Define general `llb.AuthOption` interface composed of
   `HTTPOption` and `GitOption`.
 - llb: Refactor `llb.AuthHeaderSecret` to return an `llb.AuthOption` so
   it may be used with both `llb.Git` and `llb.HTTP`.
 - llb: Define `HTTPInfo.AuthHeaderSecret`.
 - llb: Define and flag new `source.http.auth` capability when
   `HTTPInfo.AuthHeaderSecret` is set.
 - solver: Define new `http.auth` source attribute.
 - source/http: If an `http.auth` attribute is specified, resolve a
   secret named by its value and set the "Authorization" request header.

Signed-off-by: Dan Duvall <dduvall@wikimedia.org>
2025-02-20 12:12:55 -08:00
Dan Duvall
2e60c1e440 http: Refactor http.Request construction into common method
Signed-off-by: Dan Duvall <dduvall@wikimedia.org>
2025-02-20 11:50:00 -08:00
Marat Radchenko
5be7edb69c Upgrade to containerd 2
Co-authored-by: Derek McGowan <derek@mcg.dev>
Signed-off-by: Marat Radchenko <marat@slonopotamus.org>
2025-01-13 16:42:48 -08:00
Tonis Tiigi
06ff84125d http: fix etag cache scoping
Currently the lookup from previous local etags was based on
filename. This leads to possibility where (misbehaving) server
may reuse the same eTag for different URLs. While using only
the filename might theoretically create more cache matches when
the same file is used via multiple URLs, I think was accidental
mistake and not intentional.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2024-11-25 09:40:01 -08:00
Tonis Tiigi
9f43ed7e77 llb: use buildkit user-agent for HTTP source
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2024-10-07 18:43:43 -07:00
Tonis Tiigi
3b35fc3207 http: avoid possible digest mismatch error
There is a possibility to get a digest mismatch error
if the metadata for previous download does not point to
a valid reference anymore.

To mitigate this, check that ref that etag points to
is still valid before using it.

Additionally `.cacheKey` property was not previously
set in the cases where old reference was reused. This
caused a case where even if the download needed to be
performed again, it always failed validation, even if
the digest had not actually changed since previous download.

There is still a small possibility that gc/prune request
will delete the downloaded record in between cachemap and
exec call and that the contents changes in the server
at that exact time. To fix that case we would need to
modify cachemap so that it can keep hold of references
until build is complete.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2024-10-04 17:18:09 -07:00
Justin Chadwell
ac7caa8f3b chore: use a better root for computing free disk space
We shouldn't use the cachemount root, we should actually properly
use the worker's specified root which is propagated from the config.

Signed-off-by: Justin Chadwell <me@jedevc.com>
2024-09-25 10:49:52 +01:00
Tonis Tiigi
610affa5fd exec: fix pruning cache mounts with parent ref on no-cache
On a build with no-cache, cache mounts were not pruned correctly
if the mount was on top of another ref. This also appeared in
Dockerfile when mode/uid/gid was set because implicit parent
ref is created in these cases in order to change the permissions
of a subdir that is used as a cache mount base.

Because it is not possible to know ahead of time what ref
will become the parent of cache mount during build, all cache
mounts matching the ID that have a parent will be pruned.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2024-09-05 18:09:54 -07:00
Tonis Tiigi
d7b3e02a55 lint: finish up testifylint
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2024-07-12 16:03:18 -07:00
Tonis Tiigi
4103099d94 ensure context.WithoutCancel in defer funcs
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2024-06-12 19:18:32 -07:00
CrazyMax
94e2370023 provenance: move types to a dedicated package
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
2024-02-25 20:39:50 +01:00
Justin Chadwell
98bdebc164 chore: remove unnecessary locker from http source
Signed-off-by: Justin Chadwell <me@jedevc.com>
2024-01-23 12:12:25 +00:00
Anthony Nandaa
18d40f05c8 tests: enable previously skipped tests
This enables 3 tests that had been previously skipped
due to the lack of Windows support initially.

The remaining ones in that group, see #4485, will need
some code modifications to be enabled.

Signed-off-by: Anthony Nandaa <profnandaa@gmail.com>
2023-12-18 01:31:12 -08:00
Chris Goller
40741dfbaf fix: return err when unable to hash
Signed-off-by: Chris Goller <goller@gmail.com>
2023-09-05 15:30:47 -05:00
Alex Suraci
6b27487fec source: make sources pluggable
Sources are a pretty neat extension point, except there are a few code
paths that hard-code against each type. This moves code around and
adjusts interfaces so that Source implementations are self-contained and
merely need to be registered with the source.Manager.

Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
2023-08-16 09:57:55 +01:00
Erik Sipsma
cf2698c0e4 Fix gzip decoding of HTTP sources.
The go http library will normally implicitly set an Accept-Encoding of
gzip to requests and then transparently decompress the response body.
However, if the Accept-Encoding header is explicitly set by the caller
it will no longer transparently decompress the body. This was causing
HTTP LLB sources to have unexpectedly compressed contents.

The fix here just unsets the Accept-Encoding header after the HEAD
request. Another possible fix would be to do our own gzip decompression
after reading the body if it was gzipped, but this approach seemed
slightly simpler.

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
2023-04-10 18:09:45 -07:00
Tonis Tiigi
f2f3d8cf94 http: ensure HEAD and GET requests have same headers
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2023-03-27 15:57:46 -07:00
CrazyMax
086f43da9b lint: fix issues with go 1.20
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-21 12:09:54 +01:00
Eng Zer Jun
be6501b654 test: use T.TempDir to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-07-24 02:27:26 +08:00
Sebastiaan van Stijn
21e9e9641e Remove uses of deprecated io/ioutil
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-10 00:57:07 +01:00
Kohei Tokunaga
3b7422996a cache: Clean up temporary mount pool on restart
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
2022-02-23 16:33:25 +09:00
Tonis Tiigi
4d0a3f9ff3 http: handle weak etags on comparisons
When server returns a weak etag, the prefix can
cause a mismatch on direct comparison with old value.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2022-02-13 21:21:38 -08:00
Erik Sipsma
bb09f3c032 Improve progress output for merge+diff ops.
Now, when a merge or diff ref is unlazied, the progress will show up
under the vertex for the merge/diff ref. Additionally, any ancestors of
the op that also need to be unlazied as part of unlazying the merge/diff
will show status updates under its vertex in the progress.

Signed-off-by: Erik Sipsma <erik@sipsma.dev>
2022-02-08 11:26:05 -08:00
Tonis Tiigi
dc21885891 hack: enable more linters
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2022-01-19 12:20:30 -08:00