headers.adoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. [[webflux-headers]]
  2. = Security HTTP Response Headers
  3. xref:features/exploits/headers.adoc#headers[Security HTTP Response Headers] can be used to increase the security of web applications.
  4. This section is dedicated to WebFlux based support for Security HTTP Response Headers.
  5. [[webflux-headers-default]]
  6. == Default Security Headers
  7. Spring Security provides a xref:features/exploits/headers.adoc#headers-default[default set of Security HTTP Response Headers] to provide secure defaults.
  8. While each of these headers are considered best practice, it should be noted that not all clients utilize the headers, so additional testing is encouraged.
  9. You can customize specific headers.
  10. For example, assume that you want the defaults except you wish to specify `SAMEORIGIN` for xref:servlet/exploits/headers.adoc#servlet-headers-frame-options[X-Frame-Options].
  11. You can easily do this with the following Configuration:
  12. .Customize Default Security Headers
  13. ====
  14. .Java
  15. [source,java,role="primary"]
  16. ----
  17. @Bean
  18. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  19. http
  20. // ...
  21. .headers(headers -> headers
  22. .frameOptions(frameOptions -> frameOptions
  23. .mode(Mode.SAMEORIGIN)
  24. )
  25. );
  26. return http.build();
  27. }
  28. ----
  29. .Kotlin
  30. [source,kotlin,role="secondary"]
  31. ----
  32. @Bean
  33. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  34. return http {
  35. // ...
  36. headers {
  37. frameOptions {
  38. mode = Mode.SAMEORIGIN
  39. }
  40. }
  41. }
  42. }
  43. ----
  44. ====
  45. If you do not want the defaults to be added and want explicit control over what should be used, you can disable the defaults.
  46. An example is provided below:
  47. .Disable HTTP Security Response Headers
  48. ====
  49. .Java
  50. [source,java,role="primary"]
  51. ----
  52. @Bean
  53. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  54. http
  55. // ...
  56. .headers(headers -> headers.disable());
  57. return http.build();
  58. }
  59. ----
  60. .Kotlin
  61. [source,kotlin,role="secondary"]
  62. ----
  63. @Bean
  64. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  65. return http {
  66. // ...
  67. headers {
  68. disable()
  69. }
  70. }
  71. }
  72. ----
  73. ====
  74. [[webflux-headers-cache-control]]
  75. == Cache Control
  76. Spring Security includes xref:features/exploits/headers.adoc#headers-cache-control[Cache Control] headers by default.
  77. However, if you actually want to cache specific responses, your application can selectively add them to the https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/server/reactive/ServerHttpResponse.html[ServerHttpResponse] to override the header set by Spring Security.
  78. This is useful to ensure things like CSS, JavaScript, and images are properly cached.
  79. When using Spring WebFlux, this is typically done within your configuration.
  80. Details on how to do this can be found in the https://docs.spring.io/spring/docs/5.0.0.RELEASE/spring-framework-reference/web-reactive.html#webflux-config-static-resources[Static Resources] portion of the Spring Reference documentation
  81. If necessary, you can also disable Spring Security's cache control HTTP response headers.
  82. .Cache Control Disabled
  83. ====
  84. .Java
  85. [source,java,role="primary"]
  86. ----
  87. @Bean
  88. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  89. http
  90. // ...
  91. .headers(headers -> headers
  92. .cache(cache -> cache.disable())
  93. );
  94. return http.build();
  95. }
  96. ----
  97. .Kotlin
  98. [source,kotlin,role="secondary"]
  99. ----
  100. @Bean
  101. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  102. return http {
  103. // ...
  104. headers {
  105. cache {
  106. disable()
  107. }
  108. }
  109. }
  110. }
  111. ----
  112. ====
  113. [[webflux-headers-content-type-options]]
  114. == Content Type Options
  115. Spring Security includes xref:features/exploits/headers.adoc#headers-content-type-options[Content-Type] headers by default.
  116. However, you can disable it with:
  117. .Content Type Options Disabled
  118. ====
  119. .Java
  120. [source,java,role="primary"]
  121. ----
  122. @Bean
  123. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  124. http
  125. // ...
  126. .headers(headers -> headers
  127. .contentTypeOptions(contentTypeOptions -> contentTypeOptions.disable())
  128. );
  129. return http.build();
  130. }
  131. ----
  132. .Kotlin
  133. [source,kotlin,role="secondary"]
  134. ----
  135. @Bean
  136. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  137. return http {
  138. // ...
  139. headers {
  140. contentTypeOptions {
  141. disable()
  142. }
  143. }
  144. }
  145. }
  146. ----
  147. ====
  148. [[webflux-headers-hsts]]
  149. == HTTP Strict Transport Security (HSTS)
  150. Spring Security provides the xref:features/exploits/headers.adoc#headers-hsts[Strict Transport Security] header by default.
  151. However, you can customize the results explicitly.
  152. For example, the following is an example of explicitly providing HSTS:
  153. .Strict Transport Security
  154. ====
  155. .Java
  156. [source,java,role="primary"]
  157. ----
  158. @Bean
  159. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  160. http
  161. // ...
  162. .headers(headers -> headers
  163. .hsts(hsts -> hsts
  164. .includeSubdomains(true)
  165. .preload(true)
  166. .maxAge(Duration.ofDays(365))
  167. )
  168. );
  169. return http.build();
  170. }
  171. ----
  172. .Kotlin
  173. [source,kotlin,role="secondary"]
  174. ----
  175. @Bean
  176. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  177. return http {
  178. // ...
  179. headers {
  180. hsts {
  181. includeSubdomains = true
  182. preload = true
  183. maxAge = Duration.ofDays(365)
  184. }
  185. }
  186. }
  187. }
  188. ----
  189. ====
  190. [[webflux-headers-frame-options]]
  191. == X-Frame-Options
  192. By default, Spring Security disables rendering within an iframe using xref:features/exploits/headers.adoc#headers-frame-options[X-Frame-Options].
  193. You can customize frame options to use the same origin using the following:
  194. .X-Frame-Options: SAMEORIGIN
  195. ====
  196. .Java
  197. [source,java,role="primary"]
  198. ----
  199. @Bean
  200. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  201. http
  202. // ...
  203. .headers(headers -> headers
  204. .frameOptions(frameOptions -> frameOptions
  205. .mode(SAMEORIGIN)
  206. )
  207. );
  208. return http.build();
  209. }
  210. ----
  211. .Kotlin
  212. [source,kotlin,role="secondary"]
  213. ----
  214. @Bean
  215. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  216. return http {
  217. // ...
  218. headers {
  219. frameOptions {
  220. mode = SAMEORIGIN
  221. }
  222. }
  223. }
  224. }
  225. ----
  226. ====
  227. [[webflux-headers-xss-protection]]
  228. == X-XSS-Protection
  229. By default, Spring Security instructs browsers to block reflected XSS attacks using the <<headers-xss-protection,X-XSS-Protection header>.
  230. You can disable `X-XSS-Protection` with the following Configuration:
  231. .X-XSS-Protection Customization
  232. ====
  233. .Java
  234. [source,java,role="primary"]
  235. ----
  236. @Bean
  237. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  238. http
  239. // ...
  240. .headers(headers -> headers
  241. .xssProtection(xssProtection -> xssProtection.disable())
  242. );
  243. return http.build();
  244. }
  245. ----
  246. .Kotlin
  247. [source,kotlin,role="secondary"]
  248. ----
  249. @Bean
  250. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  251. return http {
  252. // ...
  253. headers {
  254. xssProtection {
  255. disable()
  256. }
  257. }
  258. }
  259. }
  260. ----
  261. ====
  262. [[webflux-headers-csp]]
  263. == Content Security Policy (CSP)
  264. Spring Security does not add xref:features/exploits/headers.adoc#headers-csp[Content Security Policy] by default, because a reasonable default is impossible to know without context of the application.
  265. The web application author must declare the security policy(s) to enforce and/or monitor for the protected resources.
  266. For example, given the following security policy:
  267. .Content Security Policy Example
  268. ====
  269. [source,http]
  270. ----
  271. Content-Security-Policy: script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/
  272. ----
  273. ====
  274. You can enable the CSP header as shown below:
  275. .Content Security Policy
  276. ====
  277. .Java
  278. [source,java,role="primary"]
  279. ----
  280. @Bean
  281. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  282. http
  283. // ...
  284. .headers(headers -> headers
  285. .contentSecurityPolicy(policy -> policy
  286. .policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/")
  287. )
  288. );
  289. return http.build();
  290. }
  291. ----
  292. .Kotlin
  293. [source,kotlin,role="secondary"]
  294. ----
  295. @Bean
  296. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  297. return http {
  298. // ...
  299. headers {
  300. contentSecurityPolicy {
  301. policyDirectives = "script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/"
  302. }
  303. }
  304. }
  305. }
  306. ----
  307. ====
  308. To enable the CSP `report-only` header, provide the following configuration:
  309. .Content Security Policy Report Only
  310. ====
  311. .Java
  312. [source,java,role="primary"]
  313. ----
  314. @Bean
  315. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  316. http
  317. // ...
  318. .headers(headers -> headers
  319. .contentSecurityPolicy(policy -> policy
  320. .policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/")
  321. .reportOnly()
  322. )
  323. );
  324. return http.build();
  325. }
  326. ----
  327. .Kotlin
  328. [source,kotlin,role="secondary"]
  329. ----
  330. @Bean
  331. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  332. return http {
  333. // ...
  334. headers {
  335. contentSecurityPolicy {
  336. policyDirectives = "script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/"
  337. reportOnly = true
  338. }
  339. }
  340. }
  341. }
  342. ----
  343. ====
  344. [[webflux-headers-referrer]]
  345. == Referrer Policy
  346. Spring Security does not add xref:features/exploits/headers.adoc#headers-referrer[Referrer Policy] headers by default.
  347. You can enable the Referrer Policy header using configuration as shown below:
  348. .Referrer Policy Configuration
  349. ====
  350. .Java
  351. [source,java,role="primary"]
  352. ----
  353. @Bean
  354. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  355. http
  356. // ...
  357. .headers(headers -> headers
  358. .referrerPolicy(referrer -> referrer
  359. .policy(ReferrerPolicy.SAME_ORIGIN)
  360. )
  361. );
  362. return http.build();
  363. }
  364. ----
  365. .Kotlin
  366. [source,kotlin,role="secondary"]
  367. ----
  368. @Bean
  369. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  370. return http {
  371. // ...
  372. headers {
  373. referrerPolicy {
  374. policy = ReferrerPolicy.SAME_ORIGIN
  375. }
  376. }
  377. }
  378. }
  379. ----
  380. ====
  381. [[webflux-headers-feature]]
  382. == Feature Policy
  383. Spring Security does not add xref:features/exploits/headers.adoc#headers-feature[Feature Policy] headers by default.
  384. The following `Feature-Policy` header:
  385. .Feature-Policy Example
  386. ====
  387. [source]
  388. ----
  389. Feature-Policy: geolocation 'self'
  390. ----
  391. ====
  392. You can enable the Feature Policy header as shown below:
  393. .Feature-Policy Configuration
  394. ====
  395. .Java
  396. [source,java,role="primary"]
  397. ----
  398. @Bean
  399. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  400. http
  401. // ...
  402. .headers(headers -> headers
  403. .featurePolicy("geolocation 'self'")
  404. );
  405. return http.build();
  406. }
  407. ----
  408. .Kotlin
  409. [source,kotlin,role="secondary"]
  410. ----
  411. @Bean
  412. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  413. return http {
  414. // ...
  415. headers {
  416. featurePolicy("geolocation 'self'")
  417. }
  418. }
  419. }
  420. ----
  421. ====
  422. [[webflux-headers-permissions]]
  423. == Permissions Policy
  424. Spring Security does not add xref:features/exploits/headers.adoc#headers-permissions[Permissions Policy] headers by default.
  425. The following `Permissions-Policy` header:
  426. .Permissions-Policy Example
  427. ====
  428. [source]
  429. ----
  430. Permissions-Policy: geolocation=(self)
  431. ----
  432. ====
  433. You can enable the Permissions Policy header as shown below:
  434. .Permissions-Policy Configuration
  435. ====
  436. .Java
  437. [source,java,role="primary"]
  438. ----
  439. @Bean
  440. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  441. http
  442. // ...
  443. .headers(headers -> headers
  444. .permissionsPolicy(permissions -> permissions
  445. .policy("geolocation=(self)")
  446. )
  447. );
  448. return http.build();
  449. }
  450. ----
  451. .Kotlin
  452. [source,kotlin,role="secondary"]
  453. ----
  454. @Bean
  455. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  456. return http {
  457. // ...
  458. headers {
  459. permissionsPolicy {
  460. policy = "geolocation=(self)"
  461. }
  462. }
  463. }
  464. }
  465. ----
  466. ====
  467. [[webflux-headers-clear-site-data]]
  468. == Clear Site Data
  469. Spring Security does not add xref:features/exploits/headers.adoc#headers-clear-site-data[Clear-Site-Data] headers by default.
  470. The following Clear-Site-Data header:
  471. .Clear-Site-Data Example
  472. ====
  473. ----
  474. Clear-Site-Data: "cache", "cookies"
  475. ----
  476. ====
  477. can be sent on log out with the following configuration:
  478. .Clear-Site-Data Configuration
  479. ====
  480. .Java
  481. [source,java,role="primary"]
  482. ----
  483. @Bean
  484. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  485. ServerLogoutHandler securityContext = new SecurityContextServerLogoutHandler();
  486. ClearSiteDataServerHttpHeadersWriter writer = new ClearSiteDataServerHttpHeadersWriter(CACHE, COOKIES);
  487. ServerLogoutHandler clearSiteData = new HeaderWriterServerLogoutHandler(writer);
  488. DelegatingServerLogoutHandler logoutHandler = new DelegatingServerLogoutHandler(securityContext, clearSiteData);
  489. http
  490. // ...
  491. .logout()
  492. .logoutHandler(logoutHandler);
  493. return http.build();
  494. }
  495. ----
  496. .Kotlin
  497. [source,kotlin,role="secondary"]
  498. ----
  499. @Bean
  500. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  501. val securityContext: ServerLogoutHandler = SecurityContextServerLogoutHandler()
  502. val writer = ClearSiteDataServerHttpHeadersWriter(CACHE, COOKIES)
  503. val clearSiteData: ServerLogoutHandler = HeaderWriterServerLogoutHandler(writer)
  504. val customLogoutHandler = DelegatingServerLogoutHandler(securityContext, clearSiteData)
  505. return http {
  506. // ...
  507. logout {
  508. logoutHandler = customLogoutHandler
  509. }
  510. }
  511. }
  512. ----
  513. ====