diff --git a/core/src/main/resources/hudson/search/Search/search-failed.jelly b/core/src/main/resources/hudson/search/Search/search-failed.jelly index 98fe619671..378eef12de 100644 --- a/core/src/main/resources/hudson/search/Search/search-failed.jelly +++ b/core/src/main/resources/hudson/search/Search/search-failed.jelly @@ -27,9 +27,9 @@ THE SOFTWARE. --> - + -

Search for '${q}'

+

Search for '${h.escape(q)}'

diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index e79f85519c..5cc2f6e7b2 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -477,10 +477,7 @@ public abstract class HudsonTestCase extends TestCase { * Performs a search from the search box. */ protected Page search(String q) throws Exception { - HtmlPage top = new WebClient().goTo(""); - HtmlForm search = top.getFormByName("search"); - search.getInputByName("q").setValueAttribute(q); - return search.submit(null); + return new WebClient().search(q); } /** @@ -788,6 +785,13 @@ public abstract class HudsonTestCase extends TestCase { return this; } + public HtmlPage search(String q) throws IOException, SAXException { + HtmlPage top = goTo(""); + HtmlForm search = top.getFormByName("search"); + search.getInputByName("q").setValueAttribute(q); + return (HtmlPage)search.submit(null); + } + /** * Short for {@code getPage(r,"")}, to access the top page of a build. */ diff --git a/test/src/test/java/hudson/search/SearchTest.java b/test/src/test/java/hudson/search/SearchTest.java index aed6189548..f6e5d14d08 100644 --- a/test/src/test/java/hudson/search/SearchTest.java +++ b/test/src/test/java/hudson/search/SearchTest.java @@ -24,7 +24,10 @@ package hudson.search; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.AlertHandler; import org.jvnet.hudson.test.HudsonTestCase; +import org.jvnet.hudson.test.Bug; /** * @author Kohsuke Kawaguchi @@ -41,4 +44,23 @@ public class SearchTest extends HudsonTestCase { assertEquals(404,e.getResponse().getStatusCode()); } } + + /** + * Makes sure the script doesn't execute. + */ + @Bug(3415) + public void testXSS() throws Exception { + try { + WebClient wc = new WebClient(); + wc.setAlertHandler(new AlertHandler() { + public void handleAlert(Page page, String message) { + throw new AssertionError(); + } + }); + wc.search(""); + fail("404 expected"); + } catch (FailingHttpStatusCodeException e) { + assertEquals(404,e.getResponse().getStatusCode()); + } + } }