-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Describe Request URL escape and unescape normalier. Fixes #110 # Change Type New normalizer.
- Loading branch information
Showing
8 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# URL Escape Normalizer | ||
|
||
The `url-escape` normalizer uses [net.url.QueryEscape](https://pkg.go.dev/net/url#QueryEscape) to escape the string so it can be safely placed inside a URL query. | ||
|
||
```golang | ||
type Request struct { | ||
Query string `checkers:"url-escape"` | ||
} | ||
|
||
request := &Request{ | ||
Query: "param1/param2 = 1 + 2 & 3 + 4", | ||
} | ||
|
||
_, valid := checker.Check(request) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
// Outputs: | ||
// param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4 | ||
fmt.Println(request.Query) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# URL Unescape Normalizer | ||
|
||
The `url-unescape` normalizer uses [net.url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape) to converte each 3-byte encoded substring of the form "%AB" into the hex-decoded byte 0xAB. | ||
|
||
```golang | ||
type Request struct { | ||
Query string `checkers:"url-unescape"` | ||
} | ||
|
||
request := &Request{ | ||
Query: "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4", | ||
} | ||
|
||
_, valid := checker.Check(request) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
if request.Query != "param1/param2 = 1 + 2 & 3 + 4" { | ||
t.Fail() | ||
} | ||
|
||
// Outputs: | ||
// param1/param2 = 1 + 2 & 3 + 4 | ||
fmt.Println(comment.Body) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package checker | ||
|
||
import ( | ||
"net/url" | ||
"reflect" | ||
) | ||
|
||
// NormalizerURLEscape is the name of the normalizer. | ||
const NormalizerURLEscape = "url-escape" | ||
|
||
// makeURLEscape makes a normalizer function for the URL escape normalizer. | ||
func makeURLEscape(_ string) CheckFunc { | ||
return normalizeURLEscape | ||
} | ||
|
||
// normalizeURLEscape applies URL escaping to special characters. | ||
// Uses net.url.QueryEscape for the actual escape operation. | ||
func normalizeURLEscape(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
value.SetString(url.QueryEscape(value.String())) | ||
|
||
return ResultValid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package checker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/checker" | ||
) | ||
|
||
func TestNormalizeURLEscapeNonString(t *testing.T) { | ||
defer checker.FailIfNoPanic(t) | ||
|
||
type Request struct { | ||
Query int `checkers:"url-escape"` | ||
} | ||
|
||
request := &Request{} | ||
|
||
checker.Check(request) | ||
} | ||
|
||
func TestNormalizeURLEscape(t *testing.T) { | ||
type Request struct { | ||
Query string `checkers:"url-escape"` | ||
} | ||
|
||
request := &Request{ | ||
Query: "param1/param2 = 1 + 2 & 3 + 4", | ||
} | ||
|
||
_, valid := checker.Check(request) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
if request.Query != "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4" { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package checker | ||
|
||
import ( | ||
"net/url" | ||
"reflect" | ||
) | ||
|
||
// NormalizerURLUnescape is the name of the normalizer. | ||
const NormalizerURLUnescape = "url-unescape" | ||
|
||
// makeURLUnescape makes a normalizer function for the URL unscape normalizer. | ||
func makeURLUnescape(_ string) CheckFunc { | ||
return normalizeURLUnescape | ||
} | ||
|
||
// normalizeURLUnescape applies URL unescaping to special characters. | ||
// Uses url.QueryUnescape for the actual unescape operation. | ||
func normalizeURLUnescape(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
unescaped, err := url.QueryUnescape(value.String()) | ||
if err == nil { | ||
value.SetString(unescaped) | ||
} | ||
|
||
return ResultValid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package checker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/checker" | ||
) | ||
|
||
func TestNormalizeURLUnescapeNonString(t *testing.T) { | ||
defer checker.FailIfNoPanic(t) | ||
|
||
type Request struct { | ||
Query int `checkers:"url-unescape"` | ||
} | ||
|
||
request := &Request{} | ||
|
||
checker.Check(request) | ||
} | ||
|
||
func TestNormalizeURLUnescape(t *testing.T) { | ||
type Request struct { | ||
Query string `checkers:"url-unescape"` | ||
} | ||
|
||
request := &Request{ | ||
Query: "param1%2Fparam2+%3D+1+%2B+2+%26+3+%2B+4", | ||
} | ||
|
||
_, valid := checker.Check(request) | ||
if !valid { | ||
t.Fail() | ||
} | ||
|
||
if request.Query != "param1/param2 = 1 + 2 & 3 + 4" { | ||
t.Fail() | ||
} | ||
} |