~gitpocket/gitpocket

4
4b5e48eebab4ce62f9be9b6ab232cb40a4f113b1
mainfeed-discovery
gitpocket/forge/unit_test.go -rw-r--r--6.3 KiB ViewLogBlameView rawPermalink 3276b11928 feat(import): import repositories over https and claim matching commit authors 5 hours ago
  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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package forge

import (
	"net"
	"net/http/httptest"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"testing"
)

func TestValidBranch(t *testing.T) {
	for _, ok := range []string{"main", "feature/x", "v1.2", "a_b-c"} {
		if !validBranch(ok) {
			t.Errorf("%q rejected", ok)
		}
	}
	for _, bad := range []string{"", "-x", "a..b", "a b", "a:b", "a~1", "a^", "x.lock", "a//b", "/a", "a/", ".a", "a/.b", "a@{1}", "@", "a\nb", "a\x00b", "a*", "a?", "a[", "a\\b"} {
		if validBranch(bad) {
			t.Errorf("%q accepted", bad)
		}
	}
}

func TestCleanPath(t *testing.T) {
	cases := map[string]bool{"a/b.txt": true, "": true, "../x": false, "a/../b": false, "-rf": false, "a\nb": false, "a\x00": false, "a\tb": false, "a//b": false}
	for in, want := range cases {
		if _, got := cleanPath(in); got != want {
			t.Errorf("cleanPath(%q) = %v", in, got)
		}
	}
}

func TestClientIP(t *testing.T) {
	r := httptest.NewRequest("GET", "/", nil)
	r.RemoteAddr = "203.0.113.9:4000"
	r.Header.Set("X-Forwarded-For", "1.1.1.1")
	if got := clientIP(r); got != "203.0.113.9" {
		t.Errorf("direct client spoofed its address as %s", got)
	}
	r.RemoteAddr = "127.0.0.1:4000"
	r.Header.Set("X-Forwarded-For", "6.6.6.6, 198.51.100.7")
	if got := clientIP(r); got != "198.51.100.7" {
		t.Errorf("proxied address %s, want the last hop", got)
	}
}

func TestParseQuery(t *testing.T) {
	text, quals := parseQuery(`kernel language:"Standard ML" user:~alice tag:sel4`)
	if text != "kernel" || quals["language"] != "Standard ML" || quals["user"] != "alice" || quals["tag"] != "sel4" {
		t.Errorf("got %q %v", text, quals)
	}
	if got := stripLanguage(`x lang:go tag:"a b"`); got != `x tag:"a b"` {
		t.Errorf("stripLanguage = %q", got)
	}
}

func TestBuckets(t *testing.T) {
	b := newBuckets(0.001, 3)
	for i := 0; i < 3; i++ {
		if !b.allow("k") {
			t.Fatal("burst refused early")
		}
	}
	if b.allow("k") {
		t.Error("bucket did not run dry")
	}
	if !b.allow("other") {
		t.Error("keys are not independent")
	}
}

func TestCappedBuffer(t *testing.T) {
	var b cappedBuffer
	if _, err := b.Write(make([]byte, maxOutput)); err != nil {
		t.Fatal(err)
	}
	if _, err := b.Write([]byte{1}); err == nil || !b.over {
		t.Error("cap not enforced")
	}
}

func TestMarkdownSanitised(t *testing.T) {
	s, _ := testServer(t)
	for _, in := range []string{
		`<script>alert(1)</script>`,
		`<img src=x onerror=alert(1)>`,
		`[x](javascript:alert(1))`,
		`<a href="javascript:alert(1)">x</a>`,
		`<video src="javascript:alert(1)"></video>`,
		`<iframe src="https://evil.example"></iframe>`,
		`<svg onload=alert(1)>`,
		`<div style="background:url(javascript:alert(1))">x</div>`,
	} {
		out := strings.ToLower(string(s.renderMarkdownAt([]byte(in), "/raw/", "/tree/")))
		for _, bad := range []string{"<script", "onerror", "javascript:", "<iframe", "onload", "style="} {
			if strings.Contains(out, bad) {
				t.Errorf("%q rendered %q", in, out)
			}
		}
	}
}

