Search code examples
keycloak

keycloak translation / message with link should open in new Tab


I would like to have a translation in my messages_en.properties containing a link, which opens in a new tab. Is there a way to do this?

I already tried to add

<a target="_blank" href="http://example.com">Example</a>

and

<a href="#" onclick='window.open("http://example.com");return false;'>Example</a>

without success.

Thanks in advance


Solution

  • Most likely your HTML isn't working because of kcSanitize in the template file. Wherever HTML messages are allowed, Keycloak's FreeMarker templates will also wrap the text with this method which:

    Allows sanitizing of html that uses Freemarker ?no_esc. This way, html can be allowed but it is still cleaned up for safety. Tags and attributes deemed unsafe will be stripped out.

    The no_esc allows HTML to render (by not being escaped) but the kcSanitize strips unsafe tags and attributes first. In my testing, I found that "target" and "onclick" are stripped out.

    One way that works but that I wouldn't recommend for safety is removing the kcSanitize() around where your message displays in the corresponding .ftl file.

    Another idea is adding an id or class to the element in your .properties file and using custom Javascript to set the target="_blank" attribute.