forked from michenriksen/gitrob
/
router.go
138 lines (119 loc) · 3.2 KB
/
router.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
package core
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/codeEmitter/gitrob/common"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gin-contrib/secure"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
const (
GithubBaseUri = "https://raw.githubusercontent.com"
MaximumFileSize = 153600
GitLabBaseUri = "https://gitlab.com"
CspPolicy = "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'"
ReferrerPolicy = "no-referrer"
)
var IsGithub bool
type binaryFileSystem struct {
fs http.FileSystem
}
func (b *binaryFileSystem) Open(name string) (http.File, error) {
return b.fs.Open(name)
}
func (b *binaryFileSystem) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
if _, err := b.fs.Open(p); err != nil {
return false
}
return true
}
return false
}
func BinaryFileSystem(root string) *binaryFileSystem {
fs := &assetfs.AssetFS{Asset, AssetDir, AssetInfo, root}
return &binaryFileSystem{
fs,
}
}
func NewRouter(s *Session) *gin.Engine {
IsGithub = s.IsGithubSession
if *s.Options.Debug == true {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.Use(static.Serve("/", BinaryFileSystem("static")))
router.Use(secure.New(secure.Config{
SSLRedirect: false,
IsDevelopment: false,
FrameDeny: true,
ContentTypeNosniff: true,
BrowserXssFilter: true,
ContentSecurityPolicy: CspPolicy,
ReferrerPolicy: ReferrerPolicy,
}))
router.GET("/stats", func(c *gin.Context) {
c.JSON(200, s.Stats)
})
router.GET("/findings", func(c *gin.Context) {
c.JSON(200, s.Findings)
})
router.GET("/targets", func(c *gin.Context) {
c.JSON(200, s.Targets)
})
router.GET("/repositories", func(c *gin.Context) {
c.JSON(200, s.Repositories)
})
router.GET("/files/:owner/:repo/:commit/*path", fetchFile)
return router
}
func fetchFile(c *gin.Context) {
fileUrl := func() string {
if IsGithub {
return fmt.Sprintf("%s/%s/%s/%s%s", GithubBaseUri, c.Param("owner"), c.Param("repo"), c.Param("commit"), c.Param("path"))
} else {
results := common.CleanUrlSpaces(c.Param("owner"), c.Param("repo"), c.Param("commit"), c.Param("path"))
return fmt.Sprintf("%s/%s/%s/%s/%s%s", GitLabBaseUri, results[0], results[1], "/-/raw/", results[2], results[3])
}
}()
resp, err := http.Head(fileUrl)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err,
})
return
}
if resp.StatusCode == http.StatusNotFound {
c.JSON(http.StatusNotFound, gin.H{
"message": "No content",
})
return
}
if resp.ContentLength > MaximumFileSize {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"message": fmt.Sprintf("File size exceeds maximum of %d bytes", MaximumFileSize),
})
return
}
resp, err = http.Get(fileUrl)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err,
})
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err,
})
return
}
c.String(http.StatusOK, string(body[:]))
}