我有一个GraphQL后端和应用程序的React前端。CORS策略被设置为只允许前端https://my.server:443
访问后端https://my.server:4444
,并且工作正常。。。。大多数时候。
我的Express.js服务器启动如下:
server.start(
{
cors: {
credentials: true,
origin: [ "https://my.server" ]
},
},
(deets) => {
console.log(`Server is now running on port http://localhost:${deets.port}`);
}
);
当我使用包含URL的有效负载向API发出POST请求时,我会得到一个CORS错误。对话如下:
OPTIONS请求:
OPTIONS / HTTP/2
Host: my.server:4444
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Referer: https://my.server/qualifications
Origin: https://my.server
Connection: keep-alive
TE: Trailers
响应:
HTTP/2 204 No Content
date: Thu, 15 Oct 2020 12:16:25 GMT
x-powered-by: Express
access-control-allow-origin: https://my.server
vary: Origin, Access-Control-Request-Headers
access-control-allow-credentials: true
access-control-allow-methods: GET,HEAD,PUT,PATCH,POST,DELETE
access-control-allow-headers: content-type
X-Firefox-Spdy: h2
POST请求:
POST / HTTP/2
Host: my.server:4444
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
Content-Length: 616
Origin: https://my.server
Referer: https://my.server/qualifications
Connection: keep-alive
Cookie: token=eyJhbGc<removed-jwt>gmWVO_I68
TE: Trailers
有效载荷:
{
"operationName": "UPDATE_QUALIFICATION_MUTATION",
"variables": {
"id": "ckf54v5lm0dck07821octvj4b",
"name": "MBA",
"description": "Testing Links",
"link": "http://www.google.com" <---- URL in payload
},
"query": "mutation UPDATE_QUALIFICATION_MUTATION($id: ID!, $name: String, $link: String, $description: String, $company: CompanyUpdateOneInput) {n updateQualification(where: {id: $id}, data: {name: $name, link: $link, description: $description, company: $company}) {n idn namen descriptionn linkn company {n idn namen __typenamen }n __typenamen }n}n"
}
响应:
HTTP/2 403 Forbidden
server: awselb/2.0
date: Thu, 15 Oct 2020 12:16:25 GMT
content-type: text/html
content-length: 118
X-Firefox-Spdy: h2
好吧。。。这次让我们在没有URL的情况下重试。。。
让我困惑的是,当我做同样的事情时,除了一个包含URL的字段外,我得到的是:
OPTIONS请求:
OPTIONS / HTTP/2
Host: my.server:4444
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Referer: https://my.server/qualifications
Origin: https://my.server
Connection: keep-alive
TE: Trailers
响应:
HTTP/2 204 No Content
date: Thu, 15 Oct 2020 13:07:41 GMT
x-powered-by: Express
access-control-allow-origin: https://my.server
vary: Origin, Access-Control-Request-Headers
access-control-allow-credentials: true
access-control-allow-methods: GET,HEAD,PUT,PATCH,POST,DELETE
access-control-allow-headers: content-type
X-Firefox-Spdy: h2
POST请求:
POST / HTTP/2
Host: my.server:4444
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/json
Content-Length: 560
Origin: https://my.server
Referer: https://my.server/qualifications
Connection: keep-alive
Cookie: token=eyJhbG<removed-jwt>WVO_I68
TE: Trailers
有效载荷:
{
"operationName": "UPDATE_QUALIFICATION_MUTATION",
"variables": {
"id": "ckf54v5lm0dck07821octvj4b",
"name": "MBA",
"description": "Testing Links",
"link": "not-a-url" <---- THIS CHANGED
},
"query": "mutation UPDATE_QUALIFICATION_MUTATION($id: ID!, $name: String, $link: String, $description: String, $company: CompanyUpdateOneInput) {n updateQualification(where: {id: $id}, data: {name: $name, link: $link, description: $description, company: $company}) {n idn namen descriptionn linkn company {n idn namen __typenamen }n __typenamen }n}n"
}
响应:
HTTP/2 200 OK
date: Thu, 15 Oct 2020 13:07:41 GMT
content-type: application/json
content-length: 173
x-powered-by: Express
access-control-allow-origin: https://my.server
vary: Origin
access-control-allow-credentials: true
X-Firefox-Spdy: h2
现在它工作得很好??留给我的问题比其他任何事情都多:
- 请求的JSON有效负载如何影响CORS
- 我的Express服务器CORS配置中缺少什么吗
- 在有效负载中发送URL是否存在某种常见问题
- 如果是,是否有建议的解决方法?(我本来打算在前端对其进行base64编码/解码,但必须这样做似乎是"错误的"!(
任何指针都将不胜感激!
我们今天偶然发现了一个类似的问题。
事实证明,在我们的案例中,它是由WAF配置拒绝请求引起的。我们在分析WAF采样和负载均衡器日志后发现了这一点。
祝你好运!