-
-
Notifications
You must be signed in to change notification settings - Fork 669
feature: add rule for trojan source #1431
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
Open
kfess
wants to merge
2
commits into
securego:master
Choose a base branch
from
kfess:feature/trojansource
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+319
−0
Open
Changes from all commits
Commits
Show all changes
2 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
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
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,96 @@ | ||
| package rules | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "os" | ||
|
|
||
| "github.com/securego/gosec/v2" | ||
| "github.com/securego/gosec/v2/issue" | ||
| ) | ||
|
|
||
| type trojanSource struct { | ||
| issue.MetaData | ||
| bidiChars map[rune]struct{} | ||
| } | ||
|
|
||
| func (r *trojanSource) ID() string { | ||
| return r.MetaData.ID | ||
| } | ||
|
|
||
| func (r *trojanSource) Match(node ast.Node, c *gosec.Context) (*issue.Issue, error) { | ||
| if file, ok := node.(*ast.File); ok { | ||
| fobj := c.FileSet.File(file.Pos()) | ||
| if fobj == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| content, err := os.ReadFile(fobj.Name()) | ||
| if err != nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| for _, ch := range string(content) { | ||
| if _, exists := r.bidiChars[ch]; exists { | ||
| return c.NewIssue(node, r.ID(), r.What, r.Severity, r.Confidence), nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| // func (r *trojanSource) Match(node ast.Node, c *gosec.Context) (*issue.Issue, error) { | ||
| // if file, ok := node.(*ast.File); ok { | ||
| // fobj := c.FileSet.File(file.Pos()) | ||
| // if fobj == nil { | ||
| // return nil, nil | ||
| // } | ||
|
|
||
| // file, err := os.Open(fobj.Name()) | ||
| // if err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
|
|
||
| // defer file.Close() | ||
|
|
||
| // scanner := bufio.NewScanner(file) | ||
| // for scanner.Scan() { | ||
| // line := scanner.Text() | ||
| // for _, ch := range line { | ||
| // if _, exists := r.bidiChars[ch]; exists { | ||
| // return c.NewIssue(node, r.ID(), r.What, r.Severity, r.Confidence), nil | ||
| // } | ||
| // } | ||
| // } | ||
|
|
||
| // if err := scanner.Err(); err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
| // } | ||
|
|
||
| // return nil, nil | ||
| // } | ||
|
|
||
| func NewTrojanSource(id string, _ gosec.Config) (gosec.Rule, []ast.Node) { | ||
| return &trojanSource{ | ||
| MetaData: issue.MetaData{ | ||
| ID: id, | ||
| Severity: issue.High, | ||
| Confidence: issue.Medium, | ||
| What: "Potential Trojan Source vulnerability via use of bidirectional text control characters", | ||
| }, | ||
| bidiChars: map[rune]struct{}{ | ||
| '\u202a': {}, | ||
| '\u202b': {}, | ||
| '\u202c': {}, | ||
| '\u202d': {}, | ||
| '\u202e': {}, | ||
| '\u2066': {}, | ||
| '\u2067': {}, | ||
| '\u2068': {}, | ||
| '\u2069': {}, | ||
| '\u200e': {}, | ||
| '\u200f': {}, | ||
| }, | ||
| }, []ast.Node{(*ast.File)(nil)} | ||
| } | ||
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,217 @@ | ||
| package testutils | ||
|
|
||
| import "github.com/securego/gosec/v2" | ||
|
|
||
| // #nosec - This file intentionally contains bidirectional Unicode characters | ||
| // for testing trojan source detection. The G116 rule scans the entire file content (not just AST nodes) | ||
| // because trojan source attacks work by manipulating visual representation of code through bidirectional | ||
| // text control characters, which can appear in comments, strings or anywhere in the source file. | ||
| // Without this #nosec exclusion, gosec would detect these test samples as actual vulnerabilities. | ||
| var ( | ||
| // SampleCodeG116 - TrojanSource code snippets | ||
| SampleCodeG116 = []CodeSample{ | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // This comment contains bidirectional unicode: access granted | ||
| isAdmin := false | ||
| fmt.Println("Access status:", isAdmin) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Trojan source with RLO character | ||
| accessLevel := "user" | ||
| // Actually assigns "nimda" due to bidi chars: accessLevel = "nimda" | ||
| if accessLevel == "admin" { | ||
| fmt.Println("Access granted") | ||
| } | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // String with bidirectional override | ||
| username := "admin Check if admin " | ||
| password := "secret" | ||
| fmt.Println(username, password) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains LRI (Left-to-Right Isolate) U+2066 | ||
| comment := "Safe comment with hidden text" | ||
| fmt.Println(comment) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains RLI (Right-to-Left Isolate) U+2067 | ||
| message := "Normal text hidden" | ||
| fmt.Println(message) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains FSI (First Strong Isolate) U+2068 | ||
| text := "Text with hidden content" | ||
| fmt.Println(text) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains LRE (Left-to-Right Embedding) U+202A | ||
| embedded := "Text with embedded content" | ||
| fmt.Println(embedded) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains RLE (Right-to-Left Embedding) U+202B | ||
| rtlEmbedded := "Text with embedded content" | ||
| fmt.Println(rtlEmbedded) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains PDF (Pop Directional Formatting) U+202C | ||
| formatted := "Text with formatting" | ||
| fmt.Println(formatted) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains LRO (Left-to-Right Override) U+202D | ||
| override := "Text override" | ||
| fmt.Println(override) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains RLO (Right-to-Left Override) U+202E | ||
| rloText := "Text override" | ||
| fmt.Println(rloText) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains RLM (Right-to-Left Mark) U+200F | ||
| marked := "Text marked" | ||
| fmt.Println(marked) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Contains LRM (Left-to-Right Mark) U+200E | ||
| lrmText := "Text marked" | ||
| fmt.Println(lrmText) | ||
| } | ||
| `}, 1, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| // Safe code without bidirectional characters | ||
| func main() { | ||
| username := "admin" | ||
| password := "secret" | ||
| fmt.Println("Username:", username) | ||
| fmt.Println("Password:", password) | ||
| } | ||
| `}, 0, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| // Normal comment with regular text | ||
| func main() { | ||
| // This is a safe comment | ||
| isAdmin := true | ||
| if isAdmin { | ||
| fmt.Println("Access granted") | ||
| } | ||
| } | ||
| `}, 0, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| // Regular ASCII characters only | ||
| message := "Hello, World!" | ||
| fmt.Println(message) | ||
| } | ||
| `}, 0, gosec.NewConfig()}, | ||
| {[]string{` | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func authenticateUser(username, password string) bool { | ||
| // Normal authentication logic | ||
| if username == "admin" && password == "secret" { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func main() { | ||
| result := authenticateUser("user", "pass") | ||
| fmt.Println("Authenticated:", result) | ||
| } | ||
| `}, 0, gosec.NewConfig()}, | ||
| } | ||
| ) |
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.
Does this perform well with large files?
I would use something like to make sure that we don't run out of memory and have performance issues: