|
| 1 | +/* |
| 2 | + * Wave, containers provisioning service |
| 3 | + * Copyright (c) 2023-2024, Seqera Labs |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.seqera.wave.configuration |
| 20 | + |
| 21 | +import javax.annotation.Nullable |
| 22 | +import javax.annotation.PostConstruct |
| 23 | + |
| 24 | +import groovy.transform.CompileStatic |
| 25 | +import groovy.transform.ToString |
| 26 | +import groovy.util.logging.Slf4j |
| 27 | +import io.micronaut.context.annotation.Requires |
| 28 | +import io.micronaut.context.annotation.Value |
| 29 | +import jakarta.inject.Singleton |
| 30 | + |
| 31 | +/** |
| 32 | + * Configuration for HTTP security headers |
| 33 | + * |
| 34 | + * @author Munish Chouhan <munish.chouhan@seqera.io> |
| 35 | + */ |
| 36 | +@ToString(includeNames = true, includePackage = false) |
| 37 | +@CompileStatic |
| 38 | +@Slf4j |
| 39 | +@Singleton |
| 40 | +@Requires(property = 'wave.security.http-headers.enabled', value = 'true', defaultValue = 'true') |
| 41 | +class SecurityHeadersConfig { |
| 42 | + |
| 43 | + /** |
| 44 | + * Enable or disable security headers globally |
| 45 | + */ |
| 46 | + @Value('${wave.security.http-headers.enabled:true}') |
| 47 | + Boolean enabled |
| 48 | + |
| 49 | + /** |
| 50 | + * HSTS max-age in seconds |
| 51 | + * default to 1 year (31536000 seconds) |
| 52 | + * This tell browsers to only use HTTPS for one year (31,536,000 seconds) or custom value set by user, |
| 53 | + * preventing downgrade attacks and improving security by ensuring encrypted connections by default, |
| 54 | + * for more information check https://hstspreload.org/#submission-requirements |
| 55 | + */ |
| 56 | + @Value('${wave.security.http-headers.hsts.max-age:31536000}') |
| 57 | + Long hstsMaxAge |
| 58 | + |
| 59 | + /** |
| 60 | + * Include subdomains in HSTS |
| 61 | + */ |
| 62 | + @Value('${wave.security.http-headers.hsts.include-sub-domains:true}') |
| 63 | + Boolean hstsIncludeSubDomains |
| 64 | + |
| 65 | + /** |
| 66 | + * X-Frame-Options header value (default: `DENY`) |
| 67 | + */ |
| 68 | + @Nullable |
| 69 | + @Value('${wave.security.http-headers.frame-options}') |
| 70 | + String frameOptions |
| 71 | + |
| 72 | + /** |
| 73 | + * X-Content-Type-Options header value |
| 74 | + */ |
| 75 | + @Nullable |
| 76 | + @Value('${wave.security.http-headers.content-type-options}') |
| 77 | + String contentTypeOptions |
| 78 | + |
| 79 | + /** |
| 80 | + * Referrer-Policy header value |
| 81 | + */ |
| 82 | + @Nullable |
| 83 | + @Value('${wave.security.http-headers.referrer-policy}') |
| 84 | + String referrerPolicy |
| 85 | + |
| 86 | + /** |
| 87 | + * Permissions-Policy header value |
| 88 | + */ |
| 89 | + @Nullable |
| 90 | + @Value('${wave.security.http-headers.permissions-policy}') |
| 91 | + String permissionsPolicy |
| 92 | + |
| 93 | + /** |
| 94 | + * Content-Security-Policy header value |
| 95 | + */ |
| 96 | + @Nullable |
| 97 | + @Value('${wave.security.http-headers.content-security-policy}') |
| 98 | + String contentSecurityPolicy |
| 99 | + |
| 100 | + @PostConstruct |
| 101 | + private void init() { |
| 102 | + log.info("Security headers config: enabled=${enabled}; hsts-max-age=${hstsMaxAge}; hsts-include-sub-domains=${hstsIncludeSubDomains}; " + |
| 103 | + "frame-options=${frameOptions}; content-type-options=${contentTypeOptions}; referrer-policy=${referrerPolicy}; " + |
| 104 | + "permissions-policy=${permissionsPolicy}; csp=${contentSecurityPolicy}") |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Build the HSTS header value |
| 109 | + */ |
| 110 | + String getHstsValue() { |
| 111 | + if (hstsMaxAge == null) { |
| 112 | + return null |
| 113 | + } |
| 114 | + final result = new StringBuilder("max-age=${hstsMaxAge}") |
| 115 | + if (hstsIncludeSubDomains) { |
| 116 | + result.append("; includeSubDomains") |
| 117 | + } |
| 118 | + return result.toString() |
| 119 | + } |
| 120 | +} |
0 commit comments