-
Notifications
You must be signed in to change notification settings - Fork 21
Add validation of query parameter for server requests #1230
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
Draft
clarasb
wants to merge
7
commits into
main
Choose a base branch
from
clarasb/1225-logs_unknown_query_parameters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
896af49
add allowed query parameter validation for static routes
clarasb 3bba90c
add test
clarasb b21b3cb
Merge branch 'main' into clarasb/1225-logs_unknown_query_parameters
clarasb 20cb143
update changelog
clarasb d3a05bf
apply formatting
clarasb 8a215ef
update tornado.py
clarasb fa2983e
Apply suggestion from @forman
clarasb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,45 @@ | |
|
|
||
| SERVER_CTX_ATTR_NAME = "__xcube_server_ctx" | ||
|
|
||
| def _assert_allowed_query_params( | ||
| request: tornado.httputil.HTTPServerRequest, allowed_query_params: Sequence[str] | ||
| ): | ||
| unknown_query_params = set(request.query_arguments) - set(allowed_query_params) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Query parameters should be, afaik, not case-sensitive. |
||
| if unknown_query_params: | ||
| raise tornado.web.HTTPError( | ||
| 404, | ||
| reason=( | ||
| f"Unknown query parameter(s): {', '.join(sorted(unknown_query_params))}" | ||
| ), | ||
| ) | ||
|
|
||
| class QueryValidatingRedirectHandler(tornado.web.RedirectHandler): | ||
| def initialize( | ||
| self, | ||
| url: str, | ||
| permanent: bool = True, | ||
| allowed_query_params: Sequence[str] = (), | ||
| ): | ||
| super().initialize(url, permanent=permanent) | ||
| self._allowed_query_params = allowed_query_params | ||
|
|
||
| def prepare(self): | ||
| _assert_allowed_query_params(self.request, self._allowed_query_params) | ||
|
|
||
|
|
||
| class QueryValidatingStaticFileHandler(tornado.web.StaticFileHandler): | ||
| def initialize( | ||
| self, | ||
| path: str, | ||
| default_filename: Optional[str] = None, | ||
| allowed_query_params: Sequence[str] = (), | ||
| ): | ||
| super().initialize(path=path, default_filename=default_filename) | ||
| self._allowed_query_params = allowed_query_params | ||
|
|
||
| def prepare(self): | ||
| _assert_allowed_query_params(self.request, self._allowed_query_params) | ||
|
|
||
|
|
||
| class TornadoFramework(Framework): | ||
| """ | ||
|
|
@@ -103,15 +142,30 @@ def add_static_routes(self, api_routes: Sequence[ApiStaticRoute], url_prefix: st | |
| for api_route in api_routes: | ||
| base_url = f"{url_prefix}{api_route.path}" | ||
| default_filename = api_route.default_filename | ||
| allowed_query_params = api_route.allowed_query_params | ||
| if allowed_query_params: | ||
| redirect_handler = QueryValidatingRedirectHandler | ||
| static_file_handler = QueryValidatingStaticFileHandler | ||
| redirect_kwargs = { | ||
| "url": f"{base_url}/", | ||
| "allowed_query_params": allowed_query_params, | ||
| } | ||
| static_file_kwargs = { | ||
| "path": api_route.dir_path, | ||
| "default_filename": default_filename, | ||
| "allowed_query_params": allowed_query_params, | ||
| } | ||
| else: | ||
| redirect_handler = tornado.web.RedirectHandler | ||
| static_file_handler = tornado.web.StaticFileHandler | ||
| redirect_kwargs = {"url": f"{base_url}/"} | ||
| static_file_kwargs = { | ||
| "path": api_route.dir_path, | ||
| "default_filename": default_filename, | ||
| } | ||
| handlers.append((f"{base_url}", redirect_handler, redirect_kwargs)) | ||
| handlers.append( | ||
| (f"{base_url}", tornado.web.RedirectHandler, {"url": f"{base_url}/"}) | ||
| ) | ||
| handlers.append( | ||
| ( | ||
| f"{base_url}/(.*)", | ||
| tornado.web.StaticFileHandler, | ||
| {"path": api_route.dir_path, "default_filename": default_filename}, | ||
| ) | ||
| (f"{base_url}/(.*)", static_file_handler, static_file_kwargs) | ||
| ) | ||
| LOG.log( | ||
| LOG_LEVEL_DETAIL, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So we only protect the routes that use this argument.
This is currently only
/viewer. An routes of the form/viewer/.../...? What others routes like/datasets?Suggestion:
allowed_query_paramscan also be abool. IfTrue, it can have any query params (like it is now). ifFalse, any query parameters are forbidden. The new default wouldFalseand we need to check, which other endpoints must be protected.