Skip to content

Commit a04bcfd

Browse files
committed
Add 2 unmarshal tag Semgrep rules
1 parent 796f564 commit a04bcfd

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

go/unmarshal_tag_is_dash.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
type TestStruct1 struct {
4+
// ok: unmarshal-tag-is-dash
5+
A string `json:"id"`
6+
}
7+
8+
type TestStruct2 struct {
9+
// ruleid: unmarshal-tag-is-dash
10+
B string `json:"-,omitempty"`
11+
}
12+
13+
type TestStruct3 struct {
14+
// ruleid: unmarshal-tag-is-dash
15+
C string `json:"-,123"`
16+
}
17+
18+
type TestStruct4 struct {
19+
// ruleid: unmarshal-tag-is-dash
20+
D string `json:"-,"`
21+
}

go/unmarshal_tag_is_dash.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
rules:
2+
- id: unmarshal-tag-is-dash
3+
message: >-
4+
Struct field can be decoded with the `-` key because the JSON tag starts with a `-` but is followed by a comma.
5+
languages: [go]
6+
severity: WARNING
7+
metadata:
8+
cwe: "CWE-172: Encoding Error"
9+
category: security
10+
subcategory: [vuln]
11+
confidence: HIGH
12+
likelihood: MEDIUM
13+
impact: MEDIUM
14+
patterns:
15+
- pattern: |
16+
type $T1 struct {
17+
...
18+
$X $T2 `$TAG`
19+
...
20+
}
21+
- focus-metavariable: $TAG
22+
- metavariable-regex:
23+
metavariable: $TAG
24+
regex: >-
25+
.*(json|yaml|xml):"-,[^"]*"
26+

go/unmarshal_tag_is_omitempty.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type SomeStruct struct {
9+
// ruleid: unmarshal-tag-is-omitempty
10+
SomeField string `json:"omitempty"`
11+
// ok: unmarshal-tag-is-omitempty
12+
SomeField_2 string `json:",omitempty"`
13+
}
14+
15+
func test_omitempty() {
16+
u := SomeStruct{}
17+
_ = json.Unmarshal([]byte(`{"omitempty": "123", "SomeField_2": "456"}`), &u)
18+
fmt.Printf("Result: %#v\n", u)
19+
// Result: main.SomeStruct{SomeField:"123"}
20+
}

go/unmarshal_tag_is_omitempty.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
rules:
2+
- id: unmarshal-tag-is-omitempty
3+
message: >-
4+
Struct field can be unmarshaled with the `omitempty` key. The developer likely wanted to write ",omitempty" instead.
5+
languages: [go]
6+
severity: WARNING
7+
metadata:
8+
cwe: "CWE-172: Encoding Error"
9+
category: security
10+
subcategory: [vuln]
11+
confidence: HIGH
12+
likelihood: MEDIUM
13+
impact: LOW
14+
patterns:
15+
- pattern: |
16+
type $T1 struct {
17+
...
18+
$X $T2 `$TAG`
19+
...
20+
}
21+
- focus-metavariable: $TAG
22+
- metavariable-regex:
23+
metavariable: $TAG
24+
regex: >-
25+
.*(json|yaml|xml):"omitempty

0 commit comments

Comments
 (0)