Skip to content

fix: reject a negative --concurrency level - #2351

Closed
qianeric-backup wants to merge 1 commit into
oras-project:mainfrom
qianeric-backup:fix/reject-negative-concurrency
Closed

qianeric-backup wants to merge 1 commit into
oras-project:mainfrom
qianeric-backup:fix/reject-negative-concurrency

Conversation

@qianeric-backup

Copy link
Copy Markdown

--concurrency is documented as concurrency level, and every command hands it straight to the copy graph, which treats a limit of zero or less as "no limit" (errgroup semantics). A negative value is therefore accepted and removes the limit instead of lowering it:

$ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
Pushed [oci-layout] /tmp/layout:test
Digest: sha256:...
$ echo $?
0

$ oras pull --oci-layout /tmp/layout:test -o /tmp/out --concurrency -2
Pulled [oci-layout] /tmp/layout:test
Digest: sha256:...
$ echo $?
0

That is the opposite of what someone writing a small number wants, and it is invisible: the push or pull succeeds, just with unbounded parallelism against the registry. oras discover already rejects the equivalent mistake for its own level flag:

$ oras discover --oci-layout /tmp/layout:test --depth -1
Error: depth value should be at least 1

Fix

The root command rejects a negative --concurrency before any command runs, so push, pull, cp, tag, attach, backup and restore are all covered:

$ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
Error: `--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)
$ echo $?
1
invocation before after
--concurrency -3 (push, pull, …) runs with no limit, exit 0 error, exit 1
--concurrency 0 no limit no limit
--concurrency 7 / default as before as before

The validation only runs when the flag was actually set, so the per-command defaults (3 or 5) are untouched.

Tests

Test_validateConcurrency covers an unused flag, a positive level, zero, and a negative level. It fails on the current tree with undefined: validateConcurrency.

go test ./cmd/oras/root/ and golangci-lint run ./cmd/oras/root/ are green.

If you would rather keep passing negative values through to the graph, the alternative is documenting 0 as "no limit" in the flag help and leaving the value unchecked — happy to switch to whatever you prefer.

`--concurrency` is documented as `concurrency level`, and every command hands it
straight to the copy graph, which treats a limit of zero or less as "no limit"
(errgroup semantics). A negative value is therefore accepted and removes the
limit instead of lowering it:

    $ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
    Pushed [oci-layout] /tmp/layout:test
    Digest: sha256:...
    $ echo $?
    0

    $ oras pull --oci-layout /tmp/layout:test -o /tmp/out --concurrency -2
    Pulled [oci-layout] /tmp/layout:test
    Digest: sha256:...
    $ echo $?
    0

That is the opposite of what someone writing a small number wants, and it is
invisible: the push or pull succeeds, just with unbounded parallelism against
the registry. `oras discover` already rejects the equivalent mistake for its own
level flag:

    $ oras discover --oci-layout /tmp/layout:test --depth -1
    Error: depth value should be at least 1

## Fix

The root command rejects a negative `--concurrency` before any command runs, so
push, pull, cp, tag, attach, backup and restore are all covered:

    $ oras push --oci-layout /tmp/layout:test --concurrency -3 f.txt:application/octet-stream
    Error: `--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)
    $ echo $?
    1

Unchanged: the default level, `--concurrency 0` (documented in the message as the
way to disable the limit) and any positive level.

| invocation | before | after |
|---|---|---|
| `--concurrency -3` (push, pull, ...) | runs with no limit, exit 0 | error, exit 1 |
| `--concurrency 0` | no limit | no limit |
| `--concurrency 7` / default | as before | as before |

## Tests

Test_validateConcurrency covers an unused flag, a positive level, zero and a
negative level; it fails on the current tree with `undefined:
validateConcurrency`. `go test ./cmd/oras/root/` and `golangci-lint run
./cmd/oras/root/` are green.

If you would rather keep passing negative values through to the graph, the
alternative is to document `0` as "no limit" in the flag help and leave the
value unchecked — say which you prefer.

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The DCO check is failing, because this commit is missing a Signed-off-by trailer:

  • 0ca2838 fix: reject a negative --concurrency level

https://github.com/oras-project/oras/runs/137096733155

To fix it, sign off the commit and force-push the branch:

git checkout fix/reject-negative-concurrency
git commit --amend --no-edit --signoff
git push --force-with-lease origin fix/reject-negative-concurrency

For future commits, use git commit -s.

Comment thread cmd/oras/root/cmd.go
return err
}
if level < 0 {
return fmt.Errorf("`--concurrency %d` is invalid: concurrency level must not be negative (use 0 for no limit)", level)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("`--concurrency %d` is invalid: concurrency level must not be negative (use 0 for no limit)", level)
return fmt.Errorf("`--concurrency %d` is invalid: concurrency level must not be negative", level)

oras-go copy/tag paths reset <= 0 to their default limit, so 0 is not "no limit".

Comment thread cmd/oras/root/cmd.go
Comment on lines +64 to +66
// validateConcurrency rejects a negative --concurrency level. The copy graph treats a limit of
// zero as "no limit" (errgroup semantics), and a negative limit is passed through to it just the
// same, so a negative value silently removes the limit instead of lowering it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// validateConcurrency rejects a negative --concurrency level. The copy graph treats a limit of
// zero as "no limit" (errgroup semantics), and a negative limit is passed through to it just the
// same, so a negative value silently removes the limit instead of lowering it.
// validateConcurrency rejects a negative --concurrency level. oras-go resets a limit <= 0 to its
// default, while internal/graph.FindPredecessors treats a non-positive limit as unbounded, so a
// negative value is never a meaningful level.

The comment's errgroup claim only holds for FindPredecessors, not the copy graph.

Comment thread cmd/oras/root/cmd_test.go
if err == nil {
t.Fatal("expected an error, got nil")
}
want := "`--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
want := "`--concurrency -3` is invalid: concurrency level must not be negative (use 0 for no limit)"
want := "`--concurrency -3` is invalid: concurrency level must not be negative"

Keeps the test in sync with the corrected error message.

Comment thread cmd/oras/root/cmd.go
cmd := &cobra.Command{
Use: "oras [command]",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A subcommand's own PersistentPreRunE would silently skip this; validate in each command's PreRunE or an option.Concurrency.Parse.

@TerryHowe

Copy link
Copy Markdown
Member

Thanks for reporting this. Closing because the PR has been inactive since review (DCO is still failing, and the review asked for validation per command instead of in the root PersistentPreRunE). I opened #2399 to track the bug. If you'd like to pick it up again along those lines, a new PR is welcome.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants