-
Notifications
You must be signed in to change notification settings - Fork 7
[NAE-2408] Implement field sanitization modes for text data fields #432
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
797ffe6
[NAE-2408] Implement field sanitization modes for text data fields
machacjozef e19f7a1
[NAE-2408] Implement field sanitization modes for text data fields
renczesstefan 26e17b4
Update error message assertions in FieldSanitizationServiceTest
renczesstefan ff46792
Update default sanitization mode to DISABLE_JAVASCRIPT
renczesstefan 569634b
Refactor sanitization behavior to default to 'OFF' mode.
renczesstefan 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
10 changes: 10 additions & 0 deletions
10
...com/netgrif/application/engine/workflow/service/interfaces/IFieldSanitizationService.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,10 @@ | ||
| package com.netgrif.application.engine.workflow.service.interfaces; | ||
|
|
||
|
|
||
| import com.netgrif.application.engine.petrinet.domain.dataset.Field; | ||
|
|
||
| public interface IFieldSanitizationService { | ||
|
|
||
| String sanitize(String value, Field<?> field); | ||
|
|
||
| } |
140 changes: 140 additions & 0 deletions
140
...om/netgrif/application/engine/workflow/service/sanitization/FieldSanitizationService.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,140 @@ | ||
| package com.netgrif.application.engine.workflow.service.sanitization; | ||
|
|
||
| import com.netgrif.application.engine.petrinet.domain.Component; | ||
| import com.netgrif.application.engine.petrinet.domain.dataset.Field; | ||
| import com.netgrif.application.engine.workflow.service.interfaces.IFieldSanitizationService; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.owasp.html.HtmlPolicyBuilder; | ||
| import org.owasp.html.PolicyFactory; | ||
| import org.owasp.html.Sanitizers; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| public class FieldSanitizationService implements IFieldSanitizationService { | ||
|
|
||
| public static final String SANITIZATION_MODE_KEY = "sanitizationMode"; | ||
| public static final String SANITIZATION_ACTION_KEY = "sanitizationAction"; | ||
|
|
||
| private static final PolicyFactory PLAIN_TEXT_POLICY = | ||
| new HtmlPolicyBuilder().toFactory(); | ||
|
|
||
| private static final PolicyFactory SAFE_HTML_BASIC_POLICY = | ||
|
Retoocs marked this conversation as resolved.
|
||
| new HtmlPolicyBuilder() | ||
| .allowElements("b", "i", "u", "em", "strong", "s", "span", "br") | ||
| .toFactory(); | ||
|
|
||
| private static final PolicyFactory SAFE_HTML_LINKS_ONLY_POLICY = | ||
| new HtmlPolicyBuilder() | ||
| .allowElements("a") | ||
| .allowAttributes("href").onElements("a") | ||
| .allowUrlProtocols("http", "https", "mailto") | ||
| .requireRelNofollowOnLinks() | ||
| .toFactory(); | ||
|
|
||
| private static final PolicyFactory SAFE_HTML_NO_LINKS_POLICY = | ||
| Sanitizers.FORMATTING.and(Sanitizers.BLOCKS); | ||
|
|
||
| private static final PolicyFactory SAFE_HTML_POLICY = | ||
| Sanitizers.FORMATTING | ||
| .and(Sanitizers.LINKS) | ||
| .and(Sanitizers.BLOCKS); | ||
|
|
||
| private static final PolicyFactory SAFE_HTML_RELAXED_POLICY = | ||
| Sanitizers.FORMATTING | ||
| .and(Sanitizers.LINKS) | ||
| .and(Sanitizers.BLOCKS) | ||
| .and(Sanitizers.TABLES) | ||
| .and(new HtmlPolicyBuilder() | ||
| .allowElements("code", "pre", "span") | ||
| .toFactory()); | ||
|
|
||
| private static final PolicyFactory DISABLE_JAVASCRIPT_POLICY = | ||
| new HtmlPolicyBuilder() | ||
| .allowElements( | ||
| "a", "abbr", "b", "blockquote", "br", "caption", "code", "col", "colgroup", | ||
| "dd", "del", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", | ||
| "hr", "i", "img", "ins", "li", "ol", "p", "pre", "s", "small", "span", | ||
| "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", | ||
| "tr", "u", "ul" | ||
| ) | ||
| .allowWithoutAttributes( | ||
| "abbr", "b", "blockquote", "br", "caption", "code", "dd", "del", "div", "dl", | ||
| "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", | ||
| "li", "ol", "p", "pre", "s", "small", "span", "strong", "sub", "sup", | ||
| "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u", "ul" | ||
| ) | ||
| .allowAttributes("href").onElements("a") | ||
| .allowAttributes("src", "alt", "title").onElements("img") | ||
| .allowAttributes("colspan", "rowspan").onElements("td", "th") | ||
| .allowUrlProtocols("http", "https", "mailto") | ||
| .requireRelNofollowOnLinks() | ||
| .toFactory(); | ||
|
|
||
| @Override | ||
| public String sanitize(String value, Field<?> field) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Component component = field.getComponent(); | ||
| SanitizationMode mode = SanitizationMode.from(getProperty(component, SANITIZATION_MODE_KEY)); | ||
| SanitizationAction action = SanitizationAction.from(getProperty(component, SANITIZATION_ACTION_KEY)); | ||
|
|
||
| if (mode == SanitizationMode.OFF) { | ||
| log.debug("Sanitization mode OFF for field [{}]", field.getStringId()); | ||
| return value; | ||
| } | ||
|
|
||
| String sanitized = resolvePolicy(mode).sanitize(value); | ||
|
|
||
| if (!value.equals(sanitized)) { | ||
| log.warn("Content was modified by sanitizer for field [{}] (mode={}, action={})", | ||
| field.getStringId(), mode, action); | ||
| if (action == SanitizationAction.REJECT) { | ||
| throw new IllegalArgumentException( | ||
| "Field [" + field.getStringId() + "] contains unsafe content " + | ||
| "and the configured action is REJECT." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return sanitized; | ||
| } | ||
|
|
||
| protected PolicyFactory resolvePolicy(SanitizationMode mode) { | ||
| switch (mode) { | ||
| case SAFE_HTML: | ||
| return SAFE_HTML_POLICY; | ||
| case SAFE_HTML_BASIC: | ||
| return SAFE_HTML_BASIC_POLICY; | ||
| case SAFE_HTML_LINKS_ONLY: | ||
| return SAFE_HTML_LINKS_ONLY_POLICY; | ||
| case SAFE_HTML_NO_LINKS: | ||
| return SAFE_HTML_NO_LINKS_POLICY; | ||
| case SAFE_HTML_RELAXED: | ||
| return SAFE_HTML_RELAXED_POLICY; | ||
| case PLAIN_TEXT: | ||
| return PLAIN_TEXT_POLICY; | ||
| case DISABLE_JAVASCRIPT: | ||
| default: | ||
| return DISABLE_JAVASCRIPT_POLICY; | ||
| } | ||
| } | ||
|
|
||
| protected String getProperty(Component component, String key) { | ||
| if (component == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Map<String, String> properties = component.getProperties(); | ||
| if (properties == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return properties.get(key); | ||
| } | ||
|
|
||
| } | ||
20 changes: 20 additions & 0 deletions
20
...java/com/netgrif/application/engine/workflow/service/sanitization/SanitizationAction.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,20 @@ | ||
| package com.netgrif.application.engine.workflow.service.sanitization; | ||
|
|
||
| public enum SanitizationAction { | ||
| SANITIZE, | ||
| REJECT; | ||
|
|
||
| static SanitizationAction from(String value) { | ||
| if (value == null || value.isBlank()) { | ||
| return SANITIZE; | ||
| } | ||
|
|
||
| for (SanitizationAction action : values()) { | ||
| if (action.name().equalsIgnoreCase(value)) { | ||
| return action; | ||
| } | ||
| } | ||
|
|
||
| return SANITIZE; | ||
|
machacjozef marked this conversation as resolved.
|
||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
...n/java/com/netgrif/application/engine/workflow/service/sanitization/SanitizationMode.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,27 @@ | ||
| package com.netgrif.application.engine.workflow.service.sanitization; | ||
|
|
||
| public enum SanitizationMode { | ||
|
|
||
| OFF, | ||
| PLAIN_TEXT, | ||
| SAFE_HTML, | ||
| SAFE_HTML_BASIC, | ||
| SAFE_HTML_LINKS_ONLY, | ||
| SAFE_HTML_NO_LINKS, | ||
| DISABLE_JAVASCRIPT, | ||
| SAFE_HTML_RELAXED; | ||
|
|
||
| public static SanitizationMode from(String value) { | ||
| if (value == null || value.isBlank()) { | ||
| return OFF; | ||
| } | ||
|
|
||
| for (SanitizationMode mode : values()) { | ||
| if (mode.name().equalsIgnoreCase(value)) { | ||
| return mode; | ||
| } | ||
| } | ||
|
|
||
| return OFF; | ||
| } | ||
| } |
Oops, something went wrong.
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.