Commit Graph
264 Commits
Author SHA1 Message Date
Cosmin Cojocar 70201119fe Skip flaging the TLS min version for go 1.18+ (#1646)
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-04-25 10:06:56 +02:00
Cosmin CojocarandClaude Opus 4.6 355cfa5a43 fix: reduce G117 false positives for custom marshalers and transformed values (#1614) (#1615)
G117 now skips findings when:
- The marshal call is inside a custom marshaler method (MarshalJSON, MarshalYAML, etc.)
- The type being marshaled implements a custom marshaler interface
- A composite literal wraps the sensitive field value in a function call (e.g. mask())

Also fixes the issue message to show the correct format name (JSON/YAML/XML/TOML)
instead of always saying "JSON key".

Closes #1614

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 08:56:12 +01:00
Cosmin Cojocar f3e2fac4d5 Regenrate the TLS config rule (#1539)
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-21 13:53:22 +01:00
Cosmin Cojocar e34e8dd8e8 Extend the G117 rule to cover other types of serialization such as yaml/xml/toml (#1529)
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-18 22:27:02 +01:00
Cosmin Cojocar b940702d5e Fix the G117 rule to take the JSON serialization into account (#1528)
* Fix g117 to take into account the json serialization

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>

* Ignore lint warning

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>

---------

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-18 21:50:17 +01:00
Cosmin Cojocar 9344582ee4 Improve test coverage in various areas (#1511)
* Improve test coverage

Improve tests coverage in various areas.

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>

* Fix lint warnings

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>

---------

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-14 16:48:39 +01:00
oittaa a631af86ec Fix URL regexp and remove redundant Google regex patterns (#1485) 2026-01-28 10:28:52 +01:00
oittaa 89685023f9 feat: implement global cache usage in rules (#1480)
* feat: implement global cache usage in rules

* refactor: make global cache generic with local key types

- Remove GlobalKey struct from gosec_cache.go
- Each use case now defines its own key type (type safety via Go's type system)
- Move RegexMatchWithCache to separate regex_cache.go file
- Move cache kind constants to rules/hardcoded_credentials.go as local types
- Add documentation for cache key requirements
2026-01-26 13:29:03 +01:00
oittaa 5288673473 feat: implement entropy pre-filtering to optimize secret detection (#1479) 2026-01-23 11:25:02 +01:00
oittaa 0cc9e01a9d Refine G407 to improve detection and coverage of hardcoded nonces (#1460)
* Refine G407 to improve detection and coverage of hardcoded nonces

* chore: consolidate common analyzer patterns into util.go and improve G602 coverage

* Optimize G602 and G115 with state caching and regex pre-compilation

* Improve G115 overflow detection and fix false positives and false negatives

* golangci-lint workaround
2026-01-12 09:56:55 +01:00
oittaa 7387d22592 Refactor rules to use callListRule base structure (#1458)
* Refactor rules to utilize callListRule base structure

- Introduced a new base structure `callListRule` in `rules/base.go` to standardize the implementation of rules that check for specific function calls.
- Updated existing rules to inherit from `callListRule`, simplifying their structure and removing redundant ID methods.
- Modified the `MetaData` field to use `RuleID` instead of `ID` for consistency across rules.
- Removed the `weakcryptohash.go` and `weakdepricatedcryptohash.go` files as their functionality has been integrated into the new structure.

* fix(tlsconfig): correct MetaData field name in generated TLS check

* refactor: standardize rule metadata and call list initialization
2026-01-09 13:55:35 +01:00
oittaaandCosmin Cojocar 649e2c8da4 remove deprecated ast.Object (#1455)
* remove deprecated ast.Object

* feat(tls): enhance TLS configuration handling with new checks for InsecureSkipVerify and PreferServerCipherSuites

---------

Co-authored-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-01-06 14:44:42 +01:00
oittaa 35a92b49d5 feat(sql): enhance SQL injection detection with improved string concatenation checks (#1454)
* feat(sql): enhance SQL injection detection with improved string concatenation checks

* optimize: only one ast.Inspect loop, use slices.ContainsFunc

* refactor(sql): streamline SQL argument retrieval, replace constObject with TryResolve, minor cleanup

* feat(sql): enhance query mutation checks for shadowed variables and add regression tests

* remove deprecated ast.Object
2026-01-06 14:30:54 +01:00
oittaa bc9d2bc879 feat(rules): enhance subprocess variable checks (#1453)
* feat(rules): enhance subprocess variable checks

* ast.Object is deprecated
2026-01-06 14:20:23 +01:00
oittaa 0f6f21cb3f feat: add secrets serialization G117 (#1451)
* Rule to detect secrets serialization

* Add G117 to rules_test.go

* Fix false positives

* Map to CWE 499, update README
2026-01-04 17:21:22 +02:00
oittaa 717706e815 feat(rules): add support for detecting high entropy strings in composite literals (#1447) 2026-01-02 09:58:08 +02:00
oittaa 082deb6cee whitelist crypto/rand Read from error checks (#1446) 2025-12-31 18:57:36 +02:00
kfessandCosmin Cojocar 424fc4cd9c feature: add rule for trojan source (#1431)
* feature: add rule for trojan source

* use bufio.Scanner for memory efficiency

* Fix lint warnings

Change-Id: Ic1df6704ba5ab8b1834d7765abd49494a98835f8
Signed-off-by: Cosmin Cojocar <ccojocar@google.com>

---------

Signed-off-by: Cosmin Cojocar <ccojocar@google.com>
Co-authored-by: Cosmin Cojocar <ccojocar@google.com>
2025-12-11 10:14:29 +01:00
Eshani Parulekar 506407e7df feat(G304): add os.Root remediation hint (Autofix) when Go >= 1.24 2025-09-16 14:37:06 +00:00
Eshani Parulekar e81fba3c3a refactor(G304): remove unused trackJoin helper; no functional change 2025-09-12 13:52:23 +00:00
Eshani Parulekar ab078db7b0 style: gofmt rules/readfile.go 2025-09-12 13:52:23 +00:00
Eshani Parulekar 79f835d9c7 rules(G304): analyze only path arg; ignore flag/perm vars; track Clean and safe Join; fix nil-context panic\n\n- Limit G304 checks to first arg (path) for os.Open/OpenFile/ReadFile, avoiding false positives when flag/perm are variables\n- Track filepath.Clean so cleaned identifiers are treated as safe\n- Consider safe joins: filepath.Join(const|resolvedBase, Clean(var)|cleanedIdent)\n- Record Join(...) assigned to identifiers and allow if later cleaned\n- Fix panic by passing non-nil context in trackJoinAssignStmt\n- All rules tests: 42 passed 2025-09-12 13:52:23 +00:00
Eshani Parulekar 40ac53017b rules(G202): detect SQL concat in ValueSpec declarations; add test sample\n\n- Handle var query string = 'SELECT ...' + user style declarations\n- Reuse existing binary expr detection on ValueSpec.Values\n- Add postgres sample mirroring issue #1309 report\n- Rules tests: 42 passed 2025-09-12 13:27:02 +00:00
Oleksandr Redko 4a8cb4609f Do not allow dashes in file names 2025-06-16 14:34:38 +02:00
Ilia Mirkin 017d1d655c G201/G202: add checks for injection into sql.Conn methods
We check sql.DB and sql.Tx, but sql.Conn appears to have been missed. It
carries the same issues as DB/Tx in terms of injection.
2025-06-03 16:22:56 +02:00
Brandon AnninandccoVeille 1336dc6820 remove G113. It only affects old/unsupported versions of Go (#1328)
* don't warn on G113 (big.Rat SetString) if on an unaffected version of Go

Newer versions of go (>=1.16.14, >=1.17.7, 1.18+) are not affected by this. Don't warn at all on those newer versions. See https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-23772

* alert on all known versions

Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>

* remove G113 CVE-2022-23772 which only affects old/unsupport Go versions

* Retire rule

* gofmt

---------

Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
2025-04-03 16:44:20 +02:00
Oleksandr Redko e0cca6fe95 Update what message for G104 (#1282) 2025-01-06 15:41:33 +01:00
Oleksandr Redko 92de0ee7a2 Replace old golang.org links with new go.dev (#1271) 2024-12-16 15:18:51 +01:00
Oleksandr Redko 399e835157 Fix typos in comments and fields 2024-11-30 10:37:42 +01:00
Dimitar Banchev 7f8f654235 Updated analyzer to use new way of initialization
* Removed old way of initializing analyzers
* Added the new analyzer to the rest of the default analyzers
* Fixed small bug in the rule
* Removed the test for the new analyzer from the file responsible for testing the rules
* Merged the diffrent examples into 1 variable
* Added tests for the analyzer
* Removed code that was used for testing rules, but it was used to test the analyzer
2024-08-30 19:35:07 +02:00
Dimitar Banchev a26215cf23 Migrated the rule to the analyzers folder 2024-08-30 19:35:07 +02:00
Dimitar Banchev 3f6e1e7326 Refractored code a little bit 2024-08-30 19:35:07 +02:00
Dimitar Banchev 0eb8143c23 Added new rule G407(hardcoded IV/nonce)
The rule is supposed to detect for the usage of hardcoded or static nonce/Iv in many encryption algorithms:

* The different modes of AES (mainly tested here)
* It should be able to work with ascon

Currently the rules doesn't check when constant variables are used.

TODO: Improve the rule, to detected for constatant variable usage
2024-08-30 19:35:07 +02:00
Rahul Gadi 81cda2f91f Allow excluding analyzers globally (#1180)
* This change does not exclude analyzers for inline comment
* Changed the expected issues count for G103, G109 samples for test. Previously G115 has been included in the issue count
* Show analyzers IDs(G115, G602) in gosec usage help
* See #1175
2024-08-20 10:43:40 +02:00
Dimitar Banchev 9a4a741e6b Added more rules
* Rule G406 responsible for the usage of deprecated MD4 and RIPEMD160 added.
* Rules G506, G507 responsible for tracking the usage of the already mentioned libraries added.
* Slight changes in the Makefile(`make clean` wasn't removing all expected files)
* Added license to `analyzer_test.go`
2024-06-25 13:18:27 +02:00
Dimitar Banchev fc0957f6a3 Minor changes
* Renamed the file responsible for rule G401
* Removed copyright of HP from the new rule
2024-06-24 15:25:54 +02:00
Dimitar Banchev 58e4fccc13 Split the G401 rule into two separate ones
Now the G401 rule is split into hashing and encryption algorithms.

G401 is responsible for checking the usage of MD5 and SHA1, with corresponding CWE of 328.
And G405(New rule) is responsible for checking the usege of DES and RC4, with corresponding CWE of 327.
2024-06-24 15:25:54 +02:00
Cosmin Cojocar ed3f51e663 Add more types to templates rule
Add additional types such as CSS, JSStr and Srcset to the template rule.
These types are marked as a security risk in the godoc
https://pkg.go.dev/html/template.

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2024-05-28 10:39:33 +02:00
Cosmin Cojocar 4bf5667f66 Add a new rule to detect integer overflow on integer types conversion
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2024-05-27 13:03:01 +02:00
Cosmin Cojocar 6fbd381238 Catch os.ModePerm permissions in os.WriteFile
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2024-05-14 15:33:23 +02:00
Cosmin Cojocar 417a44c73b Add filepath.EvalSymlinks to clean functions in rule G304
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2024-05-13 17:19:29 +02:00
Hiroki Yorimitsu be378e682f Add support for math/rand/v2 added in Go 1.22 2024-03-07 16:33:18 +01:00
Cosmin Cojocar 36878a9423 Skip the G601 tests for Go version 1.22
Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
2024-03-07 12:23:46 +01:00
Janusz Marcinkiewicz f25ccd9fb5 Ignore 'implicit memory aliasing' rule for Go 1.22+
Signed-off-by: Janusz Marcinkiewicz <januszm@nvidia.com>
2024-03-04 10:24:32 +01:00
Quentin Laplanche c824a5d308 fix(hardcoded): remove duplicated Stripe API Key 2024-02-13 10:02:03 +01:00
Cosmin Cojocar 616520f44f Update the list of unsafe functions detected by the unsafe rule (#1033)
Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
2023-10-10 09:47:36 +02:00
Cosmin Cojocar 4def3a4eb0 Fix lint warning
Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
2023-09-25 13:24:34 +02:00
Cosmin Cojocar 0d332a1027 Add a new rule which detects when a file is created with os.Create but the configured permissions are less than 0666
It seems that the os.Create will create by default a file with 0666 permissions.

This should be detected when the configured permissions are less than 0666. By default will not detect this case
unless the more restrictive mode is configured.

Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
2023-09-25 13:24:34 +02:00
Cosmin Cojocar e02e2f6d5b Redesign and reimplement the slice out of bounds check using SSA code representation
Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
2023-09-20 10:19:51 +02:00
Cosmin Cojocar 6c93653a29 Fix hardcoded_credentials rule to only match on more specific patterns (#1009)
* Fix hardcoded_credentials rule to only match on more specific patterns

Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>

* Fix lint warnings

Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>

* Fix double escape in regexps

Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>

---------

Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
2023-09-05 18:00:02 +02:00