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

Fix CORs support in dataAPI #1180

Merged
merged 1 commit into from
Jan 29, 2025
Merged

Fix CORs support in dataAPI #1180

merged 1 commit into from
Jan 29, 2025

Conversation

pschork
Copy link
Contributor

@pschork pschork commented Jan 29, 2025

This change adds OPTIONS request support to DataAPI gin router to allow CORS requests.

DataAPI was not setup to respond to HTTP:OPTIONS request which is used by browser to determine CORS options before it actually attempts the CORS request. Effectively dataAPI CORS support was broken and any CORS request was rejected.

Configure allowed CORS origin

DATA_ACCESS_API_ALLOW_ORIGINS=https://allowed-origin.xyz
DATA_ACCESS_API_SERVER_MODE=release

Verify HTTP:OPTIONS request succeeds for origin https://allowed-origin.xyz

curl -v -X OPTIONS "http://localhost:8080/api/v2/blobs/feed?limit=10" -H "Origin: https://allowed-origin.xyz"
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> OPTIONS /api/v2/blobs/feed?limit=10 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.6.0
> Accept: */*
> Origin: https://allowed-origin.xyz
>
< HTTP/1.1 204 No Content
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization
< Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS
< Access-Control-Allow-Origin: https://allowed-origin.xyz
< Access-Control-Max-Age: 43200
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Date: Wed, 29 Jan 2025 03:59:01 GMT

Verify HTTP:OPTIONS request BLOCKED for origin https://foobar.com:1234

curl -v -X OPTIONS "http://localhost:8080/api/v2/blobs/feed?limit=10" -H "Origin: http://foobar.com:1234"
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> OPTIONS /api/v2/blobs/feed?limit=10 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.6.0
> Accept: */*
> Origin: http://foobar.com:1234
>
< HTTP/1.1 403 Forbidden
< Date: Wed, 29 Jan 2025 03:58:54 GMT
< Content-Length: 0

This changes adds `OPTIONS` endpoint to DataAPI gin router.

DataAPI was not setup to respond to `HTTP:OPTIONS` request which is used
by browser to determine CORS options before it actually attempts the
CORS request. Effectively dataAPI CORS support was broken.

Configure allowed CORS origin
```
DATA_ACCESS_API_ALLOW_ORIGINS=https://blob-explorer-v2-preprod.vercel.app
```

Verify `HTTP:OPTIONS` request succeeds for origin `https://blob-explorer-v2-preprod.vercel.app`
```
curl -v -X OPTIONS "http://localhost:8080/api/v2/blobs/feed?limit=10" -H "Origin: https://blob-explorer-v2-preprod.vercel.app"
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> OPTIONS /api/v2/blobs/feed?limit=10 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.6.0
> Accept: */*
> Origin: https://blob-explorer-v2-preprod.vercel.app
>
< HTTP/1.1 204 No Content
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization
< Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS
< Access-Control-Allow-Origin: https://blob-explorer-v2-preprod.vercel.app
< Access-Control-Max-Age: 43200
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Date: Wed, 29 Jan 2025 03:59:01 GMT
```

Verify `HTTP:OPTIONS` request BLOCKED for origin `https://foobar.com:1234`
```
curl -v -X OPTIONS "http://localhost:8080/api/v2/blobs/feed?limit=10" -H "Origin: http://foobar.com:1234"
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> OPTIONS /api/v2/blobs/feed?limit=10 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.6.0
> Accept: */*
> Origin: http://foobar.com:1234
>
< HTTP/1.1 403 Forbidden
< Date: Wed, 29 Jan 2025 03:58:54 GMT
< Content-Length: 0
```
@pschork pschork requested review from anupsv, dmanc and jianoaix January 29, 2025 04:05
basePath := "/api/v2"
docsv2.SwaggerInfoV2.BasePath = basePath
docsv2.SwaggerInfoV2.Host = os.Getenv("SWAGGER_HOST")

// Configure CORS
Copy link
Contributor

Choose a reason for hiding this comment

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

The order to set this up matters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See :233 CORS middleware needs to be created before routes.

@pschork pschork merged commit fefad3a into master Jan 29, 2025
10 checks passed
@pschork pschork deleted the pschork/fix_cors branch January 29, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants