Skip to content
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

fix: redirect action status codes #1183

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/actions/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ func (a *redirectFn) Init(_ plugintypes.RuleMetadata, data string) error {
}

func (a *redirectFn) Evaluate(r plugintypes.RuleMetadata, tx plugintypes.TransactionState) {
status := 302 // default status code for redirection
rid := r.ID()
if rid == noID {
rid = r.ParentID()
}
rstatus := r.Status()
if rstatus == 301 || rstatus == 302 || rstatus == 303 || rstatus == 307 {
fzipi marked this conversation as resolved.
Show resolved Hide resolved
status = rstatus
}
tx.Interrupt(&types.Interruption{
Status: r.Status(),
Status: status,
RuleID: rid,
Action: "redirect",
Data: a.target,
Expand Down
70 changes: 70 additions & 0 deletions testing/engine/disruptive_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,71 @@ var _ = profile.RegisterProfile(profile.Profile{
},
},
},
// Phase 2
{
Stage: profile.SubStage{
Input: profile.StageInput{
URI: "/redirect2",
},
Output: profile.ExpectedOutput{
TriggeredRules: []int{21},
Interruption: &profile.ExpectedInterruption{
Status: 302,
Data: "https://www.example.com",
RuleID: 21,
Action: "redirect",
},
},
},
},
{
Stage: profile.SubStage{
Input: profile.StageInput{
URI: "/redirect6",
},
Output: profile.ExpectedOutput{
TriggeredRules: []int{61},
Interruption: &profile.ExpectedInterruption{
Status: 302,
Data: "https://www.example.com",
RuleID: 61,
Action: "redirect",
},
},
},
},
{
Stage: profile.SubStage{
Input: profile.StageInput{
URI: "/redirect7",
},
Output: profile.ExpectedOutput{
TriggeredRules: []int{62},
Interruption: &profile.ExpectedInterruption{
Status: 307,
Data: "https://www.example.com",
RuleID: 62,
Action: "redirect",
},
},
},
},
{
Stage: profile.SubStage{
Input: profile.StageInput{
URI: "/redirect8",
},
Output: profile.ExpectedOutput{
TriggeredRules: []int{63},
Interruption: &profile.ExpectedInterruption{
Status: 302,
Data: "https://www.example.com",
RuleID: 63,
Action: "redirect",
},
},
},
},
{
Stage: profile.SubStage{
Input: profile.StageInput{
Expand Down Expand Up @@ -305,6 +370,11 @@ SecRule REQUEST_URI "/redirect5$" "phase:5,id:51,log,status:302,redirect:https:/
SecRule REQUEST_URI "/deny5$" "phase:5,id:52,log,status:500,deny"
SecRule REQUEST_URI "/drop5$" "phase:5,id:53,log,drop"

SecRule REQUEST_URI "/redirect6$" "phase:2,id:61,log,redirect:https://www.example.com"
SecRule REQUEST_URI "/redirect7$" "phase:2,id:62,log,status:307,redirect:https://www.example.com"
SecRule REQUEST_URI "/redirect8$" "phase:2,id:63,log,status:401,redirect:https://www.example.com"


# Rule 103 is missing the phase, therefore phase:2 is implicitly applied with its related default actions
# So we will expect a deny with 501 response for the blocking action.
SecDefaultAction "phase:2,deny,status:501,log"
Expand Down
Loading