Skip to content

Commit 055ff21

Browse files
committed
PR feedback
After discussing this with @babakks, we consolidated the tests further and improved the documentation around why we do not resolve symlinks when checking for files on the filesystem.
1 parent 258949b commit 055ff21

File tree

2 files changed

+11
-91
lines changed

2 files changed

+11
-91
lines changed

pkg/browser/browser.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func isPossibleProtocol(u string) error {
105105
// Disallow URLs that match existing files or directories on the filesystem
106106
// as these could be executables or executed by the launcher browser due to
107107
// the file extension and/or associated application.
108+
//
109+
// Symlinks should not be resolved in order to avoid broken links or other
110+
// vulnerabilities trying to resolve them.
108111
if fileInfo, _ := os.Lstat(u); fileInfo != nil {
109112
return fmt.Errorf("opening files or directories is unsupported: %s", u)
110113
}

pkg/browser/browser_test.go

Lines changed: 8 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func TestBrowse(t *testing.T) {
6565
launcher: fmt.Sprintf("%q -test.run=TestHelperProcess -- implicit https", os.Args[0]),
6666
expected: "[implicit https github.com]",
6767
},
68+
{
69+
name: "Explicit absolute `file://` URL errors",
70+
url: "file:///System/Applications/Calculator.app",
71+
wantErr: true,
72+
},
6873
}
6974

7075
// Setup additional test scenarios covering OS-specific executables and directories
@@ -83,95 +88,7 @@ func TestBrowse(t *testing.T) {
8388
wantErr: true,
8489
},
8590
}...)
86-
case "darwin":
87-
tests = append(tests, []browseTest{
88-
{
89-
name: "Implicit absolute Mac OS file URL errors",
90-
url: "/bin/bash",
91-
wantErr: true,
92-
},
93-
{
94-
name: "Implicit absolute Mac OS directory URL errors",
95-
url: "/bin",
96-
wantErr: true,
97-
},
98-
{
99-
name: "Explicit absolute Mac OS `file://` URL errors",
100-
url: "file:///System/Applications/Calculator.app",
101-
wantErr: true,
102-
},
103-
{
104-
name: "Implicit relative file URL errors",
105-
url: "poc.command",
106-
setup: func(t *testing.T) error {
107-
// Setup a temporary directory to stage content and execute the test within,
108-
// ensure the test's original working directory is restored after.
109-
cwd, err := os.Getwd()
110-
if err != nil {
111-
return err
112-
}
113-
114-
tempDir := t.TempDir()
115-
err = os.Chdir(tempDir)
116-
if err != nil {
117-
return err
118-
}
119-
120-
t.Cleanup(func() {
121-
_ = os.Chdir(cwd)
122-
})
123-
124-
// Create content for local file URL testing
125-
path := filepath.Join(tempDir, "poc.command")
126-
err = os.WriteFile(path, []byte("#!/bin/bash\necho hello"), 0755)
127-
if err != nil {
128-
return err
129-
}
130-
131-
return nil
132-
},
133-
wantErr: true,
134-
},
135-
{
136-
name: "Implicit relative directory URL errors",
137-
url: "Fake.app",
138-
setup: func(t *testing.T) error {
139-
// Setup a temporary directory to stage content and execute the test within,
140-
// ensure the test's original working directory is restored after.
141-
cwd, err := os.Getwd()
142-
if err != nil {
143-
return err
144-
}
145-
146-
tempDir := t.TempDir()
147-
err = os.Chdir(tempDir)
148-
if err != nil {
149-
return err
150-
}
151-
152-
t.Cleanup(func() {
153-
_ = os.Chdir(cwd)
154-
})
155-
156-
// Create content for local directory URL testing
157-
path := filepath.Join(tempDir, "Fake.app")
158-
err = os.Mkdir(path, 0755)
159-
if err != nil {
160-
return err
161-
}
162-
163-
path = filepath.Join(path, "poc.command")
164-
err = os.WriteFile(path, []byte("#!/bin/bash\necho hello"), 0755)
165-
if err != nil {
166-
return err
167-
}
168-
169-
return nil
170-
},
171-
wantErr: true,
172-
},
173-
}...)
174-
// Default should handle common Unix/Linux scenarios outside of Mac OS.
91+
// Default should handle common Unix/Linux scenarios including Mac OS.
17592
default:
17693
tests = append(tests, []browseTest{
17794
{
@@ -185,7 +102,7 @@ func TestBrowse(t *testing.T) {
185102
wantErr: true,
186103
},
187104
{
188-
name: "Implicit relative file URL errors",
105+
name: "Implicit relative Unix/Linux file URL errors",
189106
url: "poc.command",
190107
setup: func(t *testing.T) error {
191108
// Setup a temporary directory to stage content and execute the test within,
@@ -217,7 +134,7 @@ func TestBrowse(t *testing.T) {
217134
wantErr: true,
218135
},
219136
{
220-
name: "Implicit relative directory URL errors",
137+
name: "Implicit relative Unix/Linux directory URL errors",
221138
url: "Fake.app",
222139
setup: func(t *testing.T) error {
223140
// Setup a temporary directory to stage content and execute the test within,

0 commit comments

Comments
 (0)