-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/hardening #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Feat/hardening #129
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9f807f6
feat(security): enable spring web security with autoconfiguration
JoergSiebahn 60bd672
feat(security): verify that HTTP method TRACE is disabled
JoergSiebahn 02b146d
test: expect no session after successful authentication
JoergSiebahn 0f48044
feat(security): value the forwarded headers
JoergSiebahn 7a52e7a
fix(auth): integrate management port authorization in SdaAccessDecisi…
JoergSiebahn 34e48c9
feat(security): enable configurable CORS restrictions
JoergSiebahn d4cdcdc
feat(security): set secure response headers and ensure custom error r…
JoergSiebahn 59cef61
docs(security): describe security hardening implementation
JoergSiebahn d36a574
feat(security): include security in the SDA Platform annotation
JoergSiebahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Security Hardening | ||
|
||
sda-spring-boot-commons changes some default configuration for security reasons. | ||
This document provides a brief overview about the addressed risks. | ||
|
||
## Risk: Accessing critical resources from untrusted environments | ||
|
||
To avoid exposing internal resources, Spring Boot Actuator is configured to listen on a separate | ||
port. | ||
Health, metrics and other sensitive information can't be exposed to the internet by accident, e.g. | ||
by missing to exclude the actuator path. | ||
|
||
Custom critical resources can be exposed at the management port by implementing | ||
`org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint` or | ||
`org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint`. | ||
Note that there is an [open discussion](https://github.com/spring-projects/spring-boot/issues/31768) | ||
about these annotations. | ||
As long as they are not deprecated, it is suggested to use them because the use is most similar to | ||
controllers used in regular REST APIs. | ||
|
||
## Risk: Root start | ||
|
||
If the service is started with extended privileges as the root user, an attacker can more easily | ||
attack the operating system after taking over from the container. | ||
|
||
The default configuration is capable to run as no root, listening to ports 8080 and 8081. | ||
Deployment checks must ensure, that the container is not configured with a root user. | ||
|
||
## Risk: Exploitation of HTTP methods | ||
|
||
The HTTP method `TRACE` is disabled by default to mitigate [Cross Site Tracing](https://owasp.org/www-community/attacks/Cross_Site_Tracing). | ||
|
||
## Risk: Loss of source IP address | ||
|
||
We expect, the services built with sda-spring-boot-commons are deployed behind a proxy, e.g. an | ||
Ingress in Kubernetes. | ||
|
||
This library is configured by default to consider `X-Forwarded-*` headers to identify the original | ||
caller. | ||
|
||
## Risk: Detection of confidential components | ||
|
||
Knowing the components used in a software makes it easier to look for and exploit specific CVEs. | ||
|
||
Custom error handlers and other configurations are used to avoid identifiable default output from | ||
the framework and its components. | ||
|
||
## Risk: Lack of visibility | ||
|
||
If there is no visibility, there is no response to an abusive action and attackers can explore risks | ||
undisturbed. | ||
|
||
Logs are written to standard out by default to comply with Kubernetes environments. | ||
Prometheus metrics are exposed as expected by SDA environments. | ||
|
||
## Risk: Buffer Overflow | ||
|
||
The size of request and response headers is limited to 8KiB. | ||
|
||
## Header | ||
|
||
By configuring the default headers, the following risks are addressed: | ||
|
||
- Cross-Site Scripting | ||
- Content interpretation by the browser | ||
- Content loading in Flash and PDFs | ||
- Clickjacking | ||
- Sharing visited URLs with third parties | ||
- Abuse from Cross-Origin Resource Sharing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
.../main/java/org/sdase/commons/spring/boot/web/auth/SdaMonitoringSecurityConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...java/org/sdase/commons/spring/boot/web/auth/management/ManagementAccessDecisionVoter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2022- SDA SE Open Industry Solutions (https://www.sda.se) | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
package org.sdase.commons.spring.boot.web.auth.management; | ||
|
||
import java.util.Collection; | ||
import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort; | ||
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; | ||
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.security.access.AccessDecisionVoter; | ||
import org.springframework.security.access.ConfigAttribute; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.FilterInvocation; | ||
import org.springframework.stereotype.Component; | ||
|
||
public interface ManagementAccessDecisionVoter extends AccessDecisionVoter<FilterInvocation> { | ||
|
||
@Override | ||
default boolean supports(ConfigAttribute attribute) { | ||
return true; | ||
} | ||
|
||
@Override | ||
default boolean supports(Class<?> clazz) { | ||
return FilterInvocation.class.isAssignableFrom(clazz); | ||
} | ||
|
||
@Component | ||
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT) | ||
class DifferentPortManagementAccessDecisionVoter implements ManagementAccessDecisionVoter { | ||
|
||
/** | ||
* The management port discovered in {@link | ||
* #onApplicationEvent(ServletWebServerInitializedEvent)}. Initially a value that can't be an | ||
* existing port to avoid granting access by accident to the application API. | ||
*/ | ||
private int managementPort = -1; | ||
|
||
@EventListener | ||
public void onApplicationEvent(ServletWebServerInitializedEvent event) { | ||
if ("management".equals(event.getApplicationContext().getServerNamespace())) { | ||
this.managementPort = event.getWebServer().getPort(); | ||
} | ||
} | ||
|
||
@Override | ||
public int vote( | ||
Authentication authentication, | ||
FilterInvocation filterInvocation, | ||
Collection<ConfigAttribute> attributes) { | ||
int requestLocalPort = filterInvocation.getRequest().getLocalPort(); | ||
return requestLocalPort == this.managementPort ? ACCESS_GRANTED : ACCESS_ABSTAIN; | ||
} | ||
} | ||
|
||
@Component | ||
@ConditionalOnManagementPort(ManagementPortType.SAME) | ||
class IgnoreSamePortManagementAccessDecisionVoter implements ManagementAccessDecisionVoter { | ||
@Override | ||
public int vote( | ||
Authentication authentication, | ||
FilterInvocation filterInvocation, | ||
Collection<ConfigAttribute> attributes) { | ||
return ACCESS_ABSTAIN; | ||
} | ||
} | ||
|
||
@Component | ||
@ConditionalOnManagementPort(ManagementPortType.DISABLED) | ||
class DisabledManagementAccessDecisionVoter implements ManagementAccessDecisionVoter { | ||
@Override | ||
public int vote( | ||
Authentication authentication, | ||
FilterInvocation filterInvocation, | ||
Collection<ConfigAttribute> attributes) { | ||
return ACCESS_ABSTAIN; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.