Skip to content
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

test(gw): improve CORS tests #8718

Merged
merged 1 commit into from
Aug 17, 2022
Merged

test(gw): improve CORS tests #8718

merged 1 commit into from
Aug 17, 2022

Conversation

lidel
Copy link
Member

@lidel lidel commented Feb 4, 2022

This PR cleans up old CORS tests and adds more resolution:

'
# OPTIONS Response from the API should NOT contain CORS headers
test_expect_success "OPTIONS response for API looks good" '
test_expect_success "OPTIONS response from {gw}/api/v0 has no CORS header" '
cat curl_output &&
grep -q "Access-Control-Allow-" curl_output && false || true
'
Copy link

@mar1n3r0 mar1n3r0 Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for the examples you have provided here : #7667

# HTTP OPTIONS Request
test_expect_success "OPTIONS to {gw}/api/v0 succeeds" '
  curl -svX OPTIONS -H "Origin: https://example.com" "http://127.0.0.1:$GWAY_PORT/api/v0/cat?arg=$thash" 2>curl_output
'
# OPTIONS Response from the API should NOT contain CORS headers
test_expect_success "OPTIONS response from {gw}/api/v0 has no CORS header" '
  cat curl_output &&
  grep -q "Access-Control-Allow-" curl_output && false || true
'

test_kill_ipfs_daemon

# Test CORS safelisting of standard headers
test_expect_success "Can configure standard CORS headers" '
  ipfs config --json Gateway.HTTPHeaders.Access-Control-Allow-Headers "[\"Content-Disposition\"]" &&
  ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin  "[\"https://example.com\"]" &&
  ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\"]"
'

test_launch_ipfs_daemon

# HTTP OPTIONS POST Request
test_expect_success "OPTIONS to {gw}/api/v0 CORS headers" '
  curl -svX OPTIONS -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: Content-Disposition" -H "Origin: https://example.com" "http://127.0.0.1:$GWAY_PORT/api/v0/cat?arg=$thash" 2>curl_output
'

# OPTIONS POST Response from the API should contain custom CORS headers
test_expect_success "OPTIONS response from {gw}/api/v0 has custom CORS header" '
  test_expect_code 1 grep "< Access-Control-Allow-Headers: Content-Disposition" curl_output
'

# OPTIONS POST Response from the API should contain default CORS headers
test_expect_success "OPTIONS response from {gw}/api/v0 has default CORS headers " '
  grep "< Access-Control-Allow-Methods: POST" curl_output &&
  grep "< Access-Control-Allow-Origin: https://example.com" curl_output
'

grep "< Access-Control-Allow-Origin: localhost" curl_output
'

# Read-Only API (at the Gateway Port)
# Read-Only /api/v0 RPC API (exposed on the Gateway Port)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_expect_success "OPTIONS to RPC API (exposed on the Gateway Port) with a custom header succeeds" '
  curl -svX OPTIONS -H "Origin: https://example.com" -H "Access-Control-Request-Headers: Content-Disposition" "http://127.0.0.1:$GWAY_PORT/api/v0/cat?args=$thash" 2>curl_output &&
  cat curl_output
'
test_expect_success "Access-Control-Allow-Headers extends the previous implicit list" '
  test_expect_code 1 grep "< Access-Control-Allow-Headers: Content-Disposition" curl_output
'

@BigLep BigLep added this to the Best Effort Track milestone Mar 10, 2022
@lidel lidel force-pushed the fix/api-headers-cors-preflight branch 2 times, most recently from c413030 to 81e2477 Compare August 17, 2022 00:55
@lidel lidel marked this pull request as ready for review August 17, 2022 01:09
@lidel
Copy link
Member Author

lidel commented Aug 17, 2022

I've rebased this to get more confidence around CORS behavior ( && false || true bits were 🙈)

Thank you for submitting tests in comments, they will be useful when someone has time for #7667

@lidel lidel enabled auto-merge (rebase) August 17, 2022 01:14
@lidel lidel disabled auto-merge August 17, 2022 01:18
@lidel lidel marked this pull request as draft August 17, 2022 02:02
@lidel lidel force-pushed the fix/api-headers-cors-preflight branch from 81e2477 to a08fffc Compare August 17, 2022 02:26
@lidel lidel marked this pull request as ready for review August 17, 2022 02:28
@lidel lidel force-pushed the fix/api-headers-cors-preflight branch from a08fffc to f2fd841 Compare August 17, 2022 02:29
@lidel lidel enabled auto-merge (rebase) August 17, 2022 02:33
This cleans up old CORS tests and adds more resolution
(proper Origin test, testing custom header behavior)

It also adds basic regression tests for /api/v0
subset exposed on Gateway port.
@lidel lidel force-pushed the fix/api-headers-cors-preflight branch from f2fd841 to 7b01368 Compare August 17, 2022 02:51
@lidel lidel disabled auto-merge August 17, 2022 03:00
@lidel lidel merged commit 84ed0ec into master Aug 17, 2022
@lidel lidel deleted the fix/api-headers-cors-preflight branch August 17, 2022 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

CORS preflight requests to RPC API with custom headers
3 participants