Skip to content

Conversation

@RasmusWL
Copy link
Member

@RasmusWL RasmusWL commented Sep 2, 2021

After creating elaborate tests for how you can use the ORM to construct queries, I realized it would be difficult to make our normal SQL injection query ever flag those, so did a 180 on how to handle it, and ended up adding a new query.

We could also extend our existing SQL-injection query to also flag up TextClause injection. I felt that putting them together would end up being a bit confusing, but I'm very open to changing this if it is cause for concern.

I've never seen this being used in real code, and this library doesn't
have a lot of traction, so I would rather not commit to supporting it
(which includes verifying that it actually makes things safe).

Personally I don't think this is the right approach for avoiding SQL
injection either.
Due to the split between `src/` and `lib/`, I was not really able to do
the next step without having moved the SQLAlchemy modeling over to be in
`lib/` as well.
instead of doing this via taint-steps. See description in code/tests.
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

Quite impressive testing here and also the work to complete the modelling 💪
I thought it over, and it does seem quite reasonable (as in not overly FP-prone) to always alert on text objects being user controlled. If such objects are not executed, the code can probably be simplified and the alert would have been still useful. So the two-query-approach seems fine here.

It might be nice to add an inline style test for the bonus query, but I am happy to merge without it. The requested changes only really comprise the one typo I spotted...

results = session.execute(stmt).scalars().all()


# All of these should be flagged by query
Copy link
Contributor

Choose a reason for hiding this comment

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

...and they are :-) I guess that would be more clear from an inline test.

Co-authored-by: yoff <lerchedahl@gmail.com>
@RasmusWL
Copy link
Member Author

It might be nice to add an inline style test for the bonus query, but I am happy to merge without it. The requested changes only really comprise the one typo I spotted...

Yes. I think we have something to learn from JS here, since they have a standard test setup where they use something like # $ OK and # $ NOT_OK to ensure proper flow.

@RasmusWL RasmusWL requested a review from yoff September 20, 2021 10:07
yoff
yoff previously approved these changes Sep 20, 2021
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

LGTM as soon as the subpaths are added to the test expectations.

Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

LGTM

@yoff yoff merged commit 4adb0c7 into github:main Sep 21, 2021
@RasmusWL RasmusWL deleted the promote-sqlalchemy branch September 21, 2021 10:02
RasmusWL added a commit to RasmusWL/codeql that referenced this pull request Sep 21, 2021
As discussed in a meeting today, this will end up presenting an query
suite that's easier to use for customers.

Since github#6589 has JUST been merged,
if we get this change in fast enough, no end-user will ever have run
`py/sqlalchemy-textclause-injection` as part of LGTM.com or Code
Scanning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants