Cross-Origin Request Blocked
See original GitHub issueI am using a React front-end with Envoy and a Go server. When I attempt to call the gRPC service from the front-end, the request gets blocked.
I’ve tried adding {"Access-Control-Allow-Origin": "*"} to the request metadata and have also tried disabling the pre-flight check when I get an instance of the GreeterClient but neither has worked. Can anyone advise on what I might try next?
Here is the React component that calls the servie:
import React from 'react';
import logo from './logo.svg';
import './App.css';
const { GreeterClient } = require('./helloworld_grpc_web_pb');
const { HelloRequest, HelloResponse } = require('./helloworld_pb.js');
var client = new GreeterClient('http://localhost:8080');
class Dashboard extends React.Component {
callGrpcService = () => {
const request = new HelloRequest();
request.setName('Hello World!');
client.sayHello(request, {"Access-Control-Allow-Origin": "*"}, (err, response) => {
if (response == null) {
console.log(err)
} else {
console.log(response)
}
});
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<button style={{ padding: 10 }} onClick={this.callGrpcService}>Click for grpc request</button>
</header>
</div>
);
}
}
export default Dashboard;
envoy.yaml:
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 0.0.0.0, port_value: 9901 }
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 8080 }
filter_chains:
- filters:
- name: envoy.http_connection_manager
config:
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: helloworld_service
max_grpc_timeout: 0s
cors:
allow_origin_string_match:
- prefix: "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: "1728000"
expose_headers: custom-header-1,grpc-status,grpc-message
http_filters:
- name: envoy.grpc_web
- name: envoy.cors
- name: envoy.router
clusters:
- name: helloworld_service
connect_timeout: 0.25s
type: logical_dns
http2_protocol_options: {}
lb_policy: round_robin
hosts: [{ socket_address: { address: localhost, port_value: 9090 }}]
The error in the console is:
2
And also:
Object { code: 2, message: "Http response at 400 or 500 level", metadata: undefined }```
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:7
Top Results From Across the Web
3 Ways to Fix the CORS Error — and How the Access-Control ...
If the frontend domain does not match the value, the browser raises the red flag and blocks the API request with the CORS...
Read more >Firefox 'Cross-Origin Request Blocked' despite headers [closed]
I came across this question having found requests in Firefox were being blocked with the message: Reason: CORS request did not succeed.
Read more >CORS errors and how to solve them - Topcoder
Why is CORS blocked? ... It is to prevent cross-site request forgery. Let's say you log in to facebook.com and your browser stores...
Read more >The Cross-Origin Request Blocked Error - Logi Analytics
Cross -origin resource sharing (CORS) is a new standard introduced in HTML 5 that allows web applications to use HTTP headers to specify...
Read more >Cross-Origin Request Blocked: The Same Origin Policy ...
If the CORS configuration isn't setup correctly, the browser console will present an error like "Cross-Origin Request Blocked: The Same Origin ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

No need to reopen this issue, but for anyone else out there who runs into the issue where the OPTIONS request succeeds but the following POST request fails:
I was running into this issue because the POST request was sending an
Authorizationheader and I forgot to add that tocors.allow_headersinenvoy.yaml.What do we do if no
OPTIONSrequest is being sent but we’re still running into this CORS issue?Here are the requests being sent by my browser, Brave:
Here’s the full error I’m seeing in my console:
Here’s my envoy config
Note that I changed the address of the cluster endpoint (the 2nd last line) to be
127.0.0.1for local testing. Also note that my GRPC server is running on port 6789 which has been reflected in the envoy config.EDIT: I just confirmed that envoy works with grpcurl. This leads me to believe that something funky is going on with my browser:
EDIT 2: Figured out what’s going on, I didn’t have a scheme used during client construction. Switching from
export default new WebClient("localhost:8080")toexport default new WebClient("http://localhost:8080")solved this.