-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_url_encoding.go
78 lines (65 loc) · 1.73 KB
/
validate_url_encoding.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
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
//go:build !coraza.disabled_operators.validateUrlEncoding
package operators
import (
"github.com/appsentinels/coraza/v3/experimental/plugins/plugintypes"
)
type validateURLEncoding struct{}
var _ plugintypes.Operator = (*validateURLEncoding)(nil)
func newValidateURLEncoding(plugintypes.OperatorOptions) (plugintypes.Operator, error) {
return &validateURLEncoding{}, nil
}
func (o *validateURLEncoding) Evaluate(_ plugintypes.TransactionState, value string) bool {
if len(value) == 0 {
return false
}
rc := validateURLEncodingInternal(value, len(value))
switch rc {
case 1:
/* Encoding is valid */
return false
case -2:
// Invalid URL Encoding: Non-hexadecimal
return true
case -3:
// Invalid URL Encoding: Not enough characters at the end of input
return true
case -1:
default:
// Invalid URL Encoding: Internal error
return true
}
return true
}
func validateURLEncodingInternal(input string, inputLen int) int {
if inputLen == 0 {
return -1
}
var i int
for i < inputLen {
if input[i] == '%' {
if i+2 >= inputLen {
/* Not enough bytes. */
return -3
}
/* Here we only decode a %xx combination if it is valid,
* leaving it as is otherwise.
*/
c1 := input[i+1]
c2 := input[i+2]
if (((c1 >= '0') && (c1 <= '9')) || ((c1 >= 'a') && (c1 <= 'f')) || ((c1 >= 'A') && (c1 <= 'F'))) && (((c2 >= '0') && (c2 <= '9')) || ((c2 >= 'a') && (c2 <= 'f')) || ((c2 >= 'A') && (c2 <= 'F'))) {
i += 3
} else {
/* Non-hexadecimal characters used in encoding. */
return -2
}
} else {
i++
}
}
return 1
}
func init() {
Register("validateUrlEncoding", newValidateURLEncoding)
}