-
Notifications
You must be signed in to change notification settings - Fork 2
feat: allow users to restrict http requests to certain paths #285
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
Open
Sl1mb0
wants to merge
2
commits into
main
Choose a base branch
from
tm/allow-certain-http-paths
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,7 @@ def perform_request(url: str) -> str: | |
| .mount(&server) | ||
| .await; | ||
|
|
||
| let mut permissions = AllowCertainHttpRequests::new(); | ||
| let mut permissions = AllowCertainHttpRequests::default(); | ||
| permissions.allow(HttpRequestMatcher { | ||
| method: http::Method::GET, | ||
| host: server.address().ip().to_string().into(), | ||
|
|
@@ -638,7 +638,7 @@ def perform_request(url: str) -> str: | |
|
|
||
| // deliberately use a runtime what we are going to throw away later to prevent tricks like `Handle::current` | ||
| let udf = rt_tmp.block_on(async { | ||
| let mut permissions = AllowCertainHttpRequests::new(); | ||
| let mut permissions = AllowCertainHttpRequests::default(); | ||
| permissions.allow(HttpRequestMatcher { | ||
| method: http::Method::GET, | ||
| host: server.address().ip().to_string().into(), | ||
|
|
@@ -677,3 +677,153 @@ def perform_request(url: str) -> str: | |
| &StringArray::from_iter([Some("hello world!".to_owned()),]) as &dyn Array, | ||
| ); | ||
| } | ||
|
|
||
|
Collaborator
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. After you've changed it to a "prefixes are matcher-specific", you should probably also harded |
||
| #[tokio::test] | ||
| async fn test_allowed_http_request_path() { | ||
| const CODE: &str = r#" | ||
| import requests | ||
|
|
||
| def perform_request(url: str) -> str: | ||
| return requests.get(url).text | ||
| "#; | ||
|
|
||
| let server = MockServer::start().await; | ||
| Mock::given(matchers::any()) | ||
| .respond_with(ResponseTemplate::new(200).set_body_string("hello world!")) | ||
| .expect(1) | ||
| .mount(&server) | ||
| .await; | ||
|
|
||
| let allowed_paths = vec!["/allowed".to_string()]; | ||
|
|
||
| let mut permissions = AllowCertainHttpRequests::new(allowed_paths); | ||
| permissions.allow(HttpRequestMatcher { | ||
| method: http::Method::GET, | ||
| host: server.address().ip().to_string().into(), | ||
| port: server.address().port(), | ||
| }); | ||
| let udf = python_udf_with_permissions(CODE, permissions).await; | ||
|
|
||
| let array = udf | ||
| .invoke_async_with_args(ScalarFunctionArgs { | ||
| args: vec![ColumnarValue::Scalar(ScalarValue::Utf8(Some(format!( | ||
| "{}/allowed", | ||
| server.uri() | ||
| ))))], | ||
| arg_fields: vec![Arc::new(Field::new("uri", DataType::Utf8, true))], | ||
| number_rows: 1, | ||
| return_field: Arc::new(Field::new("r", DataType::Utf8, true)), | ||
| config_options: Arc::new(ConfigOptions::default()), | ||
| }) | ||
| .await | ||
| .unwrap() | ||
| .unwrap_array(); | ||
|
|
||
| assert_eq!( | ||
| array.as_ref(), | ||
| &StringArray::from_iter([Some("hello world!".to_owned()),]) as &dyn Array, | ||
| ); | ||
|
|
||
| let err = udf | ||
| .invoke_async_with_args(ScalarFunctionArgs { | ||
| args: vec![ColumnarValue::Scalar(ScalarValue::Utf8(Some(format!( | ||
| "{}/not_allowed", | ||
| server.uri() | ||
| ))))], | ||
| arg_fields: vec![Arc::new(Field::new("uri", DataType::Utf8, true))], | ||
| number_rows: 1, | ||
| return_field: Arc::new(Field::new("r", DataType::Utf8, true)), | ||
| config_options: Arc::new(ConfigOptions::default()), | ||
| }) | ||
| .await | ||
| .unwrap_err(); | ||
|
|
||
| insta::assert_snapshot!( | ||
| err.to_string(), | ||
| @r#" | ||
| cannot call function | ||
| caused by | ||
| Execution error: Traceback (most recent call last): | ||
| File "/lib/python3.14/site-packages/urllib3/connectionpool.py", line 787, in urlopen | ||
| response = self._make_request( | ||
| conn, | ||
| ...<10 lines>... | ||
| **response_kw, | ||
| ) | ||
| File "/lib/python3.14/site-packages/urllib3/connectionpool.py", line 493, in _make_request | ||
| conn.request( | ||
| ~~~~~~~~~~~~^ | ||
| method, | ||
| ^^^^^^^ | ||
| ...<6 lines>... | ||
| enforce_content_length=enforce_content_length, | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ) | ||
| ^ | ||
| File "/lib/python3.14/site-packages/urllib3/contrib/wasi/connection.py", line 124, in request | ||
| self._response = wasi.send_request(request) | ||
| ~~~~~~~~~~~~~~~~~^^^^^^^^^ | ||
| File "/lib/python3.14/site-packages/urllib3/contrib/wasi/wasi.py", line 79, in send_request | ||
| raise errors.WasiErrorCode(str(response.value.value)) | ||
| urllib3.contrib.wasi.errors.WasiErrorCode: Request failed with wasi http error ErrorCode_HttpRequestDenied | ||
|
|
||
| During handling of the above exception, another exception occurred: | ||
|
|
||
| Traceback (most recent call last): | ||
| File "/lib/python3.14/site-packages/requests/adapters.py", line 644, in send | ||
| resp = conn.urlopen( | ||
| method=request.method, | ||
| ...<9 lines>... | ||
| chunked=chunked, | ||
| ) | ||
| File "/lib/python3.14/site-packages/urllib3/connectionpool.py", line 841, in urlopen | ||
| retries = retries.increment( | ||
| method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2] | ||
| ) | ||
| File "/lib/python3.14/site-packages/urllib3/util/retry.py", line 474, in increment | ||
| raise reraise(type(error), error, _stacktrace) | ||
| ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| File "/lib/python3.14/site-packages/urllib3/util/util.py", line 38, in reraise | ||
| raise value.with_traceback(tb) | ||
| File "/lib/python3.14/site-packages/urllib3/connectionpool.py", line 787, in urlopen | ||
| response = self._make_request( | ||
| conn, | ||
| ...<10 lines>... | ||
| **response_kw, | ||
| ) | ||
| File "/lib/python3.14/site-packages/urllib3/connectionpool.py", line 493, in _make_request | ||
| conn.request( | ||
| ~~~~~~~~~~~~^ | ||
| method, | ||
| ^^^^^^^ | ||
| ...<6 lines>... | ||
| enforce_content_length=enforce_content_length, | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ) | ||
| ^ | ||
| File "/lib/python3.14/site-packages/urllib3/contrib/wasi/connection.py", line 124, in request | ||
| self._response = wasi.send_request(request) | ||
| ~~~~~~~~~~~~~~~~~^^^^^^^^^ | ||
| File "/lib/python3.14/site-packages/urllib3/contrib/wasi/wasi.py", line 79, in send_request | ||
| raise errors.WasiErrorCode(str(response.value.value)) | ||
| urllib3.exceptions.ProtocolError: ('Connection aborted.', WasiErrorCode('Request failed with wasi http error ErrorCode_HttpRequestDenied')) | ||
|
|
||
| During handling of the above exception, another exception occurred: | ||
|
|
||
| Traceback (most recent call last): | ||
| File "<string>", line 5, in perform_request | ||
| File "/lib/python3.14/site-packages/requests/api.py", line 73, in get | ||
| return request("get", url, params=params, **kwargs) | ||
| File "/lib/python3.14/site-packages/requests/api.py", line 59, in request | ||
| return session.request(method=method, url=url, **kwargs) | ||
| ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| File "/lib/python3.14/site-packages/requests/sessions.py", line 589, in request | ||
| resp = self.send(prep, **send_kwargs) | ||
| File "/lib/python3.14/site-packages/requests/sessions.py", line 703, in send | ||
| r = adapter.send(request, **kwargs) | ||
| File "/lib/python3.14/site-packages/requests/adapters.py", line 659, in send | ||
| raise ConnectionError(err, request=request) | ||
| requests.exceptions.ConnectionError: ('Connection aborted.', WasiErrorCode('Request failed with wasi http error ErrorCode_HttpRequestDenied')) | ||
| "#, | ||
| ); | ||
| } | ||
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.
I think the paths should be bound to the matcher since different hosts may have different path filters, i.e. it's likely
matchers: HasMap<HttpRequestMatcher, AllowedHttpPath>, although that interface becomes a bit of a mess. I suggest the following: the public interface is based onHttpRequestMatcherand you add the add the list of allowed prefixes to that struct. For fast internal filtering I think you have two options, since you now need to replace theHashSetinAllowCertainHttpRequests:HashMap<MatcherWithoutPrefixes, Prefixes>method\0host\0port\0prefixland perform a binary search on that data