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
|
test_description='Check the shipped about-render extension'
CGIT_TEST_NO_CREATE_REPOS=YesPlease
. ./setup.sh
. "$TEST_OUTPUT_DIRECTORY/extensions/lib.sh"
for lua in ${interpreters:-lua}
do
lua_module_prereq "$lua" lpeg
test_expect_success "STANDALONE_LUA,$lua_prereq" "unit checks under $(lua_version "$lua")" '
"$lua" "$EXT_TEST_DIRECTORY/test-about-render.lua" "$EXTENSIONS_DIRECTORY/about-render.lua" lpeg
'
test_expect_success STANDALONE_LUA "fallback checks without lpeg under $(lua_version "$lua")" '
"$lua" "$EXT_TEST_DIRECTORY/test-about-render.lua" "$EXTENSIONS_DIRECTORY/about-render.lua" nolpeg
'
done
test_expect_success 'create a repository with a markdown readme' '
test_create_repo repos/md &&
(
cd repos/md &&
cat >README.md <<-\EOF &&
# The Title
A **bold** claim and a <script>alert(1)</script> attempt.
See the [roadmap](ROADMAP.md#plan).
EOF
cat >tool.1 <<-\EOF &&
.TH TOOL 1
.SH NAME
tool \- does one thing \(em well
.SH OPTIONS
.TP
.B \-v
Be \fIverbose\fR.
.nf
tool \-v <file>
.fi
EOF
echo "Plain <b>notes</b> & more" >NOTES &&
git add README.md tool.1 NOTES &&
git commit -m "add readme" &&
git branch next
)
'
test_expect_success 'point cgit at it through the about filter' '
cat >cgitrc <<-EOF
virtual-root=/
cache-size=0
repo.url=md
repo.path=$PWD/repos/md/.git
repo.readme=:README.md
repo.about-filter=lua:$EXTENSIONS_DIRECTORY/about-render.lua
repo.url=man
repo.path=$PWD/repos/md/.git
repo.readme=:tool.1
repo.about-filter=lua:$EXTENSIONS_DIRECTORY/about-render.lua
repo.url=txt
repo.path=$PWD/repos/md/.git
repo.readme=:NOTES
repo.about-filter=lua:$EXTENSIONS_DIRECTORY/about-render.lua
repo.url=noreadme
repo.path=$PWD/repos/md/.git
repo.about-filter=lua:$EXTENSIONS_DIRECTORY/about-render.lua
EOF
'
if cgit_lua_probe "$PWD/repos/md/.git" lpeg
then
test_set_prereq CGIT_LUA_LPEG
fi
test_expect_success CGIT_LUA_LPEG 'the about page renders the markdown' '
cgit_ok_url "md/about/" >tmp &&
test_grep "<h1 id=.the-title.>The Title</h1>" tmp &&
test_grep "<strong>bold</strong>" tmp
'
test_expect_success CGIT_LUA_LPEG 'readme markup reaches the page escaped' '
cgit_ok_url "md/about/" >tmp &&
test_grep "<script>alert(1)</script>" tmp &&
test_grep ! "<script>alert(1)" tmp
'
test_expect_success CGIT_LUA_LPEG 'a relative link leads into the tree' '
cgit_ok_url "md/about/" >tmp &&
test_grep "<a href=./md/tree/ROADMAP.md#plan.>roadmap</a>" tmp
'
test_expect_success CGIT_LUA_LPEG 'the link stays on the branch shown' '
cgit_ok_url "md/about/&h=next" >tmp &&
test_grep "<a href=./md/tree/ROADMAP.md?h=next#plan.>roadmap</a>" tmp
'
test_expect_success CGIT_LUA_LPEG 'the about page renders a man page' '
cgit_ok_url "man/about/" >tmp &&
test_grep "<div class=.markdown.><h2 id=.name.>NAME</h2>" tmp &&
test_grep "tool - does one thing — well" tmp &&
test_grep "<strong>-v</strong><br>" tmp &&
test_grep "<pre><code>tool -v <file>" tmp &&
test_grep ! "\\\\f" tmp
'
test_expect_success CGIT_LUA_LPEG 'the about page shows plain text as it is' '
cgit_ok_url "txt/about/" >tmp &&
test_grep "<pre class=.plaintext.>Plain <b>notes</b> & more" tmp
'
test_expect_success CGIT_LUA 'the about page wants a readme line, even with the filter' '
cgit_url "noreadme/about/" >tmp &&
test_grep "^Status: 302" tmp &&
test_grep "^Location: /noreadme/$" tmp
'
test_expect_success CGIT_LUA_LPEG 'the site readme turns no unsafe link into one' '
cat >root.md <<-\EOF &&
# Site
A [bad](javascript:alert%281%29) link and a [good](https://example.com/) one.
EOF
cat >rootrc <<-EOF &&
virtual-root=/
cache-size=0
root-readme=$PWD/root.md
about-filter=lua:$EXTENSIONS_DIRECTORY/about-render.lua
EOF
cgit_ok rootrc "p=about" >tmp &&
test_grep "<h1 id=.site.>Site</h1>" tmp &&
test_grep "<a href=.https://example.com/.>good</a>" tmp &&
test_grep "A bad link" tmp &&
test_grep ! "javascript:" tmp
'
test_expect_success CGIT_LUA 'without lpeg every readme is served escaped' '
echo "[cgit] Unable to load lpeg, so markdown and man pages show as plain text" >expect &&
without_lua_modules cgit_url "md/about/" >tmp 2>err &&
test_cmp expect err &&
test_grep "^Status: 200" tmp &&
test_grep "</html>" tmp &&
test_grep "<pre class=.plaintext.># The Title" tmp &&
test_grep "<script>alert(1)</script>" tmp &&
test_grep ! "<script>alert(1)" tmp &&
without_lua_modules cgit_url "man/about/" >tmp 2>err &&
test_cmp expect err &&
test_grep "<pre class=.plaintext.>\.TH TOOL 1" tmp &&
test_grep -F ".SH NAME" tmp &&
without_lua_modules cgit_url "txt/about/" >tmp 2>err &&
test_cmp expect err &&
test_grep "<pre class=.plaintext.>Plain <b>notes</b> & more" tmp
'
test_done
|