-
Notifications
You must be signed in to change notification settings - Fork 2k
Go: more models for log.slog
#22006
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
+169
−0
Merged
Go: more models for log.slog
#22006
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 | ||
| --- | ||
| * Improved models for the `log/slog` package (Go 1.21+), including `*slog.Logger` methods, `With`/`WithGroup`, and `Attr`/`Value` helpers, improving coverage for the `go/log-injection` and `go/clear-text-logging` queries. |
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
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
2 changes: 2 additions & 0 deletions
2
go/ql/test/library-tests/semmle/go/frameworks/Slog/CONSISTENCY/DataFlowConsistency.expected
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,2 @@ | ||
| reverseRead | ||
| | test.go:114:21:114:33 | call to Group | Origin of readStep is missing a PostUpdateNode. | |
2 changes: 2 additions & 0 deletions
2
go/ql/test/library-tests/semmle/go/frameworks/Slog/TaintFlows.expected
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,2 @@ | ||
| invalidModelRow | ||
| testFailures |
14 changes: 14 additions & 0 deletions
14
go/ql/test/library-tests/semmle/go/frameworks/Slog/TaintFlows.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,14 @@ | ||
| import go | ||
| import semmle.go.dataflow.ExternalFlow | ||
| import ModelValidation | ||
| import utils.test.InlineFlowTest | ||
|
|
||
| module Config implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node source) { | ||
| source.(DataFlow::CallNode).getTarget().getName() = ["getUntrustedData", "getUntrustedString"] | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node sink) { sink = any(LoggerCall log).getAMessageComponent() } | ||
| } | ||
|
|
||
| import FlowTest<Config, Config> |
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 @@ | ||
| module codeql-go-tests/frameworks/slog | ||
|
|
||
| go 1.26 |
115 changes: 115 additions & 0 deletions
115
go/ql/test/library-tests/semmle/go/frameworks/Slog/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,115 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| func main() {} | ||
|
|
||
| func getUntrustedData() interface{} { return nil } | ||
|
|
||
| func getUntrustedString() string { | ||
| return "tainted string" | ||
| } | ||
|
|
||
| // Package-level convenience functions. | ||
|
|
||
| func testSlogDebug() { | ||
| slog.Debug(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.Debug("msg", "key", getUntrustedData()) // $ hasValueFlow="call to getUntrustedData" | ||
| slog.Debug("msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| func testSlogInfo() { | ||
| slog.Info(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.Info("msg", slog.Any("key", getUntrustedData())) // $ hasTaintFlow="call to Any" | ||
| slog.Info("msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| func testSlogWarn() { | ||
| slog.Warn(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.Warn("msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| func testSlogError() { | ||
| slog.Error(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.Error("msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| func testSlogContextVariants(ctx context.Context) { | ||
| slog.DebugContext(ctx, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.InfoContext(ctx, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.WarnContext(ctx, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.ErrorContext(ctx, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.InfoContext(ctx, "msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| func testSlogLog(ctx context.Context) { | ||
| slog.Log(ctx, slog.LevelInfo, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.Log(ctx, slog.LevelInfo, "msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| slog.LogAttrs(ctx, slog.LevelInfo, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| slog.LogAttrs(ctx, slog.LevelInfo, "msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| // Methods on *slog.Logger. | ||
|
|
||
| func testLoggerMethods(logger *slog.Logger, ctx context.Context) { | ||
| logger.Debug(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| logger.Info(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| logger.Warn(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| logger.Error(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| logger.Info("msg", slog.Any("key", getUntrustedData())) // $ hasTaintFlow="call to Any" | ||
| logger.InfoContext(ctx, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| logger.Log(ctx, slog.LevelInfo, getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| logger.LogAttrs(ctx, slog.LevelInfo, "msg", slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| } | ||
|
|
||
| // With, Logger.With and Logger.WithGroup. Note that for ease of modeling we make these functions | ||
| // sinks, although strictly speaking we should consider logging functions called on the returned | ||
| // loggers as the sinks. | ||
|
|
||
| func testWith(logger *slog.Logger) { | ||
| logger1 := logger.With(slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| logger1.Info("hello world") | ||
| logger2 := logger.With(slog.Any(getUntrustedString(), nil)) // $ hasTaintFlow="call to Any" | ||
| logger2.Info("hello world") | ||
| logger.With("key", getUntrustedData()).Info("hello world") // $ hasValueFlow="call to getUntrustedData" | ||
| } | ||
|
|
||
| func testPackageWith() { | ||
| logger := slog.With(slog.String("key", getUntrustedString())) // $ hasTaintFlow="call to String" | ||
| logger.Info("hello world") | ||
| slog.With("key", getUntrustedData()).Info("hello world") // $ hasValueFlow="call to getUntrustedData" | ||
| } | ||
|
|
||
| func testWithGroup(logger *slog.Logger) { | ||
| grouped := logger.WithGroup(getUntrustedString()) // $ hasValueFlow="call to getUntrustedString" | ||
| grouped.Info("hello world") | ||
| } | ||
|
|
||
| // Summary models: functions relating to Attr/Value that propagate strings. | ||
|
|
||
| func testAttrConstructors(logger *slog.Logger) { | ||
| logger.Info("msg", slog.Group("group", slog.String("key", getUntrustedString()))) // $ hasTaintFlow="call to Group" | ||
| logger.Info("msg", slog.GroupAttrs("group", slog.String("key", getUntrustedString()))) // $ hasTaintFlow="call to GroupAttrs" | ||
| } | ||
|
|
||
| func testValueConstructors(logger *slog.Logger) { | ||
| logger.Info("msg", "key", slog.AnyValue(getUntrustedString())) // $ hasTaintFlow="call to AnyValue" | ||
| logger.Info("msg", "key", slog.StringValue(getUntrustedString())) // $ hasTaintFlow="call to StringValue" | ||
| attr := slog.String("key", getUntrustedString()) | ||
| logger.Info("msg", "key", slog.GroupValue(attr)) // $ hasTaintFlow="call to GroupValue" | ||
| } | ||
|
|
||
| func testAttrAndValueAccessors(logger *slog.Logger) { | ||
| attr := slog.String("key", getUntrustedString()) | ||
| logger.Info("msg", "key", attr.String()) // $ hasTaintFlow="call to String" | ||
|
|
||
| v := slog.AnyValue(getUntrustedString()) | ||
| logger.Info("msg", "key", v.Any()) // $ hasTaintFlow="call to Any" | ||
| logger.Info("msg", "key", v.String()) // $ hasTaintFlow="call to String" | ||
|
|
||
| group := slog.GroupValue(slog.String("key", getUntrustedString())) | ||
| logger.Info("msg", group.Group()[0]) // $ hasTaintFlow="index expression" | ||
| } |
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.
While these function names do make them unique - is there a particular reason why we don't have
"Handler","Attr"and"Value"specified as the class?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.
Do you mean in the signature column? Go doesn't allow overloading, so there is never any need to specify the signature. The docs say it should always be empty.
Uh oh!
There was an error while loading. Please reload this page.
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.
No, I meant the second (the "type") column. We do have it in other entries, for example
["log/slog", "Attr", True, "String", "", "", "Argument[receiver]", "ReturnValue", "taint", "manual"]Is this because it's related to the "receiver"?