Tomcat 預設啟用 DefaultServlet 的 listings 參數為 true,當請求目錄且無歡迎檔案時,會列出該目錄所有檔案。這會洩露專案結構、原始碼、配置檔等敏感資訊,屬於**資訊洩露漏洞 (CWE-548)**,生產環境**必須關閉**。
$CATALINA_HOME/conf/web.xml(全域生效)或專案 WEB-INF/web.xml(僅該專案)。建議修改全域配置。
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value> <!-- 預設為 true -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
./shutdown.sh && ./startup.sh # Linux/Mac
shutdown.bat && startup.bat # Windows
https://localhost:8080/myapp/images/<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
有歡迎檔案時優先顯示,不觸發目錄列表。
<security-constraint>
<web-resource-collection>
<web-resource-name>Hidden Resources</web-resource-name>
<url-pattern>/WEB-INF/*</url-pattern>
<url-pattern>/META-INF/*</url-pattern>
</web-resource-collection>
<auth-constraint/> <!-- 空表示禁止所有存取 -->
</security-constraint>
<init-param>
<param-name>readonly</param-name>
<param-value>true</param-value> <!-- 禁止 PUT/DELETE 修改靜態檔 -->
</init-param>
<init-param>
<param-name>fileEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addContextCustomizers(context -> {
context.setDefaultServletReadOnly(true);
context.setListings(false); // 關鍵:關閉目錄瀏覽
});
return factory;
}
或在 application.yml:
server:
servlet:
context-parameters:
org.apache.catalina.servlets.DefaultServlet.listings: false
修改 conf/web.xml 將 listings 設為 false 是 Tomcat 部署上線的**標準動作**。配合歡迎頁面設定、靜態資源存取控制、唯讀模式,構建多層防護。Spring Boot 專案同理需在嵌入式容器中顯式關閉。