Como ativar o cabeçalho HTTP seguro no Apache Tomcat 8?

A injeção de resposta HTTP com o cabeçalho seguro pode atenuar a maioria dos vulnerabilidades de segurança da web .

Se você estiver gerenciando um ambiente de produção ou um aplicativo relacionado a pagamentos, a equipe de teste de segurança/penetração solicitará que você implemente o cabeçalho HTTP necessário para cumprir o padrão de segurança PCI-DSS.

Ter cabeçalho seguro instrui o navegador a fazer ou não fazer certas coisas para prevenir certos ataques de segurança.

A maioria de vocês pode estar usando um servidor web como Apache, Nginx, IIS na frente do Tomcat, então você pode implemente os cabeçalhos diretamente no servidor web .

No entanto, se você não tiver nenhum servidor web na frente ou precisar implementar diretamente no Tomcat, boas notícias se você estiver usando o Tomcat 8.

O Tomcat 8 adicionou suporte para os seguintes cabeçalhos de resposta HTTP.

  • X-Frame-Options – para evitar ataques de clickjacking
  • X-XSS-Protection – para evitar ataques de script entre sites
  • X-Content-Type-Options – bloqueia a detecção de tipo de conteúdo
  • HSTS – adiciona segurança de transporte estrita

eu testei com Apache Tomcat 8.5.15 sobre oceano digital Servidor Linux (distribuição CentOS).

Observação: Se você está procurando proteção e segurança geral, pode consulte este guia .

Como prática recomendada, faça um backup do arquivo de configuração necessário antes de fazer alterações ou testar em um ambiente de não produção.

  • Entrar no servidor Tomcat
  • Vá para a pasta conf no caminho onde o Tomcat está instalado
  • Descomente o seguinte filtro (por padrão, é comentado)
    <filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>

Ao remover o comentário acima, você instrui o Tomcat a oferecer suporte ao filtro HTTP Header Security.

  • Adicione o seguinte logo após o filtro acima
<filter-mapping>
    <filter-name>httpHeaderSecurity</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Ao adicionar acima, você instrui o Tomcat a injetar o cabeçalho HTTP em todas as URLs do aplicativo.

  • Reinicie o Tomcat e acesse o aplicativo para verificar os cabeçalhos.

Você pode usar um ferramenta online para verificar o cabeçalho ou use F12 em um navegador para inspecionar.

Aqui está uma referência rápida de filtro tirada de um arquivo web.xml.

<!-- ================== Built In Filter Definitions ===================== -->
 <!-- A filter that sets various security related HTTP Response headers.   -->
  <!-- This filter supports the following initialization parameters         -->
  <!-- (default values are in square brackets):                             -->
  <!--                                                                      -->
  <!--   hstsEnabled         Should the HTTP Strict Transport Security      -->
  <!--                       (HSTS) header be added to the response? See    -->
  <!--                       RFC 6797 for more information on HSTS. (true)  -->
  <!--                                                                      -->
  <!--   hstsMaxAgeSeconds   The max age value that should be used in the   -->
  <!--                       HSTS header. Negative values will be treated   -->
  <!--                       as zero. (0)                                   -->
  <!--                                                                      -->
  <!--   hstsIncludeSubDomains                                              -->
  <!--                       Should the includeSubDomains parameter be      -->
  <!--                       included in the HSTS header.                   -->
  <!--                                                                      -->
  <!--   antiClickJackingEnabled                                            -->
  <!--                       Should the anti click-jacking header           -->
  <!--                       X-Frame-Options be added to every response?    -->
  <!--                       (true)                                         -->
  <!--                                                                      -->
  <!--   antiClickJackingOption                                             -->
  <!--                       What value should be used for the header. Must -->
  <!--                       be one of DENY, SAMEORIGIN, ALLOW-FROM         -->
  <!--                       (case-insensitive). (DENY)                     -->
  <!--                                                                      -->
  <!--   antiClickJackingUri IF ALLOW-FROM is used, what URI should be      -->
  <!--                       allowed? ()                                    -->
  <!--                                                                      -->
  <!--   blockContentTypeSniffingEnabled                                    -->
  <!--                       Should the header that blocks content type     -->
  <!--                       sniffing be added to every response? (true)    -->

A ativação do cabeçalho seguro no Tomcat 8 é simples e, como administrador, você deve planejar implementá-los para obter melhor segurança.

Se você é novo no Tomcat, pode estar interessado em fazer este Curso de administração Apache Tomcat .

Artigos relacionados