mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Scrub space separated ip addresses
The issue with ReplaceAllFunc is that it's capturing the leading and trailing spaces in the regexp, so successive ips don't match. From the docstring, > If 'All' is present, the routine matches successive non-overlapping > matches of the entire expression. For #40306
This commit is contained in:
parent
98db63ad01
commit
e4c818be76
2 changed files with 18 additions and 4 deletions
|
@ -21,7 +21,7 @@ const ipv6Full = `(` + ipv6Address + `(` + ipv4Address + `))` +
|
|||
`|(` + ipv6Address + `)` + `|(` + ipv6Compressed + `)`
|
||||
const optionalPort = `(:\d{1,5})?`
|
||||
const addressPattern = `((` + ipv4Address + `)|(\[(` + ipv6Full + `)\])|(` + ipv6Full + `))` + optionalPort
|
||||
const fullAddrPattern = `(^|\s|[^\w:])` + addressPattern + `(\s|(:\s)|[^\w:]|$)`
|
||||
const fullAddrPattern = `(?:^|\s|[^\w:])(` + addressPattern + `)(?:\s|(:\s)|[^\w:]|$)`
|
||||
|
||||
var scrubberPatterns = []*regexp.Regexp{
|
||||
regexp.MustCompile(fullAddrPattern),
|
||||
|
@ -46,9 +46,18 @@ func Scrub(b []byte) []byte {
|
|||
for _, pattern := range scrubberPatterns {
|
||||
// this is a workaround since go does not yet support look ahead or look
|
||||
// behind for regular expressions.
|
||||
scrubbedBytes = pattern.ReplaceAllFunc(scrubbedBytes, func(b []byte) []byte {
|
||||
return addressRegexp.ReplaceAll(b, []byte("[scrubbed]"))
|
||||
})
|
||||
var newBytes []byte
|
||||
index := 0
|
||||
for {
|
||||
loc := pattern.FindSubmatchIndex(scrubbedBytes[index:])
|
||||
if loc == nil {
|
||||
break
|
||||
}
|
||||
newBytes = append(newBytes, scrubbedBytes[index:index+loc[2]]...)
|
||||
newBytes = append(newBytes, []byte("[scrubbed]")...)
|
||||
index = index + loc[3]
|
||||
}
|
||||
scrubbedBytes = append(newBytes, scrubbedBytes[index:]...)
|
||||
}
|
||||
return scrubbedBytes
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue