Skip to content
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

Improve SQL check extensibility, add support for pgx #834

Closed
wants to merge 2 commits into from

Conversation

scop
Copy link
Contributor

@scop scop commented Jul 22, 2022

The SQL checks currently contain a number of hardwired/heuristic assumptions about the index of the argument taking an SQL string. The first commit here generalizes that.

The second one adds support for checking the SQL issues against the popular https://github.com/jackc/pgx family of PostgreSQL drivers and tools.

scop added 2 commits July 22, 2022 08:55
Remove hardwired assumption and heuristics on index of arg taking a SQL
string, be explicit about it instead.
@@ -4,6 +4,9 @@ package tools

// nolint
import (
_ "github.com/jackc/pgconn"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a blank import should be only in a main or test package, or have a comment justifying it

@scop scop marked this pull request as draft July 22, 2022 06:24
@codecov-commenter
Copy link

Codecov Report

Merging #834 (567d75a) into master (9a25f4e) will decrease coverage by 0.30%.
The diff coverage is 57.14%.

@@            Coverage Diff             @@
##           master     #834      +/-   ##
==========================================
- Coverage   74.35%   74.05%   -0.31%     
==========================================
  Files          50       50              
  Lines        3124     3134      +10     
==========================================
- Hits         2323     2321       -2     
- Misses        735      743       +8     
- Partials       66       70       +4     
Impacted Files Coverage Δ
rules/sql.go 74.64% <57.14%> (-4.76%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0212c83...567d75a. Read the comment docs.

@scop
Copy link
Contributor Author

scop commented Jul 22, 2022

Marking as a draft, because the second commit is work in progress; otherwise complete but lacking in test coverage.

Before completing it, I'd like to verify if the pgx support, once complete here, is something you'd welcome in gosec.

@ccojocar
Copy link
Member

Thanks for this contribution. I would keep the pgx stuff out of gosec since the tool is focused on standard Go package. Thanks for understanding.

@scop
Copy link
Contributor Author

scop commented Jul 29, 2022

Sure. It's unfortunate though, as there doesn't seem to be anything else one could easily use or extend to craft these checks for non-stdlib packages.

Anyway, I think the refactoring commit a18c3a0 still has value on its own. If you agree, I can submit it as another PR.

@ccojocar
Copy link
Member

@scop Thanks, I left some comments in the commit. I don't see it as a major improvement but rather a bit more complicated and not so obvious. If you can make it a bit more clear, keeping in mind that someone needs maybe at some point extend the method list, you can submit a PR.

@scop
Copy link
Contributor Author

scop commented Jul 29, 2022

The whole point of that commit is to make it easier for someone at some point to extend the method list. Before my commit, one has to modify two places in the code, and if the function to be added has the query in argument index > 1, or doesn't conform to the "ends with Context => +1" heuristic, the current code wouldn't work for that at all.

After that commit, one just adds the function name and its interesting argument index to sqlCallIdents, and it's done. Note that my second commit actually shows this in action: 567d75a -> just addition to the map and it's covered, no matter what the argument indexes are, no other code changes needed.

@ccojocar
Copy link
Member

ccojocar commented Aug 2, 2022

Thanks for clarifications! Please send that commit in a different pull request and close this one. Thanks again!

@scop
Copy link
Contributor Author

scop commented Aug 2, 2022

Done: #841

@scop scop closed this Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants