~gitpocket/gitpocket

4
4c1d80270e0f38f1fd0000c635230dacce3fae5b
mainfeed-discovery
gitpocket/unit_test.go -rw-r--r--3.9 KiB ViewLogBlameView rawPermalink 6f4e245fcc test(web): cover registration, permissions, merges and injection payloads 12 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
package main

import (
	"net/http/httptest"
	"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:~james tag:sel4`)
	if text != "kernel" || quals["language"] != "Standard ML" || quals["user"] != "james" || 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)
	}
}