-
Notifications
You must be signed in to change notification settings - Fork 10
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
Flip insecure defined flask session config #119
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #119 +/- ##
==========================================
+ Coverage 96.33% 96.46% +0.12%
==========================================
Files 66 67 +1
Lines 2729 2828 +99
==========================================
+ Hits 2629 2728 +99
Misses 100 100
|
ba75b0f
to
1a58226
Compare
1a58226
to
b45b257
Compare
b45b257
to
0b21ebd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor issues, otherwise it looks good.
flask_app_parent = self.get_metadata(ParentNodeProvider, original_node) | ||
match flask_app_parent: | ||
case cst.AnnAssign() | cst.Assign(): | ||
flask_app_attr = flask_app_parent.targets[0].target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work with AnnAssign
, as the target value is stored in the target
attribute. You should separate the cases.
This may help:
def _extract_targets_of_assignment(
self, assignment: cst.AnnAssign | cst.Assign | cst.WithItem | cst.NamedExpr
) -> list[cst.BaseExpression]:
match assignment:
case cst.AnnAssign():
if assignment.target:
return [assignment.target]
case cst.Assign():
return [t.target for t in assignment.targets]
case cst.NamedExpr():
return [assignment.target]
case cst.WithItem():
if assignment.asname:
return [assignment.asname.name]
return []
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you wrote that function and it isn't from somewhere I should cite, so I'm taking it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote it for the file resource leak codemod. This PR will probably be merged before, so go ahead.
match flask_app_parent: | ||
case cst.AnnAssign() | cst.Assign(): | ||
flask_app_attr = flask_app_parent.targets[0].target | ||
self.flask_app_name = flask_app_attr.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assumes that the target is a Name
, but the target may be any slew of different types of expressions like Subscript
:(l[0] = Flask(...)
) , Attribute
:(a.b = Flask(...)
), etc. So you should make it fail in the other cases.
My suggestion is to break this detection part into a new method is_assigned_to_variable
with return Optional[Name]
.
config = cst.Name(value="config") | ||
app_name = cst.Name(value=self.flask_app_name) | ||
app_config_node = cst.Attribute(value=app_name, attr=config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using matchers.matches
, you should use the matchers
version of the nodes instead (i.e. matchers.Name
instead of cst.Name
).
See: https://libcst.readthedocs.io/en/latest/matchers.html#libcst.matchers.matches
config = cst.Name(value="config") | ||
app_name = cst.Name(value=self.flask_app_name) | ||
app_config_node = cst.Attribute(value=app_name, attr=config) | ||
update = cst.Name(value="update") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment in _is_config_subscript
. Same issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all of the back and forth on this, I know it ended up being fairly tricky.
Based on the expected behavior in the tests this looks good to me. My only substantive comment is to remove the file that doesn't belong in this PR. Once @andrecsilva's other comments are addressed this should be good to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file doesn't belong in this PR, it looks like it's from another ticket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change does belong here, I didn't add the file, I just moved a function to utils so I had to change the import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry my bad.
91b0ebb
to
3bbbfee
Compare
Overview
A codemod that will check if
SESSION_COOKIE_HTTPONLY
,SESSION_COOKIE_SECURE
orSESSION_COOKIE_SAMESITE
are set to an insecure value on a flask app config object.Description