Commit Graph
1279 Commits
Author SHA1 Message Date
Cosmin Cojocar 4f1f362671 Add more unit tests to improve coverage (#1512)
* Add more tests to improve test coverage

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

* Fix lint warnings

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

* fix lint warnings

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

* Fix lint warnings

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

* Fix lint warnings

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 21:57:51 +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
Cosmin Cojocar 8d1b2c63ae Imprve the test coverage (#1510)
Add more tests in areas which are not currently covered.

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-14 15:47:36 +01:00
Cosmin Cojocar 993c1c4da2 Fix incorrect detection of fixed iv in G407 (#1509)
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-14 14:15:55 +01:00
Cosmin Cojocar 8668b74892 Add support for go 1.26.x and removed support for go 1.24.x (#1508)
We keep support only for two major version.

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-14 13:53:19 +01:00
Cosmin Cojocar 514225c8cb Fix the sonar report to follow the latest schema (#1507)
Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-02-14 12:59:24 +01:00
Ravi Sastry Kadali 000384e510 fix: broken taint analysis causing false positives (#1506)
* fix: broken taint analysis causing false positives

* add tests and improve code coverage
2026-02-14 12:24:33 +01:00
Ravi Sastry Kadali 616192c9d9 fix: panic on float constants in overflow analyzer (#1505) 2026-02-14 12:16:41 +01:00
Ravi Sastry Kadali 79956a3b4c fix: panic when scanning multi-module repos from root (#1504)
* fix: panic when scanning multi-module repos from root

* address linting
2026-02-14 12:15:02 +01:00
Ravi Sastry Kadali 5736e8b88b fix: G602 false positive for array element access (#1499)
Fixes #1495
2026-02-13 11:53:55 +01:00
Cosmin Cojocar 1b7e1e94bc Update gosec to version v2.23.0 in the Github action (#1496)
Update Github action to use the release of gosec v2.23.0

Change-Id: I72672694bea0a1e25229283e15459f7762965fba

Signed-off-by: Cosmin Cojocar <ccojocar@google.com>
2026-02-11 13:52:41 +01:00
Ravi Sastry Kadali 398ad549bb feat: Support for adding taint analysis engine (#1486)
* feat: add taint analysis engine for data flow security

Implements SSA-based taint analysis to detect security vulnerabilities:
- G701: SQL injection via string concatenation
- G702: Command injection via user input
- G703: Path traversal via user input
- G704: SSRF via user-controlled URLs
- G705: XSS via unescaped user input
- G706: Log injection via user input

Uses golang.org/x/tools for SSA/call graph analysis with CHA.
Zero external dependencies beyond existing gosec imports.
v2.23.0
2026-02-10 15:47:11 +01:00
renovate[bot] 6eacd5c058 chore(deps): update all dependencies (#1494)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-02-09 20:05:18 +01:00
renovate[bot] 181a7cb072 chore(deps): update all dependencies (#1494)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-02-09 19:56:07 +01:00
renovate[bot] e2fa6ab0ba chore(deps): update all dependencies (#1488)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-02-07 12:01:23 +01:00
Ravi Sastry Kadali eb252ba8d7 Fix G602 analyzer panic that kills gosec process (#1491)
* update go version to 1.25.7

* Fix G602 analyzer panic that kills gosec process

* guard against nil block

* add tests for nil guard fixes
2026-02-07 11:30:59 +01:00
Ravi Sastry Kadali 20d71a0cc1 update go version to 1.25.7 (#1492) 2026-02-07 11:29:32 +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
renovate[bot] 04f729ce53 chore(deps): update module google.golang.org/genai to v1.43.0 (#1484)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-01-26 13:27:17 +01:00
oittaa ade0e8f432 refactor: optimize nosec parsing and reduce allocations (#1478)
- Replace dynamic fmt.Errorf with static errors in hot paths
- Replace regex-based directive parsing with manual string parsing (removed regexp import)
- Use const for directive prefix
2026-01-25 12:35:59 +01:00
Ravi Sastry Kadali d24bbf7d14 Fix SARIF artifactChanges null validation error (#1483)
Only create Fix object when autofix is non-empty to avoid null artifactChanges.
Fixes #1482.
2026-01-25 12:33:21 +01:00
oittaa 15cba7fae1 feat: optimize GetCallInfo with per-package sync.Pool caching (#1481) 2026-01-23 11:27:37 +01:00
oittaa 5288673473 feat: implement entropy pre-filtering to optimize secret detection (#1479) 2026-01-23 11:25:02 +01:00
oittaa d9a9bcd45c feat: ensure GoVersion is cached using sync.Once (#1477) 2026-01-23 11:23:34 +01:00
Ravi Sastry Kadali 516260af4e Fix #1240: nosec comments now work with trailing open brackets (#1475)
Add line-based fallback for nosec comment detection when ast.CommentMap
fails to associate comments correctly. This fixes the case where #nosec
comments at the end of lines with open brackets were being ignored.

Refactor duplicate parsing logic into parseNoSecDirective helper function
to reduce code complexity and improve maintainability.
2026-01-23 10:59:42 +01:00
Ravi Sastry KadaliandOittaa be0fd6dcfd Debug Build Profiling Support: Code improvement suggestions for PR#1471 (#1476)
* feat: add debug build profiling support

Add CPU and memory profiling capabilities for debug builds using Go build
tags. Profiling code is completely excluded from release builds.

Changes:
- Add profiling_debug.go with -cpuprofile and -memprofile flags (build tag: debug)
- Add build-debug and build-debug-race Makefile targets
- Refactor main() to run() pattern for proper defer handling
- Replace logger.Fatal() with logger.Printf() + return for clean exits
- Pass logger to profiling for consistent [gosec] log prefix

Usage:
  make build-debug
  ./gosec-debug -cpuprofile cpu.prof -memprofile mem.prof ./...

* Refactor profiling: encapsulate state and improve error handling

- Encapsulate profiling state in Profiler struct to eliminate package-level mutable state
- Add proper error handling with error returns instead of silent failures
- Add exit code constants (exitSuccess/exitFailure) to replace magic numbers
- Fix Makefile clean target to remove gosec-debug binary
- Add nil logger guard for safety

* Use exit constants in all return statements

Replace remaining magic numbers (0, 1) with exitSuccess and exitFailure
constants throughout the run() function for consistency.

---------

Co-authored-by: Oittaa <eero@oittaa.net>
2026-01-23 10:38:04 +01:00
Cosmin Cojocar b579523bf6 Update the go version to 1.25.6 and 1.24.12 (#1474)
fixes #1469

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2026-01-19 18:38:57 +01:00
oittaa bd3c738bf0 G115: Enhance RangeAnalyzer with constant propagation and chained arithmetic support (#1470)
* G115: Enhance RangeAnalyzer with constant propagation and chained arithmetic support

* Fix G115 overflow detection for negated values and robustify RangeAnalyzer propagation
2026-01-19 17:56:26 +01:00
renovate[bot] 6897b3661c chore(deps): update all dependencies (#1473)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-01-19 12:07:51 +01:00
Ravi Sastry Kadali 9f202122a7 feat: support path-based rule exclusions via exclude-rules (#1465)
* add path-based rule exclusions

Implements #1287

* Ssupport for excluding specific rules from specific paths, enabling large monorepos to apply different security rules to different components (e.g., CLI tools vs services).

* fix formatting issuue with path filter test to pass gci
2026-01-15 11:59:43 +01:00
oittaa 726d847c79 Optimize analyzer with parallel package processing (#1466)
- Memory optimization
- Public API compatibility
- Stateless private functions
2026-01-14 16:37:18 +01:00
Tom van der Woerdt 3150b28fc4 feat: add goanalysis package for nogo (#1449)
* feat: add goanalysis package for nogo

Add goanalysis package providing a standard
golang.org/x/tools/go/analysis.Analyzer for gosec. Enables
integration with nogo, and go vet.

- Implements analysis.Analyzer interface
- Reuses SSA built by analysis framework for efficient caching
- Configurable severity/confidence filtering via flags
- Includes CWE IDs in diagnostics ([CWE-XXX] format)
- Runs both AST rules and SSA analyzers
- Respects #nosec and suppression directives

Also exclude testdata from security scanning in Makefile to
prevent false positives on intentionally vulnerable test files.

* Also exclude testdata from github action
2026-01-14 11:10:06 +01:00
oittaa 7284e15230 Refactor Analyzers: Unify Range Logic & Optimize Allocations (#1464)
* refactor

* optimizations

* Refactor analyzers: unify range logic and optimize allocations- Centralize numeric range analysis in util.go (shared by G115/G602).- Implement object pooling for slice_bounds and hardcoded_nonce.- Update conversion_overflow tests to use real analyzer logic.

* Refactor RangeAnalyzer
2026-01-14 10:52:35 +01:00
oittaa 7a4ccefd88 Optimize G115, G602, G407 analyzers to reduce allocations and memory (#1463)
* Optimize G115, G602, G407 analyzers to reduce allocations and memory

* improve G407 coverage
2026-01-13 19:00:33 +01:00
oittaa 833d7919e0 refactor(g115): improve coverage (#1462) 2026-01-12 11:37:18 +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
renovate[bot] 303f84d111 chore(deps): update all dependencies (#1461)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-01-12 09:51:22 +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
oittaa 52f5dbf4d4 feat(slice): enhance slice bounds analysis with dynamic bounds handling (#1457)
* feat(slice): enhance slice bounds analysis with dynamic bounds handling

* feat(slice): enhance extractLenBound to support additional offset patterns and improve slice bounds analysis

* golangci-lint run

* Improve G602 slice bounds detection: support 3-index slices and correct capacity tracking

* Support out-of-bounds detection for range loops with offsets
2026-01-09 13:52:05 +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 8a5404eabf feat(resolve): enhance TryResolve to handle KeyValueExpr, IndexExpr, and SliceExpr (#1452)
* feat(resolve): enhance TryResolve to handle KeyValueExpr, IndexExpr, and SliceExpr

* golangci-lint
2026-01-04 17:22:20 +02: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
renovate[bot] 095d529a90 chore(deps): update all dependencies (#1443)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2025-12-30 10:18:39 +02:00
Cosmin Cojocar c073629009 Improve slice bound check (#1442)
Improve slice bound check to habdle bounded values and properly parse
the address index only from references

Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
2025-12-28 19:39:40 +02:00
Ranyodh Singh 538a05cc5d docs: add documentation for using gosec with private modules (#1441)
* docs: add documentation for using gosec with private modules

Add a new section in the GitHub Action documentation explaining
how to configure gosec to work with projects that import private
Go modules.
This includes setting `GOPRIVATE` and `GITHUB_AUTHENTICATION_TOKEN`
environment variables.

* Update README.md
2025-12-19 09:41:56 +01:00