diff --git a/appengine-java21/ee8/datastore/src/main/webapp/guestbook.jsp b/appengine-java21/ee8/datastore/src/main/webapp/guestbook.jsp index 5d2d5708f7e..2cf678f30ae 100644 --- a/appengine-java21/ee8/datastore/src/main/webapp/guestbook.jsp +++ b/appengine-java21/ee8/datastore/src/main/webapp/guestbook.jsp @@ -25,7 +25,7 @@

Latest Greetings

- ${greeting.content}
+
Posted: ${greeting.date}

diff --git a/appengine-java21/ee8/datastore/src/test/java/com/example/appengine/GuestbookJspTest.java b/appengine-java21/ee8/datastore/src/test/java/com/example/appengine/GuestbookJspTest.java new file mode 100644 index 00000000000..eea273bfed5 --- /dev/null +++ b/appengine-java21/ee8/datastore/src/test/java/com/example/appengine/GuestbookJspTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.regex.Pattern; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests verifying output escaping in {@code guestbook.jsp}. */ +@RunWith(JUnit4.class) +public class GuestbookJspTest { + + private static final Pattern SAFE_CONTENT_COUT = + Pattern.compile(""); + + @Test + public void guestbookJspEscapesGreetingContent() throws IOException { + Path jspPath = + Paths.get(System.getProperty("basedir", "."), "src", "main", "webapp", "guestbook.jsp"); + String content = new String(Files.readAllBytes(jspPath), StandardCharsets.UTF_8); + + assertThat(content).contains("uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\""); + assertThat(content).contains(""); + + String withoutSafeCout = SAFE_CONTENT_COUT.matcher(content).replaceAll(""); + assertThat(withoutSafeCout).doesNotContain("${greeting.content}"); + } +} diff --git a/appengine-java8/datastore/src/main/webapp/guestbook.jsp b/appengine-java8/datastore/src/main/webapp/guestbook.jsp index 4b65c8a308e..3f83eaba7f8 100644 --- a/appengine-java8/datastore/src/main/webapp/guestbook.jsp +++ b/appengine-java8/datastore/src/main/webapp/guestbook.jsp @@ -25,7 +25,7 @@

Latest Greetings

- ${greeting.content}
+
Posted: ${greeting.date}

diff --git a/appengine-java8/datastore/src/test/java/com/example/appengine/GuestbookJspTest.java b/appengine-java8/datastore/src/test/java/com/example/appengine/GuestbookJspTest.java new file mode 100644 index 00000000000..eea273bfed5 --- /dev/null +++ b/appengine-java8/datastore/src/test/java/com/example/appengine/GuestbookJspTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.regex.Pattern; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests verifying output escaping in {@code guestbook.jsp}. */ +@RunWith(JUnit4.class) +public class GuestbookJspTest { + + private static final Pattern SAFE_CONTENT_COUT = + Pattern.compile(""); + + @Test + public void guestbookJspEscapesGreetingContent() throws IOException { + Path jspPath = + Paths.get(System.getProperty("basedir", "."), "src", "main", "webapp", "guestbook.jsp"); + String content = new String(Files.readAllBytes(jspPath), StandardCharsets.UTF_8); + + assertThat(content).contains("uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\""); + assertThat(content).contains(""); + + String withoutSafeCout = SAFE_CONTENT_COUT.matcher(content).replaceAll(""); + assertThat(withoutSafeCout).doesNotContain("${greeting.content}"); + } +}