-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
// Copyright 2026 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: MIT
package pages
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"github.com/go-git/go-git/v5"
"github.com/pocka/legit/config"
"github.com/pocka/legit/core"
"github.com/pocka/legit/tests"
)
func FuzzPathParsing(f *testing.F) {
repos := f.TempDir()
secret, err := os.Create(filepath.Join(repos, "secret.txt"))
if err != nil {
f.Fatal(err)
}
defer secret.Close()
if _, err := secret.WriteString("pepperoni"); err != nil {
f.Fatal(err)
}
_, worktree, err := tests.CreateRepository(repos, "foo")
if err != nil {
f.Fatal(err)
}
readme, err := worktree.Filesystem.Create("README.md")
if err != nil {
f.Fatal(err)
}
if _, err := readme.Write([]byte("* iawsoiwjfngbhfg812uhjikwe6789asfd")); err != nil {
f.Fatal(err)
}
_ = readme.Close()
if _, err := worktree.Add("README.md"); err != nil {
f.Fatal(err)
}
_, err = worktree.Commit("Add README", &git.CommitOptions{
Author: tests.SignatureAlice(),
})
if err != nil {
f.Fatalf("Unable to commit: %s", err)
}
var c config.Config
c.Repo.ScanPath = repos
c.Repo.Readme = []string{"README.md"}
c.Repo.MainBranch = []string{"trunk"}
core, err := core.New(&c)
if err != nil {
f.Fatal(err)
}
handler := New(core)
f.Add("/")
f.Fuzz(func(t *testing.T, path string) {
target := "/" + path
url, err := url.Parse(target)
if err != nil {
// Skip non-GET-table URLs
t.SkipNow()
}
// httptest.NewRequest panics even on slight erroneous path
r, err := http.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
t.SkipNow()
}
w := httptest.NewRecorder()
// httptest.Server actually uses socket and if we run that, OS stops this
// test due to socket usage limit.
handler.ServeHTTP(w, r)
res := w.Result()
defer res.Body.Close()
// No InternalServerError
switch res.StatusCode {
case http.StatusOK:
case http.StatusNotFound:
default:
t.Errorf(
"Expected HTTP %d or %d, got %d",
http.StatusOK, http.StatusNotFound,
res.StatusCode,
)
}
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
body := string(b)
if strings.Contains(body, "pepperoni") {
t.Error("Returned body contains content of secret file under repos directory")
}
if strings.Index(target, "/static/") != 0 {
if !strings.Contains(body, "href=\"/static/noscript.css?r=") {
t.Errorf("Returned HTML does not use an embedded template")
}
}
})
}