-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext.go
89 lines (83 loc) · 2.43 KB
/
context.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
package openrasp
import (
"github.com/baidu-security/openrasp-golang/gls"
"github.com/baidu-security/openrasp-golang/model"
v8 "github.com/baidu-security/openrasp-v8/go"
)
var contextGetters *v8.ContextGetters
func DefaultContextGetters() *v8.ContextGetters {
return contextGetters
}
func InitContextGetters() {
contextGetters = newContextGetters()
}
func newContextGetters() *v8.ContextGetters {
//requestInfo
urlFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.UrlFull
}
pathFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.UrlPath
}
querystringFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.Query
}
methodFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.Method
}
protocolFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.Protocol
}
remoteAddrFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.RemoteAddr
}
parameterFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.GetBytes
}
jsonFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.RequestBody.Raw
}
bodyFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.RequestBody.Truncated
}
appBasePathFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.AppBasePath
}
headerFunc := func() interface{} {
requestInfo, _ := gls.Get("requestInfo").(*model.RequestInfo)
return requestInfo.HeaderBytes
}
//globals
serverFunc := func() interface{} {
return GetGlobals().ContextServer.Bytes()
}
cg := &v8.ContextGetters{
Url: urlFunc,
Path: pathFunc,
Querystring: querystringFunc,
Method: methodFunc,
Protocol: protocolFunc,
RemoteAddr: remoteAddrFunc,
Header: headerFunc,
Parameter: parameterFunc,
Json: jsonFunc,
Server: serverFunc,
AppBasePath: appBasePathFunc,
Body: bodyFunc,
}
return cg
}
func RequestInfoAvailable() bool {
_, ok := gls.Get("requestInfo").(*model.RequestInfo)
return ok
}