Repository navigation
Prevent kcSanitize bypass that reactivates URLs with JavaScript scheme on the login page #53886
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
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
90 changes: 90 additions & 0 deletions
90
...base/src/test/java/org/keycloak/tests/forms/LoginPageDisplayNameHtmlSanitizationTest.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,90 @@ | ||
| package org.keycloak.tests.forms; | ||
|
|
||
| import org.keycloak.testframework.annotations.InjectRealm; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.oauth.OAuthClient; | ||
| import org.keycloak.testframework.oauth.annotations.InjectOAuthClient; | ||
| import org.keycloak.testframework.realm.ManagedRealm; | ||
| import org.keycloak.testframework.ui.annotations.InjectPage; | ||
| import org.keycloak.testframework.ui.annotations.InjectWebDriver; | ||
| import org.keycloak.testframework.ui.page.LoginPage; | ||
| import org.keycloak.testframework.ui.webdriver.ManagedWebDriver; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openqa.selenium.JavascriptExecutor; | ||
|
|
||
| @KeycloakIntegrationTest | ||
| public class LoginPageDisplayNameHtmlSanitizationTest { | ||
|
|
||
| @InjectRealm | ||
| ManagedRealm managedRealm; | ||
|
|
||
| @InjectOAuthClient | ||
| OAuthClient oauth; | ||
|
|
||
| @InjectWebDriver | ||
| ManagedWebDriver driver; | ||
|
|
||
| @InjectPage | ||
| LoginPage loginPage; | ||
|
|
||
| @Test | ||
| public void decimalNumericCharacterReferenceJavascriptUrlIsNeutralized() { | ||
| String page = renderLoginPageWithDisplayNameHtml("<a href=\"javascript:alert(xss53813)\">pwn</a>"); | ||
| assertNoExecutableScheme(page); | ||
| } | ||
|
|
||
| @Test | ||
| public void hexNumericCharacterReferenceJavascriptUrlIsNeutralized() { | ||
| String page = renderLoginPageWithDisplayNameHtml("<a href=\"javascript:prompt(xss53813)\">pwn</a>"); | ||
| assertNoExecutableScheme(page); | ||
| } | ||
|
|
||
| @Test | ||
| public void legitimateDisplayNameHtmlIsPreserved() { | ||
| String page = renderLoginPageWithDisplayNameHtml("<b>ACME53813</b>"); | ||
| Assertions.assertTrue(page.contains("<b>ACME53813</b>"), "benign display name markup must render"); | ||
| } | ||
|
|
||
| @Test | ||
| public void httpLinkIsPreserved() { | ||
| String page = renderLoginPageWithDisplayNameHtml("<a href=\"http://acme53813.example.com/help?topic=sso&lang=en\">Help</a>"); | ||
| Assertions.assertTrue(page.contains("<a href=\"http://acme53813.example.com/help?topic=sso&lang=en\" rel=\"nofollow\">Help</a>"), | ||
| "http link must be preserved"); | ||
| } | ||
|
|
||
| @Test | ||
| public void httpsLinkIsPreserved() { | ||
| String page = renderLoginPageWithDisplayNameHtml("<a href=\"https://acme53813.example.com/help?topic=sso&lang=en\">Help</a>"); | ||
| Assertions.assertTrue(page.contains("<a href=\"https://acme53813.example.com/help?topic=sso&lang=en\" rel=\"nofollow\">Help</a>"), | ||
| "https link must be preserved"); | ||
| } | ||
|
|
||
| @Test | ||
| public void mailtoLinkIsPreserved() { | ||
| String page = renderLoginPageWithDisplayNameHtml("<a href=\"mailto:support@acme53813.example.com\">Mail</a>"); | ||
| Assertions.assertTrue(page.contains("<a href=\"mailto:support@acme53813.example.com\" rel=\"nofollow\">Mail</a>"), | ||
| "mailto link must be preserved"); | ||
| } | ||
|
|
||
| private String renderLoginPageWithDisplayNameHtml(String displayNameHtml) { | ||
| managedRealm.updateWithCleanup(r -> r.update(realm -> realm.setDisplayNameHtml(displayNameHtml))); | ||
| oauth.openLoginForm(); | ||
| loginPage.assertCurrent(); | ||
| String loginPageUrl = driver.getCurrentUrl(); | ||
| return (String) ((JavascriptExecutor) driver.driver()).executeScript( | ||
| "var xhr = new XMLHttpRequest();" + | ||
| "xhr.open('GET', arguments[0], false);" + | ||
| "xhr.send();" + | ||
| "return xhr.responseText;", loginPageUrl); | ||
| } | ||
|
|
||
| private void assertNoExecutableScheme(String pageSource) { | ||
|
mabartos marked this conversation as resolved.
Resolved
|
||
| String lower = pageSource.toLowerCase(); | ||
| Assertions.assertFalse(lower.contains("javascript&#"), | ||
| "login page must not emit a reactivated javascript URL"); | ||
| Assertions.assertFalse(lower.contains("javascript:alert") || lower.contains("javascript:prompt"), | ||
| "login page must not emit a live javascript URL"); | ||
| } | ||
| } | ||
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.