Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Content Security Policy and REST API call - how does it work?

I am finding it difficult to understand an error thrown by my app when trying to invoke a REST API.

My app, a pure HTML, JavaScript based using jQuery, is running in Jetty server. The server has implemented Content-Security-Policy:

Custom HTTP headers to be added to our responses: Content-Security-Policy: 
default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src 'self'; 
img-src 'self' data:; style-src 'self' 'unsafe-inline'; base-uri 'self'; 
frame-ancestors 'self'; font-src 'self';frame-src 'self'|X-Frame-Options: 
SAMEORIGIN|X-Content-Type-Options: nosniff|X-XSS-Protection: 1; mode=block|
Referrer-Policy: strict-origin|Feature-Policy: 'none'|Strict-Transport-Security: 
max-age=63072000; includeSubDomains; preload

I understand this part. Anyone trying to access my app has to adhere to these restrictions.

However, the JavaScript code in my app tries to call a Spring Boot REST API running on same Linux VM (http://localhost:8080/... or http://server-host-name:8080/...). When doing so, I get Content Security Policy violated when accessing http://localhost:8080...: content-src 'self'.This confuses me. Trying to access my app in Jetty without adhering to the security policy should raise error. But why would it throw an error when app in Jetty it is calling a Spring Boot API which doesn't have any such restriction?

Do I need to relax the Content Security Policy restriction on Jetty server? If so, why?

Summary:
REST API: Spring Boot HTTP API running on same Linux VM
Jetty Server: CSP restriction as mentioned above
JavaScript app in Jetty: Calling Spring Boot API

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Briefly: you have to add http://localhost:8080 to the default-src directive.

Tl; DR;: the 'self' token is insidious, because you intuitively endow him with powers that he fails to fulfill.

Browsers substisute the 'self' token with the "tuple origin" (scheme + host_name + port_number) of the page URL from the browser's address bar, and then they add some CSP-specific magic:

  • allow ws: + host_name + ws_standard_port
  • allow upgrade ws: to wss: and http: to https: in CSP3-browsers

So, what do we have:

  1. Your app uses fetch to access Spring Boot REST API which should be covered by missing connect-src directive, therefore browser uses default-src as fallback.
  2. Browser transform default-src 'self' to the default-src http://your_domain.com:80 or default-src https://your_domain.com:443 (depending on how you load the page).

As you can see both of these do not allow http://localhost:8080. Although actually localhost is an alias of the your_domain.com, but CSP does not know this, and just blocking because of mismatch host_name + port_number.

But if you will load app with the http://localhost:8080/your_app_path url, fetch to REST API will be allowed because covered by 'self'

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda