13 Commits

Author SHA1 Message Date
Oliver Calder
80289f5523 interfaces/utils: allow commas in filepaths (#12697)
* interfaces/utils: allow commas in filepaths

Some device paths contain commas outside of groups (i.e. {a,b}) or
classes (i.e. [,.:;'"]).  For example, `/dev/foo,bar` is a valid device
path which one might with to use with the custom-device interface.

Most filesystems allow commas in filepaths, as does apparmor:
https://gitlab.com/apparmor/apparmor/-/blob/master/parser/parser_regex.c#L340

Previously, createRegex() would throw an error if a comma was used
outside of a group or class.  This commit removes that error and instead
treats commas outside of groups and classes as literal commas.  The
accompanying tests are also adjusted to reflect this change.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces/utils: added argument to allow commas in filepaths

Rather than allowing any caller of `NewPathPattern()` to successfully
validate paths containing commas, this change adds a boolean argument
which explicitly specifies whether commas should be allowed in the
filepath.

There are some risks involved with allowing commas in filepaths (see
discussion at https://github.com/snapcore/snapd/pull/12697), so it is
desirable to restrict when commas are allowed based on the caller.  In
particular, superprivileged interfaces (such as `custom-device` and
`mount-control`) have valid needs for commas in filepaths, and users of
these interfaces are individually verified, so it is safe for them to
use `NewPathPattern()` with commas allowed.  Other callers (particularly
unprivileged interfaces) should probably not allow commas.

I was unsure whether `overlord/hookstate/ctlcmd/mount.go` should call
`NewPathPattern()` with commas allowed or not, but since commas had
previously been disallowed and tests continue to pass with
`allowCommas=false`, then I decided to leave it as `false`.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces/{builtin,utils}: added named variables for allowCommas

Also, switched `overlord/hookstate/ctlcmd/mount.go` to allow commas
(previously did not, but this should match what is allowed in
`interfaces/builtin/mount_control.go`.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces/utils: added unit tests for commas in paths

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces/utils: remove `QuoteMeta` when adding `","` to path regex

Since `,` is not a regex special character, the `QuoteMeta` call is
unnecessary.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* interfaces/utils: renamed TestCommasInRegex to TestCreateRegexWithCommas

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

* many: added unit tests for callers of NewPathPattern with allowCommas=true

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>

---------

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
Co-authored-by: Michael Vogt <mvo@ubuntu.com>
2023-05-19 21:49:40 +02:00
Alberto Mardegan
2b2b0046bd interfaces/utils: introduce new PathPattern class 2021-12-08 09:48:58 +03:00
Pawel Stolowski
4121d58bbb interfaces: fix decoding of json numbers for static/dynamic attributes
* ifstate: fix decoding of json numbers

When we read the static slots/plugs from the state we decode them
as with the default json decoding. This leads to float64 instead
of the expected int. This PR fixes it and adds tests.

It also adds support in NormalizeInterfaceAttributes for json.Number.
2018-10-22 20:36:27 -06:00
Zygmunt Krynicki
4144a1322e interfaces: fix NormalizeInterfaceAttributes, add tests (#6012)
* interfaces: fix NormalizeInterfaceAttributes, add tests

The NormalizeInterfaceAttributes was untested so I fixed that. While
working on tests I noticed that it was unexpectedly mutating the
argument it was given (while returning a return value) so I fixed and
tested that as well.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

* interfaces: test normalization of float32

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

* interfaces: add tests for CopyAttributes

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2018-10-18 09:17:02 +02:00
Pawel Stolowski
00276aa3a4 Reverted the addition of ValidateName. 2018-07-05 12:16:49 +02:00
Pawel Stolowski
7ceb6035a2 Merge branch 'master' into hotplug-device-info 2018-07-04 10:42:39 +02:00
Zygmunt Krynicki
d77035bdf2 snap,interfaces: move interface name validation to snap
This patch moves the helper that validates plug, slot and interface
names to the snap package (alongside other validation helpers).

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2018-07-02 23:15:51 +02:00
Pawel Stolowski
e3adc772fb Merge branch 'master' into hotplug-device-info 2018-06-29 12:23:55 +02:00
Pawel Stolowski
f0d383851c Move tests as well. 2018-06-28 18:14:30 +02:00
Pawel Stolowski
29498b64dd Move ValidateName to utils. 2018-06-28 18:08:54 +02:00
Pawel Stolowski
63b33d07b2 Updated HotPlugSpec. 2018-06-28 13:10:12 +02:00
Pawel Stolowski
0e7a133eaf Review comments. 2018-06-28 09:47:57 +02:00
Pawel Stolowski
8a8d8a3783 Moved normalize method to interfaces/utils and made it public. 2018-06-27 12:56:59 +02:00