Skip to content

Commit 00427d2

Browse files
sauyonclaude
andcommitted
Go: Model log/slog as a logging sink
The standard-library structured logger `log/slog` (Go 1.21+) was not modeled, so `go/log-injection` and `go/clear-text-logging` were blind to any code that logs through it. Model its logging functions and `*slog.Logger` methods — `Debug`, `Info`, `Warn`, `Error`, their `Context` variants, and `Log`/`LogAttrs` — as `log-injection` sinks (the kind that feeds `LoggerCall`, powering both queries). Adds `log/slog` cases to the `LoggerCall` library test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e618883 commit 00427d2

5 files changed

Lines changed: 79 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added models for the `log/slog` package (Go 1.21+). Its logging functions and
5+
`*slog.Logger` methods (`Debug`/`Info`/`Warn`/`Error`, their `Context`
6+
variants, and `Log`/`LogAttrs`) are now recognized as logging sinks, so the
7+
`go/log-injection` and `go/clear-text-logging` queries cover code that logs
8+
through `slog`.

go/ql/lib/ext/log.slog.model.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: sinkModel
5+
data:
6+
# Package-level convenience functions (msg string, args ...any).
7+
- ["log/slog", "", False, "Debug", "", "", "Argument[0..1]", "log-injection", "manual"]
8+
- ["log/slog", "", False, "Info", "", "", "Argument[0..1]", "log-injection", "manual"]
9+
- ["log/slog", "", False, "Warn", "", "", "Argument[0..1]", "log-injection", "manual"]
10+
- ["log/slog", "", False, "Error", "", "", "Argument[0..1]", "log-injection", "manual"]
11+
# Context variants (ctx, msg string, args ...any).
12+
- ["log/slog", "", False, "DebugContext", "", "", "Argument[1..2]", "log-injection", "manual"]
13+
- ["log/slog", "", False, "InfoContext", "", "", "Argument[1..2]", "log-injection", "manual"]
14+
- ["log/slog", "", False, "WarnContext", "", "", "Argument[1..2]", "log-injection", "manual"]
15+
- ["log/slog", "", False, "ErrorContext", "", "", "Argument[1..2]", "log-injection", "manual"]
16+
# Log/LogAttrs (ctx, level, msg string, args/attrs ...).
17+
- ["log/slog", "", False, "Log", "", "", "Argument[2..3]", "log-injection", "manual"]
18+
- ["log/slog", "", False, "LogAttrs", "", "", "Argument[2..3]", "log-injection", "manual"]
19+
# Methods on *slog.Logger.
20+
- ["log/slog", "Logger", True, "Debug", "", "", "Argument[0..1]", "log-injection", "manual"]
21+
- ["log/slog", "Logger", True, "Info", "", "", "Argument[0..1]", "log-injection", "manual"]
22+
- ["log/slog", "Logger", True, "Warn", "", "", "Argument[0..1]", "log-injection", "manual"]
23+
- ["log/slog", "Logger", True, "Error", "", "", "Argument[0..1]", "log-injection", "manual"]
24+
- ["log/slog", "Logger", True, "DebugContext", "", "", "Argument[1..2]", "log-injection", "manual"]
25+
- ["log/slog", "Logger", True, "InfoContext", "", "", "Argument[1..2]", "log-injection", "manual"]
26+
- ["log/slog", "Logger", True, "WarnContext", "", "", "Argument[1..2]", "log-injection", "manual"]
27+
- ["log/slog", "Logger", True, "ErrorContext", "", "", "Argument[1..2]", "log-injection", "manual"]
28+
- ["log/slog", "Logger", True, "Log", "", "", "Argument[2..3]", "log-injection", "manual"]
29+
- ["log/slog", "Logger", True, "LogAttrs", "", "", "Argument[2..3]", "log-injection", "manual"]

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module codeql-go-tests/concepts/loggercall
22

3-
go 1.15
3+
go 1.21
44

55
require (
66
github.com/golang/glog v1.2.5

go/ql/test/library-tests/semmle/go/concepts/LoggerCall/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
const fmt = "formatted %s string"
44
const text = "test"
5+
const key = "key"
56

67
var v []byte
78

89
func main() {
910
glogTest(len(v))
1011
stdlib()
12+
slogTest()
1113
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
)
7+
8+
func slogTest() {
9+
ctx := context.Background()
10+
var logger *slog.Logger
11+
12+
// Methods on *slog.Logger: Debug/Info/Warn/Error(msg string, args ...any).
13+
logger.Debug(text) // $ logger=text
14+
logger.Info(text) // $ logger=text
15+
logger.Warn(text) // $ logger=text
16+
logger.Error(text) // $ logger=text
17+
logger.Info(text, key, v) // $ logger=text logger=key logger=v
18+
19+
// Context variants: (ctx, msg string, args ...any).
20+
logger.DebugContext(ctx, text) // $ logger=text
21+
logger.InfoContext(ctx, text) // $ logger=text
22+
logger.WarnContext(ctx, text) // $ logger=text
23+
logger.ErrorContext(ctx, text) // $ logger=text
24+
logger.InfoContext(ctx, text, key, v) // $ logger=text logger=key logger=v
25+
26+
// Log/LogAttrs: (ctx, level, msg string, args/attrs ...).
27+
logger.Log(ctx, slog.LevelInfo, text, key, v) // $ logger=text logger=key logger=v
28+
logger.LogAttrs(ctx, slog.LevelInfo, text) // $ logger=text
29+
30+
// Package-level convenience functions.
31+
slog.Debug(text) // $ logger=text
32+
slog.Info(text) // $ logger=text
33+
slog.Warn(text) // $ logger=text
34+
slog.Error(text) // $ logger=text
35+
slog.Info(text, key, v) // $ logger=text logger=key logger=v
36+
slog.InfoContext(ctx, text, key, v) // $ logger=text logger=key logger=v
37+
slog.Log(ctx, slog.LevelInfo, text) // $ logger=text
38+
slog.LogAttrs(ctx, slog.LevelInfo, text) // $ logger=text
39+
}

0 commit comments

Comments
 (0)