-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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
Add regex validation for the entity id filter #89991
Add regex validation for the entity id filter #89991
Conversation
Hi @flip-dots It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
@@ -120,6 +120,11 @@ async def get( | |||
entity_ids = None | |||
if entity_ids_str: | |||
entity_ids = entity_ids_str.lower().split(",") | |||
for entity_id in entity_ids: | |||
if not valid_entity_id(entity_id): |
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.
Please check if it's in the state machine first before calling this check as it can get expensive if the entity id list is large
Also this needs a test.
Thanks
Hi @flip-dots It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
Hi @flip-dots It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
Co-authored-by: J. Nick Koston <nick@koston.org>
Hi @flip-dots It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
Hi @flip-dots It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
This was causing some of the tests to fail, I guess because in some circumstances it might be desirable to subscribe to the state stream of a non-existent entity, so its probably best to just check to make sure that the entitiy_id is valid and not care if it doesnt exist.
Hi @flip-dots It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
As the CLA isn't signed yet, this PR can't be reviewed or merged. Marking this PR draft for now. Please mark it ready for review, once you've signed the CLA. ../Frenck |
for entity_id in entity_ids: | ||
if not hass.states.get(entity_id) or not valid_entity_id(entity_id): | ||
return self.json_message( | ||
"Invalid filter_entity_id", HTTPStatus.BAD_REQUEST | ||
) |
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.
Random reviewer here 🙋🏽♂️, no context on what the more extensive code is doing (they said I need to review some to expedite my own review, so...), but couldn't you compile an array of error messages and return altogether?
Also, any particular reason for not reversing the logical check in the IF statement on line 124?
As it stands, your code allows the caller to obtain the state of the entity (if it exists), even though that ID might be "invalid"
for entity_id in entity_ids: | |
if not hass.states.get(entity_id) or not valid_entity_id(entity_id): | |
return self.json_message( | |
"Invalid filter_entity_id", HTTPStatus.BAD_REQUEST | |
) | |
for entity_id in entity_ids: | |
if not valid_entity_id(entity_id) or not hass.states.get(entity_id): | |
return self.json_message( | |
"Invalid filter_entity_id", HTTPStatus.BAD_REQUEST | |
) |
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.
Now that I think about it, the regex check in the REST API is redundant when we also check the state machine, since I think we can assume that if an entitiy_id is in the state machine it must be valid, so doing a regex check only makes sense for the Web Socket API where you can subscribe to entity_ids which don't exist yet.
Breaking change
Proposed change
This adds regex validation for the filter_entity_id parameter in the history part of the REST API to make sure that the provided entity IDs are valid. Previously the API would just return an empty array instead of an error message, which is (probably) not expected behaviour.
This makes it easier to spot if you made a mistake in your request formatting, as you now get a specific error instead of an empty array which could be confusing.
Also for the WebSocket API
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: