Skip to content

Commit

Permalink
KEYCLOAK-16074 Fix check3pCookiesSupported message callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mhajas authored and Bruno Oliveira da Silva committed Nov 13, 2020
1 parent b35cd9b commit a766a1d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion adapters/oidc/js/src/main/resources/keycloak.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@
}

if (event.data !== "supported" && event.data !== "unsupported") {
promise.setError();
return;
} else if (event.data === "unsupported") {
loginIframe.enable = false;
if (kc.silentCheckSsoFallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ public JavascriptTestExecutor login() {
public JavascriptTestExecutor login(JavascriptStateValidator validator) {
return login(null, validator);
}

/**
* Attaches a MutationObserver that sends a message from iframe to main window with incorrect data when the iframe is loaded
*/
public JavascriptTestExecutor attachCheck3pCookiesIframeMutationObserver() {
jsExecutor.executeScript("// Select the node that will be observed for mutations\n" +
" const targetNode = document.body;" +
"" +
" // Options for the observer (which mutations to observe)\n" +
" const config = {attributes: true, childList: true, subtree: true};" +
"" +
" // Callback function to execute when mutations are observed\n" +
" const callback = function (mutationsList, observer) {" +
" console.log(\"Mutation found\");" +
" var iframeNode = mutationsList[0].addedNodes[0];" +
" if (iframeNode && iframeNode.localName === 'iframe') {" +
" var s = document.createElement('script');" +
" s.type = 'text/javascript';" +
" var code = \"window.parent.postMessage('Evil Message', '*');\";" +
" s.appendChild(document.createTextNode(code));" +
" iframeNode.contentDocument.body.appendChild(s);" +
" }" +
" }\n" +
"" +
" // Create an observer instance linked to the callback function\n" +
" const observer = new MutationObserver(callback);" +
"" +
" // Start observing the target node for configured mutations\n" +
" observer.observe(targetNode, config);");

return this;
}

public JavascriptTestExecutor login(String options, JavascriptStateValidator validator) {
if (options == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ public void testInitInHead() {
.validateOutputField(this::assertInitAuth);
}

@Test
public void check3pCookiesMessageCallbackTest() {
testExecutor.attachCheck3pCookiesIframeMutationObserver()
.init(defaultArguments(), this::assertInitNotAuth);
}

protected void assertAdapterIsLoggedIn(WebDriver driver1, Object output, WebElement events) {
assertTrue(testExecutor.isLoggedIn());
}
Expand Down

0 comments on commit a766a1d

Please sign in to comment.