-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add dict instance check before accessing it #15944
Add dict instance check before accessing it #15944
Conversation
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.
@rajuniit Thanks for this. I'm curious what the rationale for this change was? I'm just wondering if it would be better to give a better error message instead, rather than ignore a malformed body?
Thank you @erikjohnston for your feedback. I think there is already an implementation where if there is no 'sid', it will create a new 'sid'. That's why I believe we shouldn't block this execution, like this.
Also, we have a check in the later code
Maybe we can just log the error into the logger. what do you think? |
It sounds like a major error if |
Yes, it is from client data. And I think we are sending an error message back to the user if authdict is not defined. From this code block
The new check has been added to handle 500 errors only. |
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.
Is there anything I need to change to approve it?
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.
Is there anything I need to change to approve it?
I think we want to send a similar error back if the authdict is not a dictionary. Right now we just ignore that it is the wrong data and attempt to continue processing.
I have added an error response. |
@clokep is this failed test case related to my changes? Because I don't find anything related to it. Also, I see the same failed test case in other pull requests. |
@clokep Do I need to wait for the reviewer? Or anything I need to do from my side? |
Yes, this is in the review queue and the team will look at it when they have time. |
if not isinstance(authdict, dict): | ||
raise SynapseError( | ||
400, | ||
"Interactive auth not yet complete. Client data is not dictionary.", | ||
) |
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 had a chance to look deeper at this; check_ui_auth
has 3 callers:
validate_user_via_ui_auth
, this has a handful of callers.PasswordRestServlet.on_POST
RegisterRestServlet.on_POST
Some of these properly validate that auth
is None
or a dictionary. Others don't have validation (see #13147). I'm wondering if we should do validation up front instead of at this stage.
@DMRobertson It looks like you started adding the UI Auth validation of REST endpoints. Did you have a plan here?
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 didn't have a specific plan, I'm afraid. I vaguely remember UI auth being confusing when I looked it it, but that was about a year ago.
My general plan was to do as much validation as possible in the rest layer, and pass a parsed Pydantic model instance down to the handlers. The two can be done separately, which makes it easier to validate one endpoint at a time.
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.
There is an vaguely open question about how we should be using Pydantic going forward. It has just made a 2.x release with a strict mode built-in (no more check_pydantic_models.py) and performance improvements. There are some breaking changes though, plus some of our packagers are presumably going to be shipping Pydantic 1.x for some time. #15979 has some more detailed discussion.
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.
Hi @clokep @DMRobertson, was some decision made on Pydantic? What should we do here for now?
If this function is called from every rest endpoint at the beginning should we keep it here, or move it upfront to rest layers which are using this.
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.
@prajjawal05 I think adding the validation would be a good step forward. I don't agree there's an open question. Today we're using pydantic 1.x and should not block good work because we might one-day update to pydantic 2.x.
As mentioned, I think this is the wrong approach. We should be doing validation of the data at the REST layer to ensure that the expected fields exist. |
Pull Request Checklist
EventStore
toEventWorkerStore
.".code blocks
.(run the linters)