Skip to content

Commit

Permalink
XWIKI-21810: Improve suggest URL escaping in StringClass
Browse files Browse the repository at this point in the history
* Remove usage of a deprecated API in StringClass
* Improved formatting for StringClassTest

(cherry picked from commit 0498711)
  • Loading branch information
manuelleduc committed Jan 25, 2024
1 parent 6b06654 commit d898f41
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;

import org.apache.ecs.xhtml.input;
import org.xwiki.model.reference.LocalDocumentReference;
import org.xwiki.velocity.tools.EscapeTool;
import org.xwiki.xml.XMLUtils;

Expand Down Expand Up @@ -132,14 +133,10 @@ private void displayPickerEdit(input input)
input.setClass("suggested");
XWikiContext xWikiContext = getXWikiContext();
XWiki xwiki = xWikiContext.getWiki();
String path = xwiki.getURL("Main.WebHome", "view", xWikiContext);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(path);
stringBuilder.append('?');
stringBuilder.append(new EscapeTool().url(getParametersMap()));
stringBuilder.append('&');
String path = xwiki.getURL(new LocalDocumentReference("Main", "WebHome"), "view", xWikiContext);
String stringBuilder = String.format("%s?%s&", path, new EscapeTool().url(getParametersMap()));
input.setOnFocus(String.format("new ajaxSuggest(this, {script:\"%s\", varname:\"input\"} )",
escapeJavaScript(stringBuilder.toString())));
escapeJavaScript(stringBuilder)));
}

private Map<String, String> getParametersMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.LocalDocumentReference;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.test.MockitoOldcore;
Expand Down Expand Up @@ -53,8 +54,9 @@ void displayEdit()
{
XWikiContext xWikiContext = this.oldCore.getXWikiContext();
xWikiContext.setURLFactory(this.urlFactory);
when(this.oldCore.getSpyXWiki().getURL("Main.WebHome", "view", xWikiContext)).thenReturn("/a/b");

when(this.oldCore.getSpyXWiki()
.getURL(new LocalDocumentReference("Main", "WebHome"), "view", xWikiContext)).thenReturn("/a/b");

String fieldName = "test";
String spaceName = "\" + alert(1) + \"";
String pageName = "WebHome";
Expand All @@ -68,14 +70,14 @@ void displayEdit()
StringBuffer stringBuffer = new StringBuffer();
stringClass.displayEdit(stringBuffer, fieldName, spaceName + "." + pageName + "_0_", baseClass,
xWikiContext);
assertEquals("<input "
+ "onfocus='new ajaxSuggest(this, &#123;script:&#34;\\/a\\/b?xpage=suggest&#38;"
+ "classname=%22%20%2B%20alert%281%29%20%2B%20%22.WebHome&#38;fieldname=test&#38;firCol=-&#38;"
+ "secCol=-&#38;&#34;, varname:&#34;input&#34;} )' "
+ "size='30' "
+ "id='&#34; + alert(1) + &#34;.WebHome_0_test' "
+ "class='suggested' "
+ "name='&#34; + alert(1) + &#34;.WebHome_0_test' "
assertEquals("<input "
+ "onfocus='new ajaxSuggest(this, &#123;script:&#34;\\/a\\/b?xpage=suggest&#38;"
+ "classname=%22%20%2B%20alert%281%29%20%2B%20%22.WebHome&#38;fieldname=test&#38;firCol=-&#38;"
+ "secCol=-&#38;&#34;, varname:&#34;input&#34;} )' "
+ "size='30' "
+ "id='&#34; + alert(1) + &#34;.WebHome_0_test' "
+ "class='suggested' "
+ "name='&#34; + alert(1) + &#34;.WebHome_0_test' "
+ "type='text'/>", stringBuffer.toString());
}
}

0 comments on commit d898f41

Please sign in to comment.