| 1 | //go:build !windows |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestNormalizeLocalOpenPathPreservesUnixRoot(t *testing.T) { |
| 13 | got, err := normalizeLocalOpenPath("file:///tmp/reasonix-report.md") |
| 14 | if err != nil { |
| 15 | t.Fatalf("Unix file URL rejected: %v", err) |
| 16 | } |
| 17 | if got != "/tmp/reasonix-report.md" { |
| 18 | t.Fatalf("Unix file URL = %q, want rooted path", got) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func TestOpenLocalPathRejectsExecutableMode(t *testing.T) { |
| 23 | path := filepath.Join(t.TempDir(), "clicked-document") |
| 24 | if err := os.WriteFile(path, []byte("not safe to launch"), 0o755); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := (&App{}).OpenLocalPath(path); err == nil || !strings.Contains(err.Error(), "executable") { |
| 28 | t.Fatalf("OpenLocalPath(executable mode) err = %v, want executable refusal", err) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestOpenLocalPathRejectsSymlinkToApplicationBundle(t *testing.T) { |
| 33 | dir := t.TempDir() |
| 34 | bundle := filepath.Join(dir, "Unsafe.app") |
| 35 | if err := os.Mkdir(bundle, 0o755); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | alias := filepath.Join(dir, "innocent-folder") |
| 39 | if err := os.Symlink(bundle, alias); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | for _, path := range []string{alias, alias + string(filepath.Separator), alias + string(filepath.Separator) + "."} { |
| 43 | if err := (&App{}).OpenLocalPath(path); err == nil || !strings.Contains(err.Error(), "executable") { |
| 44 | t.Errorf("OpenLocalPath(application symlink %q) err = %v, want executable refusal", path, err) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestOpenTargetPathAllowsSymlinkToDocument(t *testing.T) { |
| 50 | dir := t.TempDir() |
| 51 | document := filepath.Join(dir, "report.md") |
| 52 | if err := os.WriteFile(document, []byte("report"), 0o644); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | alias := filepath.Join(dir, "report-link") |
| 56 | if err := os.Symlink(document, alias); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | info, err := os.Stat(alias) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if !openTargetPathAllowed(alias, info) { |
| 64 | t.Fatal("symlink to ordinary document should be openable") |
| 65 | } |
| 66 | } |
| 67 |