Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class KeycloakSanitizerPolicy {

private static final Pattern NAME = Pattern.compile("[a-zA-Z0-9\\-_\\$]+");

private static final Pattern TARGET = Pattern.compile("_blank");

private static final Pattern ALIGN = Pattern.compile(
"(?i)center|left|right|justify|char");

Expand Down Expand Up @@ -102,6 +104,7 @@ public class KeycloakSanitizerPolicy {
.allowStandardUrlProtocols()
.allowAttributes("nohref").onElements("a")
.allowAttributes("name").matching(NAME).onElements("a")
.allowAttributes("target").matching(TARGET).onElements("a")
.allowAttributes(
"onfocus", "onblur", "onclick", "onmousedown", "onmouseup")
.matching(HISTORY_BACK).onElements("a")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public void testEscapes() throws Exception {
assertResult(expectedResult, html);
}

@Test
public void testLinks() throws Exception {
List<String> html = new ArrayList<>();

html.add("<a href=\"https://www.example.org/sub-page\">Link text</a>");
String expectedResult = "<a href=\"https://www.example.org/sub-page\" rel=\"nofollow\">Link text</a>";
assertResult(expectedResult, html);

html.set(0, "<a href=\"https://www.example.org/terms-of-service\" target=\"_blank\">Link text</a>");
expectedResult = "<a href=\"https://www.example.org/terms-of-service\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Link text</a>";
assertResult(expectedResult, html);

html.set(0, "<a href=\"https://www.example.org/sub-page\" target=\"_top\">Link text</a>");
expectedResult = "<a href=\"https://www.example.org/sub-page\" rel=\"nofollow\">Link text</a>";
assertResult(expectedResult, html);

html.set(0, "<a href=\"https://www.example.org/sub-page\" target=\"someframe\">Link text</a>");
expectedResult = "<a href=\"https://www.example.org/sub-page\" rel=\"nofollow\">Link text</a>";
assertResult(expectedResult, html);
}

@Test
public void testUrls() throws Exception {
List<String> html = new ArrayList<>();
Expand Down
Loading