CORS

首先,看标准

好文章:

服务端如何处理不匹配的 Origin

  • https://github.com/koajs/cors/blob/0f3f948b9b8f00163a47de1b82e413bfef9b4a96/index.js#L89
  • https://github.com/rs/cors/blob/0c96d3850073b93c94338ed8ade7de3c8fafb1cf/cors.go#L316

看了这两个库的实现,都是只设置了 Access-Control-Allow-Origin 以及其他响应头,而且会继续执行业务逻辑。

但是当客户端请求的 Origin 和服务端 allow-origin 不匹配时,有必要继续执行到业务逻辑吗? 不仅是 GET 请求,POST 请求也可能是简单请求,不会对服务端造成影响吗?

根据 W3C 标准里的介绍

This extension enables server-side applications to enforce limitations (e.g. returning nothing) on the cross-origin requests that they are willing to service.

应该是由服务端开发者自己决定如何处理的。