generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING: don’t expose Exception::getMessage as detail (#156)
Unless the exception is of type ThrowableProblem or UnsatisfiedRouteException This aims to prevent accidental information leakage. Fixes: #144
- Loading branch information
Showing
10 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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
49 changes: 49 additions & 0 deletions
49
problem-json/src/test/groovy/io/micronaut/problem/DataLeakageOverrideSpec.groovy
This file contains 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,49 @@ | ||
package io.micronaut.problem | ||
|
||
import io.micronaut.context.annotation.Replaces | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.annotation.NonNull | ||
import io.micronaut.core.type.Argument | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import io.micronaut.http.server.exceptions.response.ErrorContext | ||
import io.micronaut.problem.conf.ProblemConfiguration | ||
import io.micronaut.web.router.exceptions.UnsatisfiedRouteException | ||
import jakarta.inject.Singleton | ||
import spock.lang.Issue | ||
|
||
@Issue("https://github.com/micronaut-projects/micronaut-problem-json/issues/144") | ||
class DataLeakageOverrideSpec extends EmbeddedServerSpecification { | ||
|
||
@Override | ||
String getSpecName() { | ||
'DataLeakageOverrideSpec' | ||
} | ||
|
||
void "You can replace and override ProblemErrorResponseProcessorReplacement::shouldIncludeErrorMessageAsDetailForDefaultProblem"() { | ||
when: | ||
Argument<?> okArg = Argument.of(String) | ||
Argument<?> errorArg = Argument.of(Map) | ||
client.exchange(HttpRequest.GET('/foo'), okArg, errorArg) | ||
|
||
then: | ||
HttpClientResponseException thrown = thrown() | ||
Optional<Map> bodyOptional = thrown.response.getBody(errorArg) | ||
bodyOptional.isPresent() | ||
bodyOptional.get().keySet().size() == 3 | ||
bodyOptional.get()['status'] == 500 | ||
bodyOptional.get()['type'] == "about:blank" | ||
bodyOptional.get()['detail'] == "Internal Server Error: foo data" | ||
} | ||
|
||
@Requires(property = "spec.name", value = "DataLeakageOverrideSpec") | ||
@Controller('/foo') | ||
static class FooController { | ||
@Get | ||
void doSomething() { | ||
throw new FooException("foo data"); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
problem-json/src/test/groovy/io/micronaut/problem/DataLeakageSpec.groovy
This file contains 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,48 @@ | ||
package io.micronaut.problem | ||
|
||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.core.type.Argument | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import spock.lang.Issue | ||
import spock.lang.Narrative | ||
|
||
@Issue("https://github.com/micronaut-projects/micronaut-problem-json/issues/144") | ||
@Narrative("""\ | ||
Exceptions that do not extend ThrowableProblem should not include Exception::getMessage in its details part as it | ||
may carry sensitive information and cause information disclosure. | ||
""") | ||
class DataLeakageSpec extends EmbeddedServerSpecification { | ||
|
||
@Override | ||
String getSpecName() { | ||
'DataLeakageSpec' | ||
} | ||
|
||
void "No unintended data leakage in instances of Problem"() { | ||
when: | ||
Argument<?> okArg = Argument.of(String) | ||
Argument<?> errorArg = Argument.of(Map) | ||
client.exchange(HttpRequest.GET('/foo'), okArg, errorArg) | ||
|
||
then: | ||
HttpClientResponseException thrown = thrown() | ||
Optional<Map> bodyOptional = thrown.response.getBody(errorArg) | ||
bodyOptional.isPresent() | ||
bodyOptional.get().keySet().size() == 2 | ||
bodyOptional.get()['status'] == 500 | ||
bodyOptional.get()['type'] == "about:blank" | ||
} | ||
|
||
@Requires(property = "spec.name", value = "DataLeakageSpec") | ||
@Controller('/foo') | ||
static class FooController { | ||
|
||
@Get | ||
void doSomething() { | ||
throw new Exception("foo data"); | ||
} | ||
} | ||
} |
This file contains 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
7 changes: 7 additions & 0 deletions
7
problem-json/src/test/java/io/micronaut/problem/FooException.java
This file contains 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,7 @@ | ||
package io.micronaut.problem; | ||
|
||
public class FooException extends RuntimeException { | ||
FooException(String message) { | ||
super(message); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...lem-json/src/test/java/io/micronaut/problem/ProblemErrorResponseProcessorReplacement.java
This file contains 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,28 @@ | ||
package io.micronaut.problem; | ||
|
||
import io.micronaut.context.annotation.Replaces; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.http.server.exceptions.response.ErrorContext; | ||
import io.micronaut.problem.conf.ProblemConfiguration; | ||
import io.micronaut.web.router.exceptions.UnsatisfiedRouteException; | ||
import jakarta.inject.Singleton; | ||
|
||
@Requires(property = "spec.name", value = "DataLeakageOverrideSpec") | ||
//tag::clazz[] | ||
@Replaces(ProblemErrorResponseProcessor.class) | ||
@Singleton | ||
public class ProblemErrorResponseProcessorReplacement | ||
extends ProblemErrorResponseProcessor { | ||
ProblemErrorResponseProcessorReplacement(ProblemConfiguration config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
protected boolean includeErrorMessage(@NonNull ErrorContext errorContext) { | ||
return errorContext.getRootCause() | ||
.map(t -> t instanceof FooException || t instanceof UnsatisfiedRouteException) | ||
.orElse(false); | ||
} | ||
} | ||
//end::clazz[] |
This file contains 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,3 @@ | ||
The default Problem+JSON payload does not include the `detail` field to avoid accidental information disclosure if the exception root cause is not of type `UnsatisfiedRouteException` or `ThrowableProblem` to avoid accidental information disclosure since 2.2.3. | ||
|
||
You can <<customizingProblemErrorResponseProcessor, customize it>> to include always the detail or for some scenarios. |
9 changes: 9 additions & 0 deletions
9
src/main/docs/guide/customizingProblemErrorResponseProcessor.adoc
This file contains 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,9 @@ | ||
The default Problem+JSON payload does not include the `detail` field to avoid accidental information disclosure if the exception root cause is not of type `UnsatisfiedRouteException` or `ThrowableProblem`. | ||
|
||
You can extend api:problem.ProblemErrorResponseProcessor[] to customize the behaviour: | ||
|
||
[source,java] | ||
---- | ||
include::problem-json/src/test/java/io/micronaut/problem/ProblemErrorResponseProcessorReplacement.java[tag=clazz] | ||
---- | ||
|
This file contains 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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
introduction: | ||
title: Introduction | ||
releaseHistory: Release History | ||
breaks: Breaking Changes | ||
installation: | ||
Installation | ||
usage: | ||
title: Usage | ||
problemBuilder: Problem Builder | ||
customProblem: Custom Problem | ||
configuration: | ||
Configuration | ||
configuration: Configuration | ||
customizingProblemErrorResponseProcessor: Custom ProblemErrorResponseProcessor | ||
repository: Repository | ||
|