Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httputil: work around NPE in signer.Add() #1999

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

glebsts
Copy link

@glebsts glebsts commented Mar 5, 2024

If auth section is empty in config file, instance of Signer is initialized with default field values:
https://github.com/quay/clair/blob/v4.7.3/internal/httputil/signer.go#L29

Later e.g. in report command processing if configuration is used, method Add is used as "marks the authority in uri as one that expects signed requests":
https://github.com/quay/clair/blob/v4.7.3/cmd/clairctl/report.go#L145

In this method an entry is being added to use field of signer receiver no matter what. But if auth section was missing in config, use has default value of nil and produces assignment to entry in nil map.

This patch fixes internal logic, but I decided to keep URI parsing before actually checking for nil to give faster feedback to user if uri is e.g. malformed. Feel free to suggest change in such a flow.

Method Add is called from multiple commands, so I changed it inside the method, not pre-checking the calls.

Other Signer fields seem to be safe (ranging over nil map is ok).

There is no test file for signer, so I did not create a new test case I added one

@glebsts glebsts requested a review from a team as a code owner March 5, 2024 08:38
@glebsts glebsts requested review from crozzy and removed request for a team March 5, 2024 08:38
@app-sre-bot
Copy link
Collaborator

Can one of the admins verify this patch?

@glebsts glebsts changed the title work around NPE in signer.Add() httputil: work around NPE in signer.Add() Mar 6, 2024
Copy link
Member

@hdonnay hdonnay left a comment

Choose a reason for hiding this comment

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

Ah, good catch.

I'd prefer if the conditional were the other way, to keep it consistent with the other checks in that function. With that change, LGTM

@glebsts
Copy link
Author

glebsts commented Mar 7, 2024

@hdonnay Hello, could you please clarify about "conditionals being other way"?
Do you mean smth like

if s.use == nil {
  return errors.New("authority map not initialized, perhaps missing auth section in config")
}

?

@glebsts glebsts requested a review from hdonnay March 18, 2024 09:43
Copy link
Member

@hdonnay hdonnay left a comment

Choose a reason for hiding this comment

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

Sorry for letting this languish.

I think the test's error message is backwards.

With that changed, this just needs a rebase and sign-off (git commit -s).

rebase steps

Just adding a note here in case you're unfamiliar with a rebase workflow.
Conceptually, you're just taking your commits and applying the diff to the current main instead of where they were originally committed.
Assuming origin is the quay/claircore repo, and fork is your fork:

  1. git checkout BRANCH_NAME
  2. git fetch origin
  3. git rebase origin/main
  4. git push --force-with-lease fork

If you get a merge conflict in the rebase step, just replace the conflict block with the code you think should be there. If something's wrong, we'll catch it. If it's for an autogenerated file like go.sum, just recreate it.

If we asked you to change a commit message, add the -i flag to the rebase command and use the reword command.

t.Error("signer initialization with empty config should succeed")
}
if signer.use != nil {
t.Error("signed request authority map should be non-initialized")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
t.Error("signed request authority map should be non-initialized")
t.Error("signed request authority map should be initialized")

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.

3 participants