fix(http): enable restricted headers for DNS rebinding protection and improve host header handling (#3022)

This commit is contained in:
Balázs Szücs
2026-02-26 08:14:01 -07:00
committed by GitHub
parent e53c545dcb
commit 073bc1045e
3 changed files with 8 additions and 6 deletions
@@ -14,6 +14,10 @@ import org.booklore.config.BookmarkProperties;
@SpringBootApplication @SpringBootApplication
public class BookloreApplication { public class BookloreApplication {
static {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(BookloreApplication.class, args); SpringApplication.run(BookloreApplication.class, args);
} }
@@ -262,8 +262,11 @@ public class SecurityConfig {
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
super.prepareConnection(connection, httpMethod); super.prepareConnection(connection, httpMethod);
connection.setInstanceFollowRedirects(false); connection.setInstanceFollowRedirects(false);
String targetHost = FileService.getTargetHost();
if (targetHost != null) {
connection.setRequestProperty("Host", targetHost);
}
if (connection instanceof HttpsURLConnection httpsConnection) { if (connection instanceof HttpsURLConnection httpsConnection) {
String targetHost = FileService.getTargetHost();
if (targetHost != null) { if (targetHost != null) {
// Set original host for SNI (even if connecting to IP) // Set original host for SNI (even if connecting to IP)
SSLSocketFactory defaultFactory = httpsConnection.getSSLSocketFactory(); SSLSocketFactory defaultFactory = httpsConnection.getSSLSocketFactory();
@@ -57,11 +57,6 @@ public class FileService {
TARGET_HOST_THREAD_LOCAL.remove(); TARGET_HOST_THREAD_LOCAL.remove();
} }
static {
// Enable restricted headers to allow 'Host' header override for DNS rebinding protection
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
private final AppProperties appProperties; private final AppProperties appProperties;
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
private final AppSettingService appSettingService; private final AppSettingService appSettingService;