Permalink
Browse files

Rewrite the Exec= line to contain an absolute path

  • Loading branch information...
1 parent e8f6208 commit f4357d1f79616a1f03746539d81dc01791c89e95 @mvo5 mvo5 committed Mar 3, 2016
Showing with 37 additions and 10 deletions.
  1. +15 −9 snappy/desktop.go
  2. +22 −1 snappy/desktop_test.go
View
@@ -77,21 +77,21 @@ func isValidDesktopFilePrefix(line string) bool {
return false
}
-func badExecLine(m *snapYaml, line string) bool {
- if !strings.HasPrefix(line, "Exec=") {
- return false
- }
+func rewriteExecLine(m *snapYaml, line string) (string, error) {
cmd := strings.SplitN(line, "=", 2)[1]
for _, app := range m.Apps {
validCmd := filepath.Base(generateBinaryName(m, app))
- // just check the prefix to allow %flag style args
+ // check the prefix to allow %flag style args
// this is ok because desktop files are not run through sh
+ // so we don't have to worry about the arguments too much
if cmd == validCmd || strings.HasPrefix(cmd, validCmd+" ") {
- return false
+ binDir := stripGlobalRootDir(dirs.SnapBinariesDir)
+ absoluteCmd := filepath.Join(binDir, cmd)
+ return strings.Replace(line, cmd, absoluteCmd, 1), nil
}
}
- return true
+ return "", fmt.Errorf("invalid exec command: %q", cmd)
}
func sanitizeDesktopFile(m *snapYaml, realBaseDir string, rawcontent []byte) []byte {
@@ -104,8 +104,14 @@ func sanitizeDesktopFile(m *snapYaml, realBaseDir string, rawcontent []byte) []b
if !isValidDesktopFilePrefix(line) {
continue
}
- if badExecLine(m, line) {
- continue
+ // rewrite exec lines to an absolute path for the binary
+ if strings.HasPrefix(line, "Exec=") {
+ var err error
+ line, err = rewriteExecLine(m, line)
+ if err != nil {
+ // something went wrong, ignore the line
+ continue
+ }
}
// do variable substitution
View
@@ -179,7 +179,7 @@ Exec=snap.app %U
e := sanitizeDesktopFile(m, "/my/basedir", desktopContent)
c.Assert(string(e), Equals, `[Desktop Entry]
Name=foo
-Exec=snap.app %U`)
+Exec=/snaps/bin/snap.app %U`)
}
// we do not support TryExec (even if its a valid line), this test ensures
@@ -219,3 +219,24 @@ Name=foo
GenericName=bar
GenericName[de]=einsehrlangeszusammengesetzteswort`)
}
+
+func (s *SnapTestSuite) TestDesktopFileRewriteExecLineInvalid(c *C) {
+ m := &snapYaml{}
+ _, err := rewriteExecLine(m, "Exec=invalid")
+ c.Assert(err, ErrorMatches, `invalid exec command: "invalid"`)
+}
+
+func (s *SnapTestSuite) TestDesktopFileRewriteExecLineOk(c *C) {
+ m, err := parseSnapYamlData([]byte(`
+name: snap
+version: 1.0
+apps:
+ app:
+ command: cmd
+`), false)
+ c.Assert(err, IsNil)
+
+ newl, err := rewriteExecLine(m, "Exec=snap.app")
+ c.Assert(err, IsNil)
+ c.Assert(newl, Equals, "Exec=/snaps/bin/snap.app")
+}

0 comments on commit f4357d1

Please sign in to comment.