-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Go: Add flow sources for AWS Lambda function handlers #15373
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
Merged
atorralba
merged 4 commits into
github:main
from
atorralba:atorralba/go/aws-lambda-sources
Jan 19, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d7dbec
Go: Add flow sources for AWS Lambda function handlers
atorralba d3a9a5e
Update go/ql/lib/semmle/go/frameworks/AwsLambda.qll
atorralba 9a0fb39
Model StartWithContext
atorralba 8d6aa28
Update go/ql/lib/semmle/go/frameworks/AwsLambda.qll
atorralba 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 hidden or 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,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Support for flow sources in [AWS Lambda function handlers](https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html) has been added. |
This file contains hidden or 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,59 @@ | ||
/** | ||
* Provides classes for working with untrusted flow sources, sinks and taint propagators | ||
* from the `github.com/aws/aws-lambda-go/lambda` package. | ||
*/ | ||
|
||
import go | ||
|
||
/** A source of input data in an AWS Lambda. */ | ||
private class LambdaInput extends UntrustedFlowSource::Range { | ||
LambdaInput() { | ||
exists(Parameter p | p = this.asParameter() | | ||
p = any(HandlerFunction hf).getAParameter() and | ||
not p.getType().hasQualifiedName("context", "Context") and | ||
not p instanceof ReceiverVariable | ||
) | ||
} | ||
} | ||
|
||
private class HandlerFunction extends FuncDef { | ||
HandlerFunction() { | ||
exists(StartOrNewHandlerFunction f, DataFlow::Node handlerArg | | ||
f.getACall().getArgument(f.getHandlerArgPos()) = handlerArg | ||
| | ||
handlerArg = this.(FuncDecl).getFunction().getARead() or | ||
handlerArg = DataFlow::exprNode(this.(FuncLit)) | ||
) | ||
or | ||
this = any(Method m | m.implements(awsLambdaPkg(), "Handler", "Invoke")).getFuncDecl() | ||
or | ||
exists(DataFlow::TypeCastNode typeCast | | ||
typeCast.getResultType() instanceof HandlerImpl and | ||
this.(FuncDecl).getFunction().getARead() = typeCast.getOperand() | ||
) | ||
} | ||
} | ||
|
||
private class StartOrNewHandlerFunction extends Function { | ||
int handlerArgPos; | ||
|
||
StartOrNewHandlerFunction() { | ||
this.hasQualifiedName(awsLambdaPkg(), | ||
[ | ||
"Start", "StartHandler", "StartHandlerFunc", "StartWithOptions", "NewHandler", | ||
"NewHandlerWithOptions" | ||
]) and | ||
handlerArgPos = 0 | ||
or | ||
this.hasQualifiedName(awsLambdaPkg(), ["StartHandlerWithContext", "StartWithContext"]) and | ||
handlerArgPos = 1 | ||
} | ||
|
||
int getHandlerArgPos() { result = handlerArgPos } | ||
} | ||
|
||
private class HandlerImpl extends Type { | ||
HandlerImpl() { this.implements(awsLambdaPkg(), "Handler") } | ||
} | ||
|
||
private string awsLambdaPkg() { result = "github.com/aws/aws-lambda-go/lambda" } |
5 changes: 5 additions & 0 deletions
5
go/ql/test/library-tests/semmle/go/frameworks/AwsLambda/go.mod
This file contains hidden or 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,5 @@ | ||
module AwsLambda | ||
|
||
go 1.21 | ||
|
||
require github.com/aws/aws-lambda-go v1.44.0 |
Empty file.
220 changes: 220 additions & 0 deletions
220
go/ql/test/library-tests/semmle/go/frameworks/AwsLambda/test.go
This file contains hidden or 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,220 @@ | ||
package test | ||
|
||
//go:generate depstubber -vendor github.com/aws/aws-lambda-go/lambda Handler,HandlerFunc,Option Start,StartHandler,StartHandlerFunc,StartHandlerWithContext,NewHandler | ||
// FIXME: ^ this currently fails for generic types and functions, like HandlerFunc or StartHandlerFunc. Stubs have been manually created for now. | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
) | ||
|
||
type Event struct { | ||
Name string `json:""` | ||
Age int `json:""` | ||
} | ||
|
||
type Response struct { | ||
Message string `json:""` | ||
} | ||
|
||
func sink(sink interface{}) {} | ||
|
||
// func (TIn) | ||
func Handler0(event *Event) { | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
} | ||
|
||
// func (TIn) error | ||
func Handler1(event *Event) error { | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return nil | ||
} | ||
|
||
// func (TIn) (TOut, error) | ||
func Handler2(event *Event) (*Response, error) { | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
// func (context.Context) | ||
func Handler3(ctx context.Context) { | ||
sink(ctx) // safe | ||
} | ||
|
||
// func (context.Context) error | ||
func Handler4(ctx context.Context) error { | ||
sink(ctx) // safe | ||
return nil | ||
} | ||
|
||
// func (context.Context) (TOut, error) | ||
func Handler5(ctx context.Context) (*Response, error) { | ||
sink(ctx) // safe | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
// func (context.Context, TIn) | ||
func Handler6(ctx context.Context, event Event) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
} | ||
|
||
// func (context.Context, TIn) error | ||
func Handler7(ctx context.Context, event Event) error { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return nil | ||
} | ||
|
||
// func (context.Context, TIn) (TOut, error) | ||
func Handler8(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
type MyHandlerFunc func(context.Context, []byte) ([]byte, error) | ||
|
||
func (f MyHandlerFunc) Invoke(ctx context.Context, payload []byte) ([]byte, error) { | ||
return f(ctx, payload) | ||
} | ||
|
||
func Handler9(ctx context.Context, payload []byte) ([]byte, error) { | ||
sink(payload) // $ hasTaintFlow="payload" | ||
return payload, nil | ||
} | ||
|
||
type Handler10 struct{} | ||
|
||
func (h *Handler10) Invoke(ctx context.Context, payload []byte) ([]byte, error) { | ||
sink(payload) // $ hasTaintFlow="payload" | ||
return payload, nil | ||
} | ||
|
||
func Handler11(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
func Handler12(ctx context.Context, payload []byte) ([]byte, error) { | ||
sink(payload) // $ hasTaintFlow="payload" | ||
return payload, nil | ||
} | ||
|
||
type Handler13 struct{} | ||
|
||
func (h *Handler13) Invoke(ctx context.Context, payload []byte) ([]byte, error) { | ||
sink(payload) // $ hasTaintFlow="payload" | ||
return payload, nil | ||
} | ||
|
||
func Handler14(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
func Handler15(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
func Handler16(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
func Handler17(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(Handler0) | ||
lambda.Start(Handler1) | ||
lambda.Start(Handler2) | ||
lambda.Start(Handler3) | ||
lambda.Start(Handler4) | ||
lambda.Start(Handler5) | ||
lambda.Start(Handler6) | ||
lambda.Start(Handler7) | ||
lambda.Start(Handler8) | ||
lambda.Start(func(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
}) | ||
lambda.StartHandler(MyHandlerFunc(Handler9)) | ||
lambda.StartHandler(&Handler10{}) | ||
lambda.StartHandlerFunc(Handler11) | ||
lambda.StartHandlerFunc(func(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
}) | ||
lambda.StartHandlerWithContext(context.Background(), MyHandlerFunc(Handler12)) | ||
lambda.StartHandlerWithContext(context.Background(), &Handler13{}) | ||
lambda.StartWithContext(context.Background(), Handler14) | ||
lambda.StartWithContext(context.Background(), func(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
}) | ||
lambda.StartWithOptions(Handler15) | ||
lambda.StartWithOptions(func(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
}) | ||
lambda.NewHandler(Handler16) | ||
lambda.NewHandler(func(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
}) | ||
lambda.NewHandlerWithOptions(Handler17) | ||
lambda.NewHandlerWithOptions(func(ctx context.Context, event Event) (*Response, error) { | ||
sink(ctx) // safe | ||
sink(event.Name) // $ hasTaintFlow="selection of Name" | ||
sink(event.Age) // $ hasTaintFlow="selection of Age" | ||
sink(event) // $ hasTaintFlow="event" | ||
return &Response{Message: ""}, nil | ||
}) | ||
} |
13 changes: 13 additions & 0 deletions
13
go/ql/test/library-tests/semmle/go/frameworks/AwsLambda/test.ql
This file contains hidden or 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,13 @@ | ||
import go | ||
import semmle.go.frameworks.AwsLambda | ||
import TestUtilities.InlineFlowTest | ||
|
||
module Config implements DataFlow::ConfigSig { | ||
predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource } | ||
|
||
predicate isSink(DataFlow::Node sink) { | ||
exists(Function fn | fn.hasQualifiedName(_, "sink") | sink = fn.getACall().getAnArgument()) | ||
} | ||
} | ||
|
||
import TaintFlowTest<Config> |
38 changes: 38 additions & 0 deletions
38
...y-tests/semmle/go/frameworks/AwsLambda/vendor/github.com/aws/aws-lambda-go/lambda/stub.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
go/ql/test/library-tests/semmle/go/frameworks/AwsLambda/vendor/modules.txt
This file contains hidden or 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,3 @@ | ||
# github.com/aws/aws-lambda-go v1.44.0 | ||
## explicit | ||
github.com/aws/aws-lambda-go |
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.
For now, I'm not considering any part of the
Context
as tainted (a more thorough analysis in the future may be worthwhile to make sure).