forked from finos/architecture-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize ADR inputs for HTML elements (finos#761)
- Loading branch information
1 parent
f689014
commit 1d08997
Showing
5 changed files
with
153 additions
and
6 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
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
50 changes: 50 additions & 0 deletions
50
calm-hub/src/main/java/org/finos/calm/sanitizer/AdrSanitizer.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,50 @@ | ||
package org.finos.calm.sanitizer; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.finos.calm.domain.adr.Decision; | ||
import org.finos.calm.domain.adr.Link; | ||
import org.finos.calm.domain.adr.NewAdrRequest; | ||
import org.finos.calm.domain.adr.Option; | ||
import org.owasp.html.HtmlPolicyBuilder; | ||
import org.owasp.html.PolicyFactory; | ||
|
||
@ApplicationScoped | ||
public class AdrSanitizer { | ||
|
||
//strict policy that strips all HTML elements | ||
private final PolicyFactory strictPolicy = new HtmlPolicyBuilder().toFactory(); | ||
|
||
public NewAdrRequest sanitizeNewAdrRequest(NewAdrRequest newAdrRequest) { | ||
return new NewAdrRequest( | ||
newAdrRequest.title() == null ? null : strictPolicy.sanitize(newAdrRequest.title()), | ||
newAdrRequest.contextAndProblemStatement() == null ? null : strictPolicy.sanitize(newAdrRequest.contextAndProblemStatement()), | ||
newAdrRequest.decisionDrivers() == null ? null : newAdrRequest.decisionDrivers().stream().map(strictPolicy::sanitize).toList(), | ||
newAdrRequest.consideredOptions() == null ? null : newAdrRequest.consideredOptions().stream().map(this::sanitizeOption).toList(), | ||
newAdrRequest.decisionOutcome() == null ? null : sanitizeDecision(newAdrRequest.decisionOutcome()), | ||
newAdrRequest.links() == null ? null : newAdrRequest.links().stream().map(this::sanitizeLink).toList() | ||
); | ||
} | ||
|
||
private Option sanitizeOption(Option option) { | ||
return new Option( | ||
option.name() == null ? null : strictPolicy.sanitize(option.name()), | ||
option.description() == null ? null : strictPolicy.sanitize(option.description()), | ||
option.positiveConsequences() == null ? null : option.positiveConsequences().stream().map(strictPolicy::sanitize).toList(), | ||
option.negativeConsequences() == null ? null : option.negativeConsequences().stream().map(strictPolicy::sanitize).toList() | ||
); | ||
} | ||
|
||
private Decision sanitizeDecision(Decision decision) { | ||
return new Decision( | ||
decision.chosenOption() == null ? null : sanitizeOption(decision.chosenOption()), | ||
decision.rationale() == null ? null : strictPolicy.sanitize(decision.rationale()) | ||
); | ||
} | ||
|
||
private Link sanitizeLink(Link link) { | ||
return new Link( | ||
link.rel() == null ? null : strictPolicy.sanitize(link.rel()), | ||
link.href() == null ? null : strictPolicy.sanitize(link.href()) | ||
); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
calm-hub/src/test/java/org/finos/calm/sanitizer/TestAdrSanitizerShould.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,83 @@ | ||
package org.finos.calm.sanitizer; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
import org.finos.calm.domain.adr.Decision; | ||
import org.finos.calm.domain.adr.Link; | ||
import org.finos.calm.domain.adr.NewAdrRequest; | ||
import org.finos.calm.domain.adr.Option; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@QuarkusTest | ||
class TestAdrSanitizerShould { | ||
|
||
@Inject | ||
AdrSanitizer adrSanitizer; | ||
|
||
@Test | ||
public void remove_html_tags_from_adr() { | ||
NewAdrRequest unsafeNewAdrRequest = new NewAdrRequest( | ||
"<a href=\"http://my-url.com\"><b><i>My ADR</i></b></a>", | ||
"<img> <body>problem statement", | ||
List.of("<p><h1>decision driver"), | ||
List.of(new Option( | ||
"<p><h1>name", | ||
"<p><h1>description", | ||
List.of("<p><h1> blah"), | ||
List.of("<p><h1> blah") | ||
)), | ||
new Decision(new Option( | ||
"<p><h1>name", | ||
"<p><h1>description", | ||
List.of("<p><h1> blah"), | ||
List.of("<p><h1> blah")), | ||
"blah <h1>" | ||
), | ||
List.of(new Link("<p><h1> blah", "<p><h1> blah")) | ||
); | ||
|
||
NewAdrRequest expectedSafeNewAdrRequest = new NewAdrRequest( | ||
"My ADR", | ||
" problem statement", | ||
List.of("decision driver"), | ||
List.of(new Option( | ||
"name", | ||
"description", | ||
List.of(" blah"), | ||
List.of(" blah") | ||
)), | ||
new Decision(new Option( | ||
"name", | ||
"description", | ||
List.of(" blah"), | ||
List.of(" blah")), | ||
"blah " | ||
), | ||
List.of(new Link(" blah", " blah")) | ||
); | ||
|
||
assertEquals(expectedSafeNewAdrRequest, adrSanitizer.sanitizeNewAdrRequest(unsafeNewAdrRequest)); | ||
} | ||
|
||
@Test | ||
public void handle_null_values_in_adr() { | ||
NewAdrRequest newAdrRequest = new NewAdrRequest( | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null | ||
); | ||
|
||
assertEquals(newAdrRequest, adrSanitizer.sanitizeNewAdrRequest(newAdrRequest)); | ||
} | ||
|
||
} |