|
| 1 | +package testutils |
| 2 | + |
| 3 | +import "github.com/securego/gosec/v2" |
| 4 | + |
| 5 | +// #nosec - This file intentionally contains bidirectional Unicode characters |
| 6 | +// for testing trojan source detection. The G116 rule scans the entire file content (not just AST nodes) |
| 7 | +// because trojan source attacks work by manipulating visual representation of code through bidirectional |
| 8 | +// text control characters, which can appear in comments, strings or anywhere in the source file. |
| 9 | +// Without this #nosec exclusion, gosec would detect these test samples as actual vulnerabilities. |
| 10 | +var ( |
| 11 | + // SampleCodeG116 - TrojanSource code snippets |
| 12 | + SampleCodeG116 = []CodeSample{ |
| 13 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// This comment contains bidirectional unicode: access\u202e\u2066 granted\u2069\u202d\n\tisAdmin := false\n\tfmt.Println(\"Access status:\", isAdmin)\n}\n"}, 1, gosec.NewConfig()}, |
| 14 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Trojan source with RLO character\n\taccessLevel := \"user\"\n\t// Actually assigns \"nimda\" due to bidi chars: accessLevel = \"\u202enimda\"\n\tif accessLevel == \"admin\" {\n\t\tfmt.Println(\"Access granted\")\n\t}\n}\n"}, 1, gosec.NewConfig()}, |
| 15 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// String with bidirectional override\n\tusername := \"admin\u202e \u2066Check if admin\u2069 \u2066\"\n\tpassword := \"secret\"\n\tfmt.Println(username, password)\n}\n"}, 1, gosec.NewConfig()}, |
| 16 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains LRI (Left-to-Right Isolate) U+2066\n\tcomment := \"Safe comment \u2066with hidden text\u2069\"\n\tfmt.Println(comment)\n}\n"}, 1, gosec.NewConfig()}, |
| 17 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains RLI (Right-to-Left Isolate) U+2067\n\tmessage := \"Normal text \u2067hidden\u2069\"\n\tfmt.Println(message)\n}\n"}, 1, gosec.NewConfig()}, |
| 18 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains FSI (First Strong Isolate) U+2068\n\ttext := \"Text with \u2068hidden content\u2069\"\n\tfmt.Println(text)\n}\n"}, 1, gosec.NewConfig()}, |
| 19 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains LRE (Left-to-Right Embedding) U+202A\n\tembedded := \"Text with \u202aembedded\u202c content\"\n\tfmt.Println(embedded)\n}\n"}, 1, gosec.NewConfig()}, |
| 20 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains RLE (Right-to-Left Embedding) U+202B\n\trtlEmbedded := \"Text with \u202bembedded\u202c content\"\n\tfmt.Println(rtlEmbedded)\n}\n"}, 1, gosec.NewConfig()}, |
| 21 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains PDF (Pop Directional Formatting) U+202C\n\tformatted := \"Text with \u202cformatting\"\n\tfmt.Println(formatted)\n}\n"}, 1, gosec.NewConfig()}, |
| 22 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains LRO (Left-to-Right Override) U+202D\n\toverride := \"Text \u202doverride\"\n\tfmt.Println(override)\n}\n"}, 1, gosec.NewConfig()}, |
| 23 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains RLO (Right-to-Left Override) U+202E\n\trloText := \"Text \u202eoverride\"\n\tfmt.Println(rloText)\n}\n"}, 1, gosec.NewConfig()}, |
| 24 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains RLM (Right-to-Left Mark) U+200F\n\tmarked := \"Text \u200fmarked\"\n\tfmt.Println(marked)\n}\n"}, 1, gosec.NewConfig()}, |
| 25 | + {[]string{"\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\t// Contains LRM (Left-to-Right Mark) U+200E\n\tlrmText := \"Text \u200emarked\"\n\tfmt.Println(lrmText)\n}\n"}, 1, gosec.NewConfig()}, |
| 26 | + {[]string{` |
| 27 | +package main |
| 28 | +
|
| 29 | +import "fmt" |
| 30 | +
|
| 31 | +// Safe code without bidirectional characters |
| 32 | +func main() { |
| 33 | + username := "admin" |
| 34 | + password := "secret" |
| 35 | + fmt.Println("Username:", username) |
| 36 | + fmt.Println("Password:", password) |
| 37 | +} |
| 38 | +`}, 0, gosec.NewConfig()}, |
| 39 | + {[]string{` |
| 40 | +package main |
| 41 | +
|
| 42 | +import "fmt" |
| 43 | +
|
| 44 | +// Normal comment with regular text |
| 45 | +func main() { |
| 46 | + // This is a safe comment |
| 47 | + isAdmin := true |
| 48 | + if isAdmin { |
| 49 | + fmt.Println("Access granted") |
| 50 | + } |
| 51 | +} |
| 52 | +`}, 0, gosec.NewConfig()}, |
| 53 | + {[]string{` |
| 54 | +package main |
| 55 | +
|
| 56 | +import "fmt" |
| 57 | +
|
| 58 | +func main() { |
| 59 | + // Regular ASCII characters only |
| 60 | + message := "Hello, World!" |
| 61 | + fmt.Println(message) |
| 62 | +} |
| 63 | +`}, 0, gosec.NewConfig()}, |
| 64 | + {[]string{` |
| 65 | +package main |
| 66 | +
|
| 67 | +import "fmt" |
| 68 | +
|
| 69 | +func authenticateUser(username, password string) bool { |
| 70 | + // Normal authentication logic |
| 71 | + if username == "admin" && password == "secret" { |
| 72 | + return true |
| 73 | + } |
| 74 | + return false |
| 75 | +} |
| 76 | +
|
| 77 | +func main() { |
| 78 | + result := authenticateUser("user", "pass") |
| 79 | + fmt.Println("Authenticated:", result) |
| 80 | +} |
| 81 | +`}, 0, gosec.NewConfig()}, |
| 82 | + } |
| 83 | +) |
0 commit comments