웹 개발

HTTP에서 localhost 호출 시 CORS 에러 발생하는 이유

노루아부지 2022. 10. 29. 21:12

개요

ActiveX가 없어지면서 ActiveX 프로그램을 Local Server를 활용한 프로그램으로 변경해야 했습니다. 그런데 HTTPS 서버에서는 localhost를 호출 가능한데, HTTP에서는 CORS 에러가 발생했습니다. 예전에는 분명히 호출이 됐던것 같은데 왜 안되는 것일까요?

 

 

원인

에러가 나는 원인에 대한 해답은 크롬 공식 블로그에 있습니다.

 

 

Chrome is deprecating access to private network endpoints from non-secure websites as part of the Private Network Access specification. The aim is to protect users from cross-site request forgery (CSRF) attacks targeting routers and other devices on private networks. These attacks have affected hundreds of thousands of users, allowing attackers to redirect them to malicious servers.

Chrome will introduce the following changes:

  • Blocking requests to private networks from insecure public websites starting in Chrome 94.
  • Introducing a deprecation trial which will end in Chrome 109. It will allow developers to request a time extension for chosen origins, which will not be affected during the deprecation trial.
  • Introducing a Chrome policy which will allow managed Chrome deployments to bypass the deprecation permanently. Available in Chrome 92.

 

Chrome 94부터 안전하지 않은 공개 웹사이트에서 개인 네트워크에 대한 요청을 차단합니다. 여기서 말하는 "안전하지 않은 공개 웹사이트"는 HTTP를 의미합니다. 또한 공개 웹 사이트는 사설 인터넷이 아닌 공개 인터넷을 의미합니다.

따라서 localhost를 호출하기 위해서는 다음 두 가지중 한 가지이어야 합니다.

  1. 사설망의 HTTP 서버에서 호출
  2. 공개망의 HTTPS 서버에서 호출

 

 

사설망이란?

사설망(private network)은 우리가 흔이 사용하는 192.168로 시작하는 IP입니다. 하지만 많은 사람들이 192.168로 시작하는 IP만 사설망으로 알고 있습니다. 하지만 192.168로 시작하는 IP는 C 클래스 IP이고, A 클래스와 B 클래스도 사설 IP가 다음과 같이 존재합니다.

HTTP에서 localhost 호출 시 CORS 에러 발생하는 이유

 

 

다른 CORS 에러 발생 관련 글

html파일을 local에서 실행할 때 CORS 에러가 발생하는 이유

728x90
loading