internal/codescan/attrs_test.go
87 lines · 3051 bytes
1package codescan
2
3import (
4 "errors"
5 "testing"
6
7 "gitbay.org/gitbay/internal/store"
8)
9
10func TestUnquotedLines(t *testing.T) {
11 for _, tc := range []struct {
12 name, src string
13 want bool
14 }{
15 {"quotes inside a quoted value", `<a href="/{{ page.url | replace("index.html", "") }}">x</a>`, false},
16 {"quoted literal in a quoted value", `<meta property="og:image" content="{{ "og.png" | absolute }}">`, false},
17 {"single quotes", `<a href='{{ x }}'>`, false},
18 {"action adding a name", `<option value="off"{{if eq $mode "off"}} selected{{end}}>Off</option>`, false},
19 {"handlebars block", `<input {{#if on}}checked{{/if}}>`, false},
20 {"comment", `<p {{/* note */}} class="a">`, false},
21 {"jinja statement", `<option {% if x %}selected{% endif %}>`, false},
22 {"expression in text", `<p>{{ title }}</p>`, false},
23 {"script body", "<script>if (a<b) { x = {{ y }} }</script>", false},
24 {"html comment", `<!-- <a href={{ x }}> -->`, false},
25 {"unquoted value", `<a href={{ x }}>`, true},
26 {"unquoted after text", `<a href=/p/{{ x }}>`, true},
27 {"standing in for attributes", `<div {{ attrs }}>`, true},
28 {"output inside an action", `<option {{if .A}}value={{.V}}{{end}}>`, true},
29 {"triple stash", `<a href={{{ x }}}>`, true},
30 } {
31 lines, ok := unquotedLines([]byte(tc.src))
32 if !ok {
33 t.Errorf("%s: not read", tc.name)
34 continue
35 }
36 if lines[1] != tc.want {
37 t.Errorf("%s: unquoted = %v, want %v", tc.name, lines[1], tc.want)
38 }
39 }
40}
41
42func TestUnquotedLinesAcrossLines(t *testing.T) {
43 src := "<ul>\n<li\n class=\"a\"\n data-x={{ x }}\n title=\"{{ \"t\" }}\">\n</ul>\n"
44 lines, ok := unquotedLines([]byte(src))
45 if !ok || !lines[4] || lines[5] || len(lines) != 1 {
46 t.Errorf("lines = %v ok %v", lines, ok)
47 }
48 for _, src := range []string{`<a href="{{ x }}`, `<a href={{ x`, `<!-- open`} {
49 if _, ok := unquotedLines([]byte(src)); ok {
50 t.Errorf("%q read", src)
51 }
52 }
53}
54
55func TestScopeChecksUnquotedAttributes(t *testing.T) {
56 rule := "opt.opengrep-rules.generic.html-templates.security.unquoted-attribute-var"
57 files := map[string]string{
58 "quoted.html": "<p>\n<a href=\"{{ f(\"a\") }}\">x</a>\n",
59 "unquoted.html": "<a href={{ x }}>\n",
60 "broken.html": "<a href=\"{{ x }}",
61 }
62 read := func(p string) ([]byte, error) {
63 if src, ok := files[p]; ok {
64 return []byte(src), nil
65 }
66 return nil, errors.New("missing")
67 }
68 findings := []store.SecurityFinding{
69 {Rule: rule, Path: "quoted.html", Line: 2},
70 {Rule: rule, Path: "unquoted.html", Line: 1},
71 {Rule: rule, Path: "broken.html", Line: 1},
72 {Rule: rule, Path: "gone.html", Line: 1},
73 {Rule: "other", Path: "quoted.html", Line: 2},
74 }
75 kept, aside, _ := Scope{Read: read}.Apply(findings)
76 if aside.Quoted != 1 || len(kept) != 4 {
77 t.Fatalf("kept %+v aside %+v", kept, aside)
78 }
79 for _, f := range kept {
80 if f.Path == "quoted.html" && f.Rule == rule {
81 t.Errorf("quoted hit kept")
82 }
83 }
84 if kept, aside, _ := (Scope{}).Apply(findings); aside.Quoted != 0 || len(kept) != 5 {
85 t.Errorf("without Read: kept %d aside %+v", len(kept), aside)
86 }
87}