-
Notifications
You must be signed in to change notification settings - Fork 45
/
servegin.go
180 lines (160 loc) · 4.89 KB
/
servegin.go
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
package servegin
import (
"github.com/Contrast-Security-OSS/go-test-bench/internal/xss"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/Contrast-Security-OSS/go-test-bench/internal/common"
"github.com/Contrast-Security-OSS/go-test-bench/internal/injection/cmdi"
"github.com/Contrast-Security-OSS/go-test-bench/internal/injection/sqli"
"github.com/Contrast-Security-OSS/go-test-bench/internal/pathtraversal"
"github.com/Contrast-Security-OSS/go-test-bench/internal/ssrf"
"github.com/Contrast-Security-OSS/go-test-bench/internal/unvalidated"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
var base = gin.H{"Framework": "Gin", "Logo": "https://raw.githubusercontent.com/gin-gonic/logo/master/color.png"}
//return a copy of 'base', with Name updated. uses a copy to avoid race conditions.
func templateData(name string) gin.H {
cpy := make(gin.H, len(base)+1)
for k, v := range base {
cpy[k] = v
}
cpy["Name"] = name
return cpy
}
func loadTemplates() multitemplate.Renderer {
templatesDir, err := common.FindViewsDir()
if err != nil {
panic(err.Error())
}
pages, err := filepath.Glob(filepath.Join(templatesDir, "pages", "*.gohtml"))
if err != nil {
panic(err.Error())
}
partials, err := filepath.Glob(filepath.Join(templatesDir, "partials", "*.gohtml"))
if err != nil {
panic(err.Error())
}
layout := filepath.Join(templatesDir, "layout.gohtml")
r := multitemplate.NewRenderer()
for _, p := range pages {
files := append([]string{layout, p}, partials...)
r.AddFromFilesFuncs(filepath.Base(p), common.FuncMap(), files...)
}
return r
}
//add a handler to gin
func add(router *gin.Engine, rt common.Route) {
base := router.Group(rt.Base)
base.GET("", func(c *gin.Context) {
c.HTML(http.StatusOK, rt.TmplFile, templateData(rt.Base))
})
for _, s := range rt.Sinks {
if s.Handler == nil {
var err error
s.Handler, err = common.GenericHandler(s)
if err != nil {
log.Fatal(err)
}
}
sinkFn := func(s *common.Sink) func(c *gin.Context) {
return func(c *gin.Context) {
c.Header("Cache-Control", "no-store") //makes development a whole lot easier
mode := common.Safety(c.Param("mode"))
source := c.Param("source")
payload := extractInput(c, source)
for _, e := range c.Errors {
log.Printf("%s: error %s", c.Request.URL.Path, e)
}
data, mime, status := s.Handler(mode, payload, c)
if len(data) == 0 {
// don't unconditionally write response, as it can result in
// - a warning (when status changes), or
// - a panic (when content-length is already set and headers are written)
return
}
if len(mime) == 0 {
mime = "text/plain"
}
c.Header("Content-Type", mime)
c.String(status, data)
}
}(s)
sinkPg := base.Group("/" + s.URL)
rel := "/:source/:mode"
//route data isn't a perfect match for the method(s) we actually use, so just accept anything
sinkPg.Any(rel, sinkFn)
for _, i := range rt.Inputs {
if i == "params" {
sinkPg.Any(rel+"/*param", sinkFn)
break
}
}
}
}
var ginPathTraversal = common.Sink{
Name: "gin.File",
Sanitize: url.QueryEscape,
VulnerableFnWrapper: func(opaque interface{}, payload string) (data string, raw bool, err error) {
c, ok := opaque.(*gin.Context)
if !ok {
log.Fatalf("'opaque': want *gin.Context, got %T", opaque)
}
c.File(payload)
return "", true, nil
},
}
var unvalidatedRedirect = common.Sink{
Name: "gin.Redirect",
Sanitize: url.PathEscape,
VulnerableFnWrapper: func(opaque interface{}, payload string) (data string, raw bool, err error) {
c, ok := opaque.(*gin.Context)
if !ok {
log.Fatalf("'opaque': want *gin.Context, got %T", opaque)
}
c.Redirect(http.StatusFound, payload)
return "", true, nil
},
}
// RegisterRoutes registers all decoupled routes used with gin. Shared with cmd/exercise.
func RegisterRoutes() {
cmdi.RegisterRoutes()
sqli.RegisterRoutes()
pathtraversal.RegisterRoutes(&ginPathTraversal)
ssrf.RegisterRoutes()
unvalidated.RegisterRoutes(&unvalidatedRedirect)
xss.RegisterRoutes()
}
// Setup loads templates, sets up routes, etc.
func Setup(addr string) (router *gin.Engine, dbFile string) {
base["Addr"] = addr
//register all decoupled routes in this function
RegisterRoutes()
rmap := common.PopulateRouteMap(common.AllRoutes)
base["Rulebar"] = rmap
router = gin.Default()
log.Println("Loading templates...")
router.HTMLRender = loadTemplates()
log.Println("Templates loaded.")
pub, err := common.LocateDir("public", 5)
if err != nil {
log.Fatal(err)
}
router.StaticFS("/assets/", http.Dir(pub))
router.GET("/", func(c *gin.Context) {
c.Header("Application-Framework", "Gin")
c.HTML(http.StatusOK, "index.gohtml", templateData(""))
})
for _, h := range common.AllRoutes {
add(router, h)
}
// setting up a database to execute the built query
dbSrc, err := os.CreateTemp(".", "tempDatabase*.db")
if err != nil {
panic(err)
}
return router, dbSrc.Name()
}