-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Gateway: add support for HTTP OPTIONS request type #2232
Merged
Merged
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 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 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/sh | ||
# | ||
# Copyright (c) 2016 Marcin Rataj | ||
# MIT Licensed; see the LICENSE file in this repository. | ||
# | ||
|
||
test_description="Test HTTP Gateway CORS Support" | ||
|
||
test_config_ipfs_cors_headers() { | ||
ipfs config --json Gateway.HTTPHeaders.Access-Control-Allow-Origin '["*"]' | ||
ipfs config --json Gateway.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]' | ||
ipfs config --json Gateway.HTTPHeaders.Access-Control-Allow-Headers '["X-Requested-With"]' | ||
|
||
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' | ||
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]' | ||
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["X-Requested-With"]' | ||
} | ||
|
||
. lib/test-lib.sh | ||
|
||
test_init_ipfs | ||
test_config_ipfs_gateway_readonly $ADDR_GWAY | ||
test_config_ipfs_cors_headers | ||
test_launch_ipfs_daemon | ||
|
||
gwport=$PORT_GWAY | ||
apiport=$PORT_API | ||
thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' | ||
|
||
# Gateway | ||
|
||
# HTTP GET Request | ||
test_expect_success "GET to Gateway succeeds" ' | ||
curl -svX GET "http://127.0.0.1:$gwport/ipfs/$thash" 2>curl_output | ||
' | ||
# GET Response from Gateway should contain CORS headers | ||
test_expect_success "GET response for Gateway resource looks good" ' | ||
grep "Access-Control-Allow-Origin:" curl_output && | ||
grep "Access-Control-Allow-Methods:" curl_output && | ||
grep "Access-Control-Allow-Headers:" curl_output | ||
' | ||
|
||
# HTTP OPTIONS Request | ||
test_expect_success "OPTIONS to Gateway succeeds" ' | ||
curl -svX OPTIONS "http://127.0.0.1:$gwport/ipfs/$thash" 2>curl_output | ||
' | ||
# OPTION Response from Gateway should contain CORS headers | ||
test_expect_success "OPTIONS response for Gateway resource looks good" ' | ||
grep "Access-Control-Allow-Origin:" curl_output && | ||
grep "Access-Control-Allow-Methods:" curl_output && | ||
grep "Access-Control-Allow-Headers:" curl_output | ||
' | ||
|
||
# Read-Only API (at the Gateway Port) | ||
|
||
# HTTP GET Request | ||
test_expect_success "GET to API succeeds" ' | ||
curl -svX GET "http://127.0.0.1:$gwport/api/v0/cat?arg=$thash" 2>curl_output | ||
' | ||
# GET Response from the API should NOT contain CORS headers | ||
# Blacklisting: https://git.io/vzaj2 | ||
# Rationale: https://git.io/vzajX | ||
test_expect_success "OPTIONS response for API looks good" ' | ||
grep -q "Access-Control-Allow-" curl_output && false || true | ||
' | ||
|
||
# HTTP OPTIONS Request | ||
test_expect_success "OPTIONS to API succeeds" ' | ||
curl -svX OPTIONS "http://127.0.0.1:$gwport/api/v0/cat?arg=$thash" 2>curl_output | ||
' | ||
# OPTIONS Response from the API should NOT contain CORS headers | ||
test_expect_success "OPTIONS response for API looks good" ' | ||
grep -q "Access-Control-Allow-" curl_output && false || true | ||
' | ||
|
||
test_kill_ipfs_daemon | ||
|
||
test_done |
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.
Is this enabled in both ro and writable case? Shouldn't it be enabled for ro only, ref: #934 (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 feel we should always produce
OPTIONS
response.Gateway owner can decide what types of requests are accepted by customizing
Access-Control-Allow-Method
header (GET
is ro,POST
,PUT
,DELETE
are rw).All major browsers respect this header and refuse to do XHR requests if method is not whitelisted.
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.
ic