-
Notifications
You must be signed in to change notification settings - Fork 9
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
Provide request input sanitization #358
Conversation
|
||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
if "method" not in scope or scope["method"] in ("GET", "HEAD", "OPTIONS"): |
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 just went with those methods. Please adjust this line, if we want to make this middleware more specific to special header configurations.
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.
No POST?
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 conditional asserts all exclude options as true. Since we like to sanitize POST, it's not listed here. Or do I misunderstood something?
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.
oh by returning none it will move forward with the middleware? if so can you explicitly return none.
if thats not it, i need to look at the starlette docs again 😅
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.
ohh. the sanitize functions are in call. okay i just cant read indents today. my b.
We might want to sanitize GETs though. we can coms back to that
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.
Only the sanitize_request_body
def is in __call__
, sorry if this is confusing 🙈 When I implemented it, it made perfectly sense 😂 Always open for suggestions.
if isinstance(value, dict): | ||
json_body[key] = __class__.sanitize_dict(value) | ||
elif isinstance(value, list): | ||
json_body[key] = __class__.sanitize_list(value) | ||
else: | ||
json_body[key] = __class__.sanitize_str(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.
I thought handling dicts, lists and strings would be sufficient for now. But let me know, if we need more checks here.
|
||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
if "method" not in scope or scope["method"] in ("GET", "HEAD", "OPTIONS"): |
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.
No POST?
return message | ||
if not isinstance(body, bytes) : | ||
return message | ||
json_body = json.loads(body) |
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 we use formData for login/password form. So this might not work here. 🤔
I guess you could catch the exception and sanitize body input itself.
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.
Good point, will add that
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 provided a is_json()
utility function now.
@MelissaAutumn This PR should be ready now, I added an example test case for schedule updates. We can add more tests for different endpoints in the future, if we need them. |
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.
Looks good now! Thanks!
I'm guessing this doesn't sanitize formData? If not we can look into other solutions for that but I don't think it's a problem right now since we don't exactly have it on stage/prod.
Since we're only using formData for temporary dev login for now, let's take a look at that again if we need it somewhere else. |
* ➕ Provide request input sanitization * 🔨 Check for json request body * ➕ Add test for input sanitization
Description of the Change
This PR provides a new middleware to strip all html tags from data changing requests using nh3.
I added one test case for schedule updates, we can add more if we need them at other places.
Benefits
Prevent XSS.
Applicable Issues
Closes #263