Skip to content

Commit

Permalink
desktop/desktopentry: add more tests for file URI checks, and special…
Browse files Browse the repository at this point in the history
… characters within file paths
  • Loading branch information
jhenstridge authored and mvo5 committed Sep 6, 2023
1 parent dfece4e commit dea4dea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions desktop/desktopentry/expand_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bytes"
"fmt"
"net/url"
"path/filepath"
"strings"

"github.com/snapcore/snapd/strutil/shlex"
Expand All @@ -43,6 +44,9 @@ func toFilePath(uri string) (string, error) {
if u.Scheme != "file" {
return "", fmt.Errorf("%q is not a file URI", uri)
}
if !filepath.IsAbs(u.Path) {
return "", fmt.Errorf("%q does not have an absolute file path", uri)
}
return u.Path, nil
}

Expand Down
14 changes: 14 additions & 0 deletions desktop/desktopentry/expand_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ func (s *desktopentrySuite) TestExpandExecHelper(c *C) {
in: `foo %f`,
uris: []string{"foo/bar"},
err: `"foo/bar" is not an absolute URI`,
}, {
in: `foo %f`,
uris: []string{"file:foo/bar"},
err: `"file:foo/bar" does not have an absolute file path`,
}, {
// Environment variables are not expanded.
in: `foo $PATH`,
out: []string{"foo", "$PATH"},
}, {
// Special characters within URIs are preserved in the
// final command line.
in: `foo %f`,
uris: []string{"file:///special%20chars%27%22%25%20%24foo"},
out: []string{"foo", `/special chars'"% $foo`},
}} {
c.Logf("tc %d", i)

Expand Down

0 comments on commit dea4dea

Please sign in to comment.