-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsemgrep.yml
More file actions
201 lines (189 loc) · 6.71 KB
/
semgrep.yml
File metadata and controls
201 lines (189 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright (C) 2025 l3montree GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
rules:
- id: missing-error-message
languages:
- go
severity: ERROR
message: |
Missing error message or forwarded error message from internal error. Use: echo.NewHTTPError(..., "...").
Do not concatenate the error message directly. Use fmt.Sprintf(...) instead. For example: echo.NewHTTPError(400, fmt.Sprintf("could not process request: %s", err.Error()))
patterns:
- pattern: echo.NewHTTPError(...)
- pattern-not: echo.NewHTTPError(..., "...")
- pattern-not: echo.NewHTTPError(..., fmt.Sprintf(...))
- id: no-internal-error
languages:
- go
severity: ERROR
message: |
Provide internal error for better logging. Use: echo.NewHTTPError(...).WithInternal(err)
patterns:
- pattern: echo.NewHTTPError(500)
- pattern-not-inside: |
echo.NewHTTPError(500).WithInternal(...)
- id: repo-method-missing-ctx
message: Repository method should have ctx context.Context as first parameter
severity: WARNING
languages: [go]
paths:
include:
- "**/database/repositories/**"
patterns:
- pattern: func ($R $T) $METHOD(...) { ... }
- pattern-not: func ($R $T) $METHOD(ctx context.Context, ...) { ... }
fix-regex:
regex: '(\) \w+\()(?!\))'
replacement: '\1ctx context.Context, '
count: 1
- id: repo-method-missing-ctx-empty-params
message: Repository method should have ctx context.Context as first parameter
severity: WARNING
languages: [go]
paths:
include:
- "**/database/repositories/**"
patterns:
- pattern: func ($R $T) $METHOD() { ... }
fix-regex:
regex: '(\) \w+\()\)'
replacement: '\1ctx context.Context)'
count: 1
- id: repo-direct-db-access
message: Use GetDB(ctx, tx) instead of accessing the db field directly
severity: WARNING
languages: [go]
paths:
include:
- "**/database/repositories/**"
exclude:
- "**/database/repositories/gorm_repository.go"
patterns:
- pattern: $R.db
- pattern-not-inside: func ($R $T) GetDB(...) { ... }
- id: repo-getdb-nil-when-tx-available
message: tx is available as parameter, use GetDB(ctx, tx) instead of GetDB(ctx, nil)
severity: WARNING
languages: [go]
paths:
include:
- "**/database/repositories/**"
patterns:
- pattern-inside: func ($RECV $TYPE) $METHOD(ctx context.Context, tx *gorm.DB, ...) { ... }
- pattern: $R.GetDB(ctx, nil)
- id: repo-method-missing-tx
message: Repository method should have tx *gorm.DB as second parameter after ctx
severity: WARNING
languages: [go]
paths:
include:
- "**/database/repositories/**"
exclude:
- "**/database/repositories/gorm_repository.go"
patterns:
- pattern: func ($R $T) $METHOD(ctx context.Context, $A $B, ...) { ... }
- pattern-not: func ($R $T) $METHOD(ctx context.Context, tx *gorm.DB, ...) { ... }
- id: service-method-missing-ctx
message: Service method should have ctx context.Context as first parameter
severity: WARNING
languages: [go]
paths:
include:
- "**/services/**"
patterns:
- pattern: func ($R $T) $METHOD(...) { ... }
- pattern-not: func ($R $T) $METHOD(ctx context.Context, ...) { ... }
- pattern-not: func ($R $T) $METHOD($C echo.Context, ...) { ... }
- pattern-not: func ($R $T) $METHOD($C shared.Context, ...) { ... }
- id: service-method-missing-ctx-empty-params
message: Service method should have ctx context.Context as first parameter
severity: WARNING
languages: [go]
paths:
include:
- "**/services/**"
patterns:
- pattern: func ($R $T) $METHOD() { ... }
- id: repo-method-missing-tx-only-ctx
message: Repository method should have tx *gorm.DB as second parameter after ctx
severity: WARNING
languages: [go]
paths:
include:
- "**/database/repositories/**"
exclude:
- "**/database/repositories/gorm_repository.go"
patterns:
- pattern: func ($R $T) $METHOD(ctx context.Context) { ... }
- pattern-not: func ($R $T) Begin(ctx context.Context) { ... }
- id: tx-begin-without-defer-rollback
message: |
db.Begin() called without defer tx.Rollback(). Leaked transactions hold locks.
Use: tx := db.Begin(); defer tx.Rollback(); ... ; tx.Commit()
severity: ERROR
languages: [go]
patterns:
- pattern: $TX := $DB.Begin()
- pattern-not-inside: |
...
defer $TX.Rollback()
...
- id: tx-begin-without-commit
message: |
db.Begin() called but tx.Commit() is never called in this function.
The transaction will always be rolled back.
severity: ERROR
languages: [go]
patterns:
- pattern: |
$TX := $DB.Begin()
...
- pattern-not-inside: |
...
$TX.Commit()
...
- id: context-background-when-ctx-available
message: |
context.Background() / context.TODO() used inside a function that already receives a context parameter.
Reuse the passed-in context to propagate tracing and cancellation signals instead of starting a new root context.
severity: WARNING
languages: [go]
paths:
exclude:
- "**/*_test.go"
- "**/tests/**"
- "**/cmd/**"
patterns:
- pattern-either:
- pattern-inside: |
func $NAME(..., $CTX context.Context, ...) $RET {
...
}
- pattern-inside: |
func ($RECV $TYPE) $NAME(..., $CTX context.Context, ...) $RET {
...
}
- pattern-inside: |
func $NAME(..., $C echo.Context, ...) $RET {
...
}
- pattern-inside: |
func ($RECV $TYPE) $NAME(..., $C echo.Context, ...) $RET {
...
}
- pattern-not-inside: trace.ContextWithSpan(context.Background(), ...)
- pattern-either:
- pattern: context.Background()
- pattern: context.TODO()