This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
safebrowsing.go
126 lines (104 loc) · 2.97 KB
/
safebrowsing.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
package google
import (
"encoding/json"
"fmt"
"net/http"
"github.com/short-d/app/fw/webreq"
"github.com/short-d/short/backend/app/usecase/risk"
)
var _ risk.BlackList = (*SafeBrowsing)(nil)
const safeBrowsingLookupAPI = "https://safebrowsing.googleapis.com/v4/threatMatches:find"
type threatType string
const (
unknown threatType = "THREAT_TYPE_UNSPECIFIED"
malware threatType = "MALWARE"
socialEngineering threatType = "SOCIAL_ENGINEERING"
potentiallyHarmfulApp threatType = "POTENTIALLY_HARMFUL_APPLICATION"
unwantedSoftware threatType = "UNWANTED_SOFTWARE"
)
type platformType string
const (
allPlatforms platformType = "ALL_PLATFORMS"
anyPlatform platformType = "ANY_PLATFORM"
window platformType = "WINDOWS"
linux platformType = "LINUX"
osx platformType = "OSX"
chrome platformType = "CHROME"
ios platformType = "IOS"
android platformType = "Android"
)
type threatEntryType string
const (
unspecified threatEntryType = "THREAT_ENTRY_TYPE_UNSPECIFIED"
maliciousURL threatEntryType = "URL"
executable threatEntryType = "EXECUTABLE"
)
type threat struct {
URL string `json:"url"`
}
type lookupAPIRequest struct {
ThreatInfo threatInfo `json:"threatInfo"`
}
type threatInfo struct {
ThreatTypes []threatType `json:"threatTypes"`
PlatformTypes []platformType `json:"platformTypes"`
ThreatEntries []threat `json:"threatEntries"`
}
type lookupAPIResponse struct {
Matches []match `json:"matches"`
}
type match struct {
ThreatType threatType `json:"threatType"`
PlatformType platformType `json:"platformType"`
Threat threat `json:"threat"`
CacheDuration string `json:"cacheDuration"`
ThreatEntryType threatEntryType `json:"threatEntryType"`
}
// SafeBrowsing represents Googles SafeBrowsing APIs.
type SafeBrowsing struct {
apiKey string
httpRequest webreq.HTTP
}
// HasURL checks whether a given URL is blacklisted by Google.
func (s SafeBrowsing) HasURL(url string) (bool, error) {
api := s.auth(safeBrowsingLookupAPI)
body := lookupAPIRequest{
ThreatInfo: threatInfo{
ThreatTypes: []threatType{
malware,
socialEngineering,
potentiallyHarmfulApp,
unwantedSoftware,
},
PlatformTypes: []platformType{
allPlatforms,
},
ThreatEntries: []threat{
{URL: url},
},
},
}
buf, err := json.Marshal(body)
if err != nil {
return false, err
}
headers := map[string]string{
"Content-Type": "application/json",
}
res := lookupAPIResponse{}
err = s.httpRequest.JSON(http.MethodPost, api, headers, string(buf), &res)
if err != nil {
return false, err
}
return len(res.Matches) > 0, nil
}
func (s SafeBrowsing) auth(baseURL string) string {
return fmt.Sprintf("%s/?key=%s", baseURL, s.apiKey)
}
// NewSafeBrowsing initializes Google's SafeBrowsing API client.
func NewSafeBrowsing(apiKey string, req webreq.HTTP) SafeBrowsing {
return SafeBrowsing{
apiKey: apiKey,
httpRequest: req,
}
}