Repository navigation
Description
What is the problem?
BinaryTarget.Parse (cmd/oras/internal/option/binary_target.go:66-67) builds the per-side DNS rules from the same base slice:
target.From.resolveFlag = append(target.resolveFlag, target.From.resolveFlag...)
target.To.resolveFlag = append(target.resolveFlag, target.To.resolveFlag...)pflag's StringArray grows target.resolveFlag with append, so after an odd number of --resolve values the slice usually has spare capacity. For example, 3 values give len 3, cap 4. Both append calls then write into the same backing array. The To append overwrites the element that the From append just placed at index 3, so From.resolveFlag silently ends with the --to-resolve rule.
Steps to reproduce
oras cp \
--resolve a:1:1.1.1.1 --resolve b:1:1.1.1.1 --resolve c:1:1.1.1.1 \
--from-resolve src.example:443:10.0.0.1 \
--to-resolve dst.example:443:10.0.0.2 \
src.example/repo:tag dst.example/repo:tag
The source side resolves using dst.example:443:10.0.0.2 instead of its own rule. The source host therefore falls back to normal DNS, or to a wrong address if the hosts overlap.
Proposed fix
target.From.resolveFlag = slices.Concat(target.resolveFlag, target.From.resolveFlag)
target.To.resolveFlag = slices.Concat(target.resolveFlag, target.To.resolveFlag)Add a unit test with 3 base --resolve values plus one --from-resolve and one --to-resolve, and assert that each side keeps its own rule.
oras-go v3 note
This is CLI-only code and is unchanged on the v3 migration branch (#2002).
Confirmed reproduction
Reproduced on main by parsing the flags through a real pflag.FlagSet (via BinaryTarget.ApplyFlags) and then running the two append lines from Parse:
base len=3 cap=4
From=[a:1:1.1.1.1 b:1:1.1.1.1 c:1:1.1.1.1 dst:443:10.0.0.2]
To=[a:1:1.1.1.1 b:1:1.1.1.1 c:1:1.1.1.1 dst:443:10.0.0.2]
From loses src:443:10.0.0.1 and picks up the --to-resolve rule instead.
The bug only triggers when --resolve is used with both --from-resolve and --to-resolve, and the base slice has spare capacity. Because append doubles capacity (1, 2, 4, 4, 8, …), 3, 5, 6, or 7 base values trigger it. 1, 2, or 4 values (where cap == len) happen not to.
Test notes
- The test has to build
resolveFlagthrough pflag, or withmake([]string, 3, 4). A slice literal hascap == len, which hides the bug. binary_target_test.gocurrently only coversModifyError. Nothing testsParseor the resolve-rule merge.- Also cover: zero base rules, only one side-specific rule, and a base count where
cap == len(e.g. 4). slices.Concatis available becausego.modtargets Go 1.26.
Activity