Skip to content

Commit 8a24daf

Browse files
committed
modify tests and rule
1 parent 7877082 commit 8a24daf

File tree

6 files changed

+162
-97
lines changed

6 files changed

+162
-97
lines changed

go/ql/src/experimental/CWE-525/WebCacheDeception.ql

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212

1313
import go
1414

15-
from
16-
DataFlow::CallNode httpHandleFuncCall, DataFlow::CallNode call, Method get
15+
from DataFlow::CallNode httpHandleFuncCall, Http::HeaderWrite::Range hw
1716
where
1817
httpHandleFuncCall.getTarget().hasQualifiedName("net/http", "HandleFunc") and
1918
httpHandleFuncCall.getArgument(0).getType().getUnderlyingType() instanceof StringType and
2019
httpHandleFuncCall.getArgument(0).getStringValue().matches("%/") and
21-
get.hasQualifiedName("net/http", "Header", "Set") and
22-
call = get.getACall() and
23-
call.getArgument(0).getStringValue() = "Cache-Control"
24-
select httpHandleFuncCall.getArgument(0), call.getArgument(0)
20+
hw.getHeaderName() = "cache-control"
21+
select httpHandleFuncCall.getArgument(0), hw.getHeaderName()

go/ql/src/experimental/CWE-525/WebCacheDeceptionBad.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package bad
22

33
import (
44
"fmt"
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package good
2+
3+
import (
4+
"fmt"
5+
"html/template"
6+
"log"
7+
"net/http"
8+
"os/exec"
9+
"strings"
10+
"sync"
11+
)
12+
13+
var sessionMap = make(map[string]string)
14+
15+
var (
16+
templateCache = make(map[string]*template.Template)
17+
mutex = &sync.Mutex{}
18+
)
19+
20+
type Lists struct {
21+
Uid string
22+
UserName string
23+
UserLists []string
24+
ReadFile func(filename string) string
25+
}
26+
27+
func parseTemplateFile(templateName string, tmplFile string) (*template.Template, error) {
28+
mutex.Lock()
29+
defer mutex.Unlock()
30+
31+
// Check if the template is already cached
32+
if cachedTemplate, ok := templateCache[templateName]; ok {
33+
fmt.Println("cached")
34+
return cachedTemplate, nil
35+
}
36+
37+
// Parse and store the template in the cache
38+
parsedTemplate, _ := template.ParseFiles(tmplFile)
39+
fmt.Println("not cached")
40+
41+
templateCache[templateName] = parsedTemplate
42+
return parsedTemplate, nil
43+
}
44+
45+
func ShowAdminPageCache(w http.ResponseWriter, r *http.Request) {
46+
47+
if r.Method == "GET" {
48+
fmt.Println("cache called")
49+
sessionMap[r.RequestURI] = "admin"
50+
51+
// Check if a session value exists
52+
if _, ok := sessionMap[r.RequestURI]; ok {
53+
cmd := "mysql -h mysql -u root -prootwolf -e 'select id,name,mail,age,created_at,updated_at from vulnapp.user where name not in (\"" + "admin" + "\");'"
54+
55+
// mysql -h mysql -u root -prootwolf -e 'select id,name,mail,age,created_at,updated_at from vulnapp.user where name not in ("test");--';echo");'
56+
fmt.Println(cmd)
57+
58+
res, err := exec.Command("sh", "-c", cmd).Output()
59+
if err != nil {
60+
fmt.Println("err : ", err)
61+
}
62+
63+
splitedRes := strings.Split(string(res), "\n")
64+
65+
p := Lists{Uid: "1", UserName: "admin", UserLists: splitedRes}
66+
67+
parsedTemplate, _ := parseTemplateFile("page", "./views/admin/userlists.gtpl")
68+
w.Header().Set("Cache-Control", "no-store, no-cache")
69+
err = parsedTemplate.Execute(w, p)
70+
}
71+
} else {
72+
http.NotFound(w, nil)
73+
}
74+
75+
}
76+
77+
func main() {
78+
fmt.Println("Vulnapp server listening : 1337")
79+
80+
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
81+
82+
http.HandleFunc("/adminusers", ShowAdminPageCache)
83+
err := http.ListenAndServe(":1337", nil)
84+
if err != nil {
85+
log.Fatal("ListenAndServe: ", err)
86+
}
87+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| WebCacheDeceptionBad.go:14:18:14:31 | "/adminusers/" | WebCacheDeceptionGood.go:68:19:68:33 | "Cache-Control" |
1+
| WebCacheDeceptionBad.go:82:18:82:31 | "/adminusers/" | cache-control |

go/ql/test/experimental/CWE-525/WebCacheDeceptionBad.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
1-
package main
1+
package bad
22

33
import (
44
"fmt"
5+
"html/template"
56
"log"
67
"net/http"
8+
"os/exec"
9+
"strings"
10+
"sync"
711
)
812

9-
func badExample() {
13+
var sessionMap = make(map[string]string)
14+
15+
var (
16+
templateCache = make(map[string]*template.Template)
17+
mutex = &sync.Mutex{}
18+
)
19+
20+
type Lists struct {
21+
Uid string
22+
UserName string
23+
UserLists []string
24+
ReadFile func(filename string) string
25+
}
26+
27+
func parseTemplateFile(templateName string, tmplFile string) (*template.Template, error) {
28+
mutex.Lock()
29+
defer mutex.Unlock()
30+
31+
// Check if the template is already cached
32+
if cachedTemplate, ok := templateCache[templateName]; ok {
33+
fmt.Println("cached")
34+
return cachedTemplate, nil
35+
}
36+
37+
// Parse and store the template in the cache
38+
parsedTemplate, _ := template.ParseFiles(tmplFile)
39+
fmt.Println("not cached")
40+
41+
templateCache[templateName] = parsedTemplate
42+
return parsedTemplate, nil
43+
}
44+
45+
func ShowAdminPageCache(w http.ResponseWriter, r *http.Request) {
46+
47+
if r.Method == "GET" {
48+
fmt.Println("cache called")
49+
sessionMap[r.RequestURI] = "admin"
50+
51+
// Check if a session value exists
52+
if _, ok := sessionMap[r.RequestURI]; ok {
53+
cmd := "mysql -h mysql -u root -prootwolf -e 'select id,name,mail,age,created_at,updated_at from vulnapp.user where name not in (\"" + "admin" + "\");'"
54+
55+
// mysql -h mysql -u root -prootwolf -e 'select id,name,mail,age,created_at,updated_at from vulnapp.user where name not in ("test");--';echo");'
56+
fmt.Println(cmd)
57+
58+
res, err := exec.Command("sh", "-c", cmd).Output()
59+
if err != nil {
60+
fmt.Println("err : ", err)
61+
}
62+
63+
splitedRes := strings.Split(string(res), "\n")
64+
65+
p := Lists{Uid: "1", UserName: "admin", UserLists: splitedRes}
66+
67+
parsedTemplate, _ := parseTemplateFile("page", "./views/admin/userlists.gtpl")
68+
w.Header().Set("Cache-Control", "no-store, no-cache")
69+
err = parsedTemplate.Execute(w, p)
70+
}
71+
} else {
72+
http.NotFound(w, nil)
73+
}
74+
75+
}
76+
77+
func main() {
1078
fmt.Println("Vulnapp server listening : 1337")
1179

1280
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))

go/ql/test/experimental/CWE-525/WebCacheDeceptionGood.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)