[KEYCLOAK-17602] Email account verification link is wrongly encoded

This commit is contained in:
Douglas Palmer 2021-05-31 21:18:50 -07:00 committed by Stian Thorgersen
parent b152d89e22
commit aac0b6ec5f
2 changed files with 19 additions and 6 deletions

View file

@ -21,6 +21,9 @@ import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.owasp.html.PolicyFactory;
/**
@ -41,7 +44,22 @@ public class KeycloakSanitizerMethod implements TemplateMethodModelEx {
String html = list.get(0).toString();
String sanitized = KEYCLOAK_POLICY.sanitize(html);
return sanitized;
return fixURLs(sanitized);
}
private String fixURLs(String msg) {
Pattern hrefs = Pattern.compile("href=\"([^\"]*)\"");
Matcher matcher = hrefs.matcher(msg);
int count = 0;
while(matcher.find()) {
count++;
String original = matcher.group(count);
String href = original.replaceAll("=", "=")
.replaceAll("\\.\\.", ".")
.replaceAll("&", "&");
msg = msg.replace(original, href);
}
return msg;
}
}

View file

@ -61,11 +61,6 @@ public class MailUtils {
final String textChangePwdUrl = getLink(body.getText());
String htmlChangePwdUrl = getLink(body.getHtml());
// undo changes that may have been made by html sanitizer
htmlChangePwdUrl = htmlChangePwdUrl.replace("=", "=");
htmlChangePwdUrl = htmlChangePwdUrl.replace("..", ".");
htmlChangePwdUrl = htmlChangePwdUrl.replace("&", "&");
assertEquals(htmlChangePwdUrl, textChangePwdUrl);
return htmlChangePwdUrl;