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

serverless/appsec: fix handling of ALB headers & query string #22005

Merged
merged 5 commits into from
Jan 16, 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
31 changes: 28 additions & 3 deletions pkg/serverless/appsec/httpsec/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func (lp *ProxyLifecycleProcessor) OnInvokeStart(startDetails *invocationlifecyc
eventType := trigger.GetEventType(lowercaseEventPayload)
if eventType == trigger.Unknown {
log.Debugf("appsec: proxy-lifecycle: Failed to extract event type")
} else {
log.Debugf("appsec: proxy-lifecycle: Extracted event type: %v", eventType)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be helpful when diagnosing future cases, as this line makes it very easy to rule out the "failed to identify payload" scenario (presence of positive evidence is easier to reason about than absence of negative evidence).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this is a good addition. Ideally at some point we should add a String() method to the event type. Right now, if I recall correctly, it will just print a number.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg, great minds think alike, I just scrolled down and saw the String() method you added! 🎉

}

var event interface{}
Expand Down Expand Up @@ -206,14 +208,24 @@ func (lp *ProxyLifecycleProcessor) spanModifier(lastReqId string, chunk *pb.Trac
)

case *events.ALBTargetGroupRequest:
var sourceIp string
// ALB is a "trusted" origin, so we extract the source IP from the X-Forwarded-For header.
// ALB also provides headers with fully lower-case names, so we don't have to worry about case.
if xff, found := event.MultiValueHeaders["x-forwarded-for"]; found && len(xff) > 0 {
sourceIp = xff[0]
} else if xff, found := event.Headers["x-forwarded-for"]; found {
sourceIp = xff
}
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
makeContext(
&ctx,
nil,
&event.Path,
event.MultiValueHeaders,
event.MultiValueQueryStringParameters,
// Depending on how the ALB is configured, headers will be either in MultiValueHeaders or Headers (not both).
multiOrSingle(event.MultiValueHeaders, event.Headers),
// Depending on how the ALB is configured, query parameters will be either in MultiValueQueryStringParameters or QueryStringParameters (not both).
multiOrSingle(event.MultiValueQueryStringParameters, event.QueryStringParameters),
nil,
"",
sourceIp,
&event.Body,
event.IsBase64Encoded,
)
Expand Down Expand Up @@ -258,6 +270,19 @@ func (lp *ProxyLifecycleProcessor) spanModifier(lastReqId string, chunk *pb.Trac
}
}

// multiOrSingle picks the first non-nil map, and returns the content formatted
// as the multi-map.
func multiOrSingle(multi map[string][]string, single map[string]string) map[string][]string {
if multi == nil && single != nil {
// There is no multi-map, but there is a single-map, so we'll make a multi-map out of that.
multi = make(map[string][]string, len(single))
for key, value := range single {
multi[key] = []string{value}
}
}
return multi
}

//nolint:revive // TODO(ASM) Fix revive linter
type ExecutionContext interface {
LastRequestID() string
Expand Down
46 changes: 46 additions & 0 deletions pkg/serverless/trigger/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package trigger

import (
"fmt"
"strings"

jsonEncoder "github.com/json-iterator/go"
Expand Down Expand Up @@ -294,3 +295,48 @@ func eventRecordsKeyEquals(event map[string]any, key string, val string) bool {
}
return false
}

func (et AWSEventType) String() string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only there to make the debug log of detection easier to read/reason about.

switch et {
case Unknown:
return "Unknown"
case APIGatewayEvent:
return "APIGatewayEvent"
case APIGatewayV2Event:
return "APIGatewayV2Event"
case APIGatewayWebsocketEvent:
return "APIGatewayWebsocketEvent"
case APIGatewayLambdaAuthorizerTokenEvent:
return "APIGatewayLambdaAuthorizerTokenEvent"
case APIGatewayLambdaAuthorizerRequestParametersEvent:
return "APIGatewayLambdaAuthorizerRequestParametersEvent"
case ALBEvent:
return "ALBEvent"
case CloudWatchEvent:
return "CloudWatchEvent"
case CloudWatchLogsEvent:
return "CloudWatchLogsEvent"
case CloudFrontRequestEvent:
return "CloudFrontRequestEvent"
case DynamoDBStreamEvent:
return "DynamoDBStreamEvent"
case KinesisStreamEvent:
return "KinesisStreamEvent"
case S3Event:
return "S3Event"
case SNSEvent:
return "SNSEvent"
case SQSEvent:
return "SQSEvent"
case SNSSQSEvent:
return "SNSSQSEvent"
case AppSyncResolverEvent:
return "AppSyncResolverEvent"
case EventBridgeEvent:
return "EventBridgeEvent"
case LambdaFunctionURLEvent:
return "LambdaFunctionURLEvent"
default:
return fmt.Sprintf("EventType(%d)", et)
}
}
Loading