-
Notifications
You must be signed in to change notification settings - Fork 2.8k
server: prevent metadata checks in read-only mode #4250
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
server: prevent metadata checks in read-only mode #4250
Conversation
Could you write a test that runs your example |
|
Review app for commit 5124945 deployed to Heroku: https://hge-ci-pull-4250.herokuapp.com |
To be completely honest, I don't quite understand yet either. But it's the reason the tests in #4204 kept failing. Upon investigating, I found this error message: a read-only query that matches "drop". This causes the code to treat it as as an alteration, to run the metadata check... and then it fails with an internal postgres error. The only thing I don't know is why / when the console marks a query as being read only. But while writing that comment, I checked: it is simply a valid argument for run_sql, which means I know now how to write a test. ^^ |
|
Reading the code, AFAICT, the regex is only used to determine whether it can skip rebuilding the schema cache—executing the query in |
|
Review app for commit f227328 deployed to Heroku: https://hge-ci-pull-4250.herokuapp.com |
|
Here's a short write-up of the investigation. replicating the bugImagine the two following run_sql commands are sent to the server: The first one only creates a trivial view. What is interesting is what happens with the second one:
tl;dr: when hdb_views is not empty (1), a read_only (2) run_sql query that match words such as "drop" or "alter" (3) will erroneously trigger a metadata check and issue a drop while in read-only mode. fixing the bugThis PR offers a very simple solution to the problem: make the regex check for word boundaries. This is a quick-fix and it is not enough. The problem would still arise if one of the "bad words" were to be included in a string in the query, such as: There are three ways we could choose to tackle this; note that they are not mutually exclusive:
This PR goes towards (1), making the check more robust. I am not sure if / how the two other points are reasonable. |
I think that doing (1) is a Good Thing, since it’s clearly an improvement over the old code. However, I think that doing it alone is a bit sketchy, since it doesn’t actually fix the bug—it just sweeps it under the rug. The imprecision of the regex is a red herring, since the code was written with the intent that the regex would enable a conservative optimization, so any regex at all ought to do! The code ought to work if the regex were So I think we have to do (2) as well. We’ve discovered the bug, and the fix is easy, so we should fix it now! All we need to do is guard the use of |
f227328 to
7259821
Compare
|
I've added the check for read-only mode, added more tests, and rewrote the PR description. I am unsure whether this needs a changelog entry; it is only a minor bug, in the grand scheme of things... What do you think? |
|
Review app for commit 7259821 deployed to Heroku: https://hge-ci-pull-4250.herokuapp.com |
|
|
||
| isAltrDropReplace :: QErrM m => T.Text -> m Bool | ||
| isAltrDropReplace = either throwErr return . matchRegex regex False | ||
| containsMutationKeyword :: QErrM m => T.Text -> m Bool |
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 something along the lines of containsDDLKeyword might be better?
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 agree, and I’ve made this change.
|
Deploy preview for hasura-docs ready! Built with commit 2b5073c |
lexi-lambda
left a comment
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’ve pushed three changes to this PR:
-
I’ve incorporated @0x777’s suggestion.
-
I’ve refactored the code around the regex to perform the parsing of the pattern at compile-time instead of at runtime, so
containsDDLKeywordcan be total. -
I’ve added a
Notethat incorporates the discussion from slack.
* improve the isAltrDropReplace regex * do not perform the metadata check in read-only mode
2b5073c to
288bd6b
Compare
|
Review app for commit 288bd6b deployed to Heroku: https://hge-ci-pull-4250.herokuapp.com |
|
Review app https://hge-ci-pull-4250.herokuapp.com is deleted |
* do not perform the metadata check in read-only mode * improve the isAltrDropReplace regex * quote the regex at compile-time to handle syntax errors statically Co-authored-by: Alexis King <lexi.lambda@gmail.com>
Description
This fixes a bug where the server could perform a metadata check while in read-only mode, causing a postgres error. (For a full explanation of the bug, see this comment.)
This PR also slightly improves the regex used to identify whether a given request might need a metadata check. While it is now ok for it to aggressively check, this will reduce the number of false positives.
Changelog
CHANGELOG.mdis updated with user-facing content relevant to this PR.Affected components