-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcanonicalize.go
303 lines (283 loc) · 8.57 KB
/
canonicalize.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright (c) 2013, Richard Johnson
Copyright (c) 2014, Kilian Gilonne
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package safebrowsing
import (
"bytes"
"encoding/hex"
"fmt"
"net"
"regexp"
"strconv"
"strings"
)
// Canonicalize a URL as needed for safe browsing lookups.
// This is required before obtaining the host key or generating
// url lookup iterations.
func Canonicalize(fullurl string) (canonicalized string) {
// basic trim
fullurl = strings.TrimSpace(fullurl)
// add default http protocol
re := regexp.MustCompile("[a-zA-Z][a-zA-Z0-9+-.]*://.*")
if !re.Match([]byte(fullurl)) {
fullurl = "http://" + fullurl
}
// strip off the fragment (if it exists)
fullurl = strings.Split(fullurl, "#")[0]
// remove any tab (0x09), CR (0x0d), and LF (0x0a)
fullurl = strings.Replace(fullurl, "\t", "", -1)
fullurl = strings.Replace(fullurl, "\n", "", -1)
fullurl = strings.Replace(fullurl, "\r", "", -1)
// unescape until there are no more encoded chars
for newurl, performedUnescape := unescape(fullurl); performedUnescape; {
fullurl = newurl
newurl, performedUnescape = unescape(fullurl)
}
// extract the hostname
fullurl = canonicalizeHostname(fullurl)
fullurl = canonicalizePath(fullurl)
fullurl = escapeUrl(fullurl)
return fullurl
}
func canonicalizeHostname(fullurl string) (canonicalized string) {
// extract the hostname from the url
re := regexp.MustCompile("[a-zA-Z][a-zA-Z0-9+-.]*://([^/]+)/.*")
matches := re.FindAllSubmatch([]byte(fullurl), 1)
if len(matches) > 0 {
hostname := string(matches[0][1])
// remove all leading and trailing dots
canonicalized = strings.Trim(hostname, ".")
// Replace consecutive dots with a single dot.
re = regexp.MustCompile("\\.\\.*")
canonicalized = re.ReplaceAllString(canonicalized, ".")
// attempt to parse as a IP address.
ip := net.ParseIP(canonicalized)
if ip != nil {
canonicalized = ip.String()
}
ipInt, err := strconv.ParseUint(canonicalized, 10, 0)
if err == nil {
// we were an int!
canonicalized = fmt.Sprintf("%d.%d.%d.%d",
(ipInt>>24)&0xFF,
(ipInt>>16)&0xFF,
(ipInt>>8)&0xFF,
ipInt&0xFF)
}
canonicalized = strings.ToLower(canonicalized)
canonicalized = strings.Replace(fullurl, hostname, canonicalized, 1)
return canonicalized
}
return fullurl
}
func canonicalizePath(fullurl string) (canonicalized string) {
re := regexp.MustCompile("[a-zA-Z][a-zA-Z0-9+-.]*://[^/]+(/[^?]+)")
matches := re.FindAllSubmatch([]byte(fullurl), 1)
if len(matches) > 0 {
path := string(matches[0][1])
// The sequences "/../" and "/./" in the path should be resolved,
// by replacing "/./" with "/", and removing "/../" along with
// the preceding path component.
canonicalized = strings.Replace(path, "/./", "/", -1)
re = regexp.MustCompile("/?[^/]+/\\.\\.(/|$)")
canonicalized = re.ReplaceAllString(canonicalized, "/")
re = regexp.MustCompile("//*")
canonicalized = re.ReplaceAllString(canonicalized, "/")
canonicalized = strings.Replace(fullurl, path, canonicalized, 1)
return canonicalized
}
if fullurl[len(fullurl)-1] != '/' {
fullurl = fullurl + "/"
}
return fullurl
}
func escapeUrl(url string) string {
// percent-escape all characters in the URL which are <= ASCII 32,
// >= 127, "#", or "%". The escapes should use uppercase hex characters.
buf := bytes.Buffer{}
buf.Grow(len(url))
for _, b := range []byte(url) {
switch {
case b <= 32 || b >= 127 || b == '#' || b == '%':
buf.WriteByte('%')
buf.WriteString(hex.EncodeToString([]byte{b}))
default:
buf.WriteByte(b)
}
}
return buf.String()
}
// custom version of unescape to work around potential errors
// that would otherwise throw off url.QueryUnescape
func unescape(s string) (string, bool) {
// Count %, check that they're well-formed.
n := 0
t := make([]byte, len(s)-2*n)
j := 0
performedUnescape := false
for i := 0; i < len(s); {
switch s[i] {
case '%':
if i+2 >= len(s) || !ishex(s[i+1]) || !ishex(s[i+2]) {
// we were an invalid encoding, copy a char and keep going
t[j] = s[i]
j++
i++
} else {
t[j] = unhex(s[i+1])<<4 | unhex(s[i+2])
performedUnescape = true
j++
i += 3
}
default:
t[j] = s[i]
j++
i++
}
}
return string(t[:j]), performedUnescape
}
func unhex(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}
func ishex(c byte) bool {
switch {
case '0' <= c && c <= '9':
return true
case 'a' <= c && c <= 'f':
return true
case 'A' <= c && c <= 'F':
return true
}
return false
}
func iterateHostnames(fullurl string) (urls []string) {
re := regexp.MustCompile("([a-zA-Z][a-zA-Z0-9+-.]*://)([^/]+)/.*")
matches := re.FindAllSubmatch([]byte(fullurl), 1)
if len(matches) > 0 {
hostname := string(matches[0][2])
ip := net.ParseIP(hostname)
if ip != nil {
// we're an IP!
return []string{fullurl}
}
pathBits := strings.Split(hostname, ".")
urls = make([]string, 0, len(pathBits))
// add the initial one
urls = append(urls, fullurl)
if len(pathBits) > 1 {
if len(pathBits) > 6 {
pathBits = pathBits[len(pathBits)-6:]
}
newHost := pathBits[len(pathBits)-2] + "." + pathBits[len(pathBits)-1]
urls = append(urls, strings.Replace(fullurl, hostname, newHost, 1))
for x := len(pathBits) - 3; x > 0; x-- {
newHost = pathBits[x] + "." + newHost
newUrl := strings.Replace(fullurl, hostname, newHost, 1)
urls = append(urls, newUrl)
}
}
return urls
}
return []string{fullurl}
}
func iteratePaths(fullurl string) (urls []string) {
re := regexp.MustCompile("([a-zA-Z][a-zA-Z0-9+-.]*://[^/]+)(/[^?]*)")
matches := re.FindAllSubmatch([]byte(fullurl), 1)
urls = make([]string, 0)
if strings.ContainsRune(fullurl, '?') {
// add original url
urls = append(urls, fullurl)
}
if len(matches) > 0 {
bits := matches[0]
if len(bits) > 2 {
path := string(bits[2])
prefix := string(bits[1])
// url without query string
urls = append(urls, prefix+path)
pathBits := strings.Split(path, "/")
if len(pathBits) > 1 {
// url without path
prefix += "/"
urls = append(urls, prefix)
for x := 1; x < len(pathBits)-1 && x < 4; x++ {
prefix += pathBits[x] + "/"
urls = append(urls, prefix)
}
}
}
}
return urls
}
func stripProtocol(fullurl string) (url string) {
sep := "://"
startByte := strings.Index(fullurl, sep)
if startByte == -1 {
return fullurl
}
startByte += len(sep)
return fullurl[startByte:]
}
// Generate all required iterations of the URL for checking against the
// lookup table.
// NOTE: We assume that the URL has already be Canonicalized
func GenerateTestCandidates(url string) (urls []string) {
urls = make([]string, 0)
values := iterateHostnames(url)
for _, val := range values {
paths := iteratePaths(val)
for _, path := range paths {
path = stripProtocol(path)
urls = append(urls, path)
}
}
return urls
}
// Extract the host from a URL in a format suitable for hashing to generate
// a Host Key.
// NOTE: We assume that the URL has already be Canonicalized
func ExtractHostKey(fullUrl string) (url string) {
// strip off protocol
url = stripProtocol(fullUrl)
// strip off the path
index := strings.Index(url, "/")
if index > 0 {
url = url[:index+1]
} else {
url += "/"
}
dotCount := strings.Count(url, ".")
for dotCount > 2 {
url = url[strings.Index(url, ".")+1:]
dotCount = strings.Count(url, ".")
}
return url
}