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

many: fix snap set and unset panics on empty strings #14958

Merged
merged 3 commits into from
Jan 24, 2025

Conversation

andrewphelpsj
Copy link
Member

@andrewphelpsj andrewphelpsj commented Jan 21, 2025

These cases were triggering panics:

snap set some-snap '!'
snap set some-snap '=value'
snap unset some-snap ''

These all end up in config.Transaction.Set, which calls config.PatchConfig. That function assumes that the parameters that are passed in are valid. In these given cases, the key we're either trying to set or unset is an empty string.

I added a check in config.Transaction.Set to hopefully prevent this kind of thing in the future, and I added explicit checks on the CLI side to prevent a trip to the daemon in the case that we have invalid parameters.

This was reported here: https://forum.snapcraft.io/t/bug-calling-unset-with-empty-string-explodes-snapd/44741

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (master@dc7d81f). Learn more about missing BASE report.
Report is 21 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #14958   +/-   ##
=========================================
  Coverage          ?   78.26%           
=========================================
  Files             ?     1160           
  Lines             ?   153488           
  Branches          ?        0           
=========================================
  Hits              ?   120133           
  Misses            ?    25950           
  Partials          ?     7405           
Flag Coverage Δ
unittests 78.26% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@olivercalder olivercalder left a comment

Choose a reason for hiding this comment

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

Looks good! Codecov isn't thrilled, could the new error messages be added to tests in client/clientutil as well? The consumers certainly test this well enough, though.

@andrewphelpsj
Copy link
Member Author

@olivercalder done, thanks!

Copy link

github-actions bot commented Jan 21, 2025

Fri Jan 24 17:00:57 UTC 2025
The following results are from: https://github.com/canonical/snapd/actions/runs/12909945646

Failures:

Preparing:

  • openstack:opensuse-tumbleweed-64
  • openstack:opensuse-tumbleweed-64
  • openstack:opensuse-tumbleweed-64
  • openstack:opensuse-tumbleweed-64

Restoring:

  • openstack:opensuse-tumbleweed-64
  • openstack:opensuse-tumbleweed-64
  • openstack:opensuse-tumbleweed-64

Copy link
Member

@olivercalder olivercalder left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@@ -52,6 +53,10 @@ func ParseConfigValues(confValues []string, opts *ParseConfigOptions) (map[strin
parts := strings.SplitN(patchValue, "=", 2)
if len(parts) == 1 && strings.HasSuffix(patchValue, "!") {
key := strings.TrimSuffix(patchValue, "!")
if key == "" {
return nil, nil, errors.New(i18n.G("configuration keys cannot be empty"))
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we could use a more specific error for ! input:

Suggested change
return nil, nil, errors.New(i18n.G("configuration keys cannot be empty"))
return nil, nil, errors.New(i18n.G("configuration keys cannot be empty (want key! to unset a key)"))

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, thanks!

Copy link
Member

@alfonsosanchezbeato alfonsosanchezbeato left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@andrewphelpsj andrewphelpsj merged commit 7adb9b7 into canonical:master Jan 24, 2025
62 of 63 checks passed
@andrewphelpsj andrewphelpsj deleted the fix-snap-unset-panic branch January 28, 2025 16:55
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.

5 participants