-
-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for case sensitive args #1059
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20c4076
feat: add support for case sensitive args
fzipi 4225223
Merge branch 'main' into change-args-names-to-be-case-sensitive
fzipi 3da3bcd
Merge branch 'main' into change-args-names-to-be-case-sensitive
jcchavezs cf7e286
Change args names to be case sensitive tag (#1065)
jcchavezs cf5f8de
Merge branch 'main' into change-args-names-to-be-case-sensitive
fzipi 1dd665a
Update README with new build tag
M4tteoP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build coraza.rule.case_sensitive_args_keys | ||
|
||
package corazawaf | ||
|
||
var shouldUseCaseSensitiveNamedCollection = true |
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,8 @@ | ||
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build !coraza.rule.case_sensitive_args_keys | ||
|
||
package corazawaf | ||
|
||
var shouldUseCaseSensitiveNamedCollection = false |
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 @@ | ||
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build coraza.rule.case_sensitive_args_keys | ||
|
||
package corazawaf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/corazawaf/coraza/v3/types/variables" | ||
) | ||
|
||
func TestCaseSensitiveArgsVariableKeys(t *testing.T) { | ||
rule := NewRule() | ||
if err := rule.AddVariable(variables.ArgsGet, "Som3ThinG", false); err != nil { | ||
t.Error(err) | ||
} | ||
if rule.variables[0].KeyStr != "Som3ThinG" { | ||
t.Error("variable key is not case insensitive") | ||
} | ||
} |
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,133 @@ | ||
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build coraza.rule.case_sensitive_args_keys | ||
|
||
package seclang | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/corazawaf/coraza/v3/internal/corazawaf" | ||
) | ||
|
||
func TestCaseSensitiveRuleMatchRegex(t *testing.T) { | ||
waf := corazawaf.NewWAF() | ||
parser := NewParser(waf) | ||
err := parser.FromString(` | ||
SecRuleEngine On | ||
SecRule ARGS:/^Key/ "@streq my-value" "id:1028,phase:1,deny,status:403,msg:'ARGS:key matched.'" | ||
`) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
tx := waf.NewTransaction() | ||
tx.ProcessURI("https://asdf.com/index.php?t1=aaa&T1=zzz&t2=bbb&t3=ccc&Keyless=my-value&a=test&jsessionid=74B0CB414BD77D17B5680A6386EF1666", "GET", "HTTP/1.1") | ||
tx.ProcessConnection("127.0.0.1", 0, "", 0) | ||
tx.ProcessRequestHeaders() | ||
if len(tx.MatchedRules()) != 1 { | ||
t.Errorf("failed to match rules with %d", len(tx.MatchedRules())) | ||
} | ||
if tx.Interruption() == nil { | ||
t.Fatal("failed to interrupt transaction") | ||
} | ||
} | ||
|
||
func TestCaseSensitiveArguments(t *testing.T) { | ||
waf := corazawaf.NewWAF() | ||
rules := `SecRule ARGS:Test1 "Xyz" "id:3, phase:2, log, deny"` | ||
parser := NewParser(waf) | ||
|
||
err := parser.FromString(rules) | ||
if err != nil { | ||
t.Error() | ||
return | ||
} | ||
|
||
tx := waf.NewTransaction() | ||
tx.ProcessRequestHeaders() | ||
tx.AddPostRequestArgument("Test1", "Xyz") | ||
it, err := tx.ProcessRequestBody() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if it == nil { | ||
t.Errorf("failed to test arguments value match: Same case argument name, %+v\n", tx.MatchedRules()) | ||
} | ||
|
||
tx = waf.NewTransaction() | ||
tx.ProcessRequestHeaders() | ||
tx.AddPostRequestArgument("TEST1", "Xyz") | ||
it, err = tx.ProcessRequestBody() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if it != nil { | ||
t.Errorf("failed to test arguments value match: argument is matching a different case, %+v\n", tx.MatchedRules()) | ||
} | ||
|
||
tx = waf.NewTransaction() | ||
tx.ProcessRequestHeaders() | ||
tx.AddPostRequestArgument("Test1", "XYZ") | ||
it, err = tx.ProcessRequestBody() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if it != nil { | ||
t.Errorf("failed to test arguments value match: argument is matching a different case, %+v\n", tx.MatchedRules()) | ||
} | ||
} | ||
|
||
func TestCaseSensitiveURIQueryParam(t *testing.T) { | ||
waf := corazawaf.NewWAF() | ||
rules := `SecRule ARGS:Test1 "@contains SQLI" "id:3, phase:2, log, pass"` | ||
parser := NewParser(waf) | ||
|
||
err := parser.FromString(rules) | ||
if err != nil { | ||
t.Error() | ||
return | ||
} | ||
|
||
tx := waf.NewTransaction() | ||
tx.ProcessURI("/url?Test1='SQLI", "POST", "HTTP/1.1") | ||
tx.ProcessRequestHeaders() | ||
_, err = tx.ProcessRequestBody() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if len(tx.MatchedRules()) == 1 { | ||
if len(tx.MatchedRules()[0].MatchedDatas()) != 1 { | ||
t.Errorf("failed to test uri query param. Found matches: %d, %+v\n", | ||
len(tx.MatchedRules()[0].MatchedDatas()), tx.MatchedRules()) | ||
} | ||
if !isMatchData(tx.MatchedRules()[0].MatchedDatas(), "Test1") { | ||
t.Error("Key did not match: Test1 !=", tx.MatchedRules()[0]) | ||
} | ||
} else { | ||
t.Errorf("failed to test uri query param: Same case arg name: %d, %+v\n", | ||
len(tx.MatchedRules()), tx.MatchedRules()) | ||
} | ||
|
||
tx = waf.NewTransaction() | ||
tx.ProcessURI("/test?test1='SQLI&Test1='SQLI&TEST1='SQLI", "POST", "HTTP/1.1") | ||
tx.ProcessRequestHeaders() | ||
_, err = tx.ProcessRequestBody() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if len(tx.MatchedRules()) == 1 { | ||
if len(tx.MatchedRules()[0].MatchedDatas()) != 1 { | ||
t.Errorf("failed to test uri query param. Found matches: %d, %+v\n", | ||
len(tx.MatchedRules()[0].MatchedDatas()), tx.MatchedRules()) | ||
} | ||
if !isMatchData(tx.MatchedRules()[0].MatchedDatas(), "Test1") { | ||
t.Error("Key did not match: Test1 !=", tx.MatchedRules()[0]) | ||
} | ||
} else { | ||
t.Errorf("failed to test qparam pollution: Multiple arg different case: %d, %+v\n", | ||
len(tx.MatchedRules()), tx.MatchedRules()) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it deserves a line in the README under the
Build tags
sectionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like the following?
if 👍 I would push the line and merge it