func TestSizes(t *testing.T) {
	for in, want := range map[string]int64{"2G": 2 << 30, "500M": 500 << 20, "2048 MiB": 2 << 30, "10KB": 10 << 10, "0": 0} {
		if got, err := parseSize(in); err != nil || got != want {
			t.Errorf("parseSize(%q) = %d %v", in, got, err)
		}
	}
	if got := compactSize(2 << 30); got != "2G" {
		t.Errorf("compactSize = %s", got)
	}
}

func TestHeaderInjection(t *testing.T) {
	if got := headerText("hi\r\nBcc: x@evil.example"); strings.ContainsAny(got, "\r\n") {
		t.Errorf("header kept a line break: %q", got)
	}
	for _, bad := range []string{"a@b.c\r\nBcc: x@y.z", "a@b.c, x@y.z", "<a@b.c>", "nope", "a b@c.d"} {
		if validEmail(bad) {
			t.Errorf("accepted %q", bad)
		}
	}
	if !validEmail("someone@example.org") {
		t.Error("rejected a plain address")
	}
}

func TestRelativeLinkBaseEscaped(t *testing.T) {
	s, _ := testServer(t)
	out := string(s.renderMarkdownAt([]byte("![x](pic.png) [y](doc.md)"), `/~a/r/raw/x"onmouseover="alert(1)/`, `/~a/r/tree/x"><script>/`))
	if strings.Contains(out, `"onmouseover`) || strings.Contains(out, "<script") {
		t.Errorf("base broke out of the attribute: %s", out)
	}
}

func TestTrustedProxy(t *testing.T) {
	_, n, _ := net.ParseCIDR("172.31.255.0/28")
	trustedProxies = []*net.IPNet{n}
	defer func() { trustedProxies = nil }()
	r := httptest.NewRequest("GET", "/", nil)
	r.Header.Set("X-Forwarded-For", "203.0.113.9, 198.51.100.7")
	r.RemoteAddr = "172.31.255.1:5000"
	if got := clientIP(r); got != "198.51.100.7" {
		t.Errorf("trusted proxy hop ignored: %s", got)
	}
	r.RemoteAddr = "192.0.2.4:5000"
	if got := clientIP(r); got != "192.0.2.4" {
		t.Errorf("untrusted peer could choose its address: %s", got)
	}
}

func TestImportSource(t *testing.T) {
	for _, raw := range []string{"http://github.com/a/b", "https://127.0.0.1/a", "https://localhost/a", "https://user:pw@github.com/a/b", "file:///etc", "https://10.0.0.1/x", "ssh://git@github.com/a/b"} {
		if _, _, err := importSource(raw); err == nil {
			t.Errorf("%s accepted", raw)
		}
	}
}

func TestRewriteAuthors(t *testing.T) {
	dir := t.TempDir()
	run := func(env []string, args ...string) string {
		cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
		cmd.Env = append(os.Environ(), env...)
		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Fatalf("git %v: %v %s", args, err, out)
		}
		return string(out)
	}
	run(nil, "init", "-q", "-b", "main")
	for i, who := range []string{"Old Me", "Someone Else", "Old Me"} {
		os.WriteFile(filepath.Join(dir, "f"), []byte(strconv.Itoa(i)), 0o644)
		run(nil, "add", "f")
		run([]string{"GIT_AUTHOR_NAME=" + who, "GIT_AUTHOR_EMAIL=" + strings.ReplaceAll(strings.ToLower(who), " ", ".") + "@example.org", "GIT_COMMITTER_NAME=" + who, "GIT_COMMITTER_EMAIL=c@example.org"}, "commit", "-q", "-m", "data 99\nmessage that mentions author Old Me <old.me@example.org>")
	}
	run(nil, "tag", "v1")
	if err := rewriteAuthors(dir, map[string]bool{"Old Me <old.me@example.org>": true}, "New Me <new@example.org>"); err != nil {
		t.Fatal(err)
	}
	got := run(nil, "log", "--format=%an <%ae>", "main")
	if strings.Count(got, "New Me <new@example.org>") != 2 || strings.Count(got, "Someone Else") != 1 || strings.Contains(got, "Old Me <old.me") {
		t.Errorf("history after rewrite:\n%s", got)
	}
	if !strings.Contains(run(nil, "log", "-1", "--format=%B", "main"), "author Old Me <old.me@example.org>") {
		t.Error("commit message was rewritten")
	}
	if run(nil, "rev-parse", "v1^{commit}") != run(nil, "rev-parse", "main") {
		t.Error("tag not moved to the rewritten commit")
	}
}