Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<h2>Latest Greetings</h2>
<c:forEach items="${greetings}" var="greeting">
<p>
${greeting.content}<br>
<c:out value="${greeting.content}"/><br>
Posted: ${greeting.date}
</p>
</c:forEach>
Expand Down
Original file line number Diff line number Diff line change
@@ -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("<c:out\\s+value=\"\\$\\{greeting\\.content\\}\"\\s*/>");

@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("<c:out value=\"${greeting.content}\"/>");

String withoutSafeCout = SAFE_CONTENT_COUT.matcher(content).replaceAll("");
assertThat(withoutSafeCout).doesNotContain("${greeting.content}");
}
}
2 changes: 1 addition & 1 deletion appengine-java8/datastore/src/main/webapp/guestbook.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<h2>Latest Greetings</h2>
<c:forEach items="${greetings}" var="greeting">
<p>
${greeting.content}<br>
<c:out value="${greeting.content}"/><br>
Posted: ${greeting.date}
</p>
</c:forEach>
Expand Down
Original file line number Diff line number Diff line change
@@ -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("<c:out\\s+value=\"\\$\\{greeting\\.content\\}\"\\s*/>");

@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("<c:out value=\"${greeting.content}\"/>");

String withoutSafeCout = SAFE_CONTENT_COUT.matcher(content).replaceAll("");
assertThat(withoutSafeCout).doesNotContain("${greeting.content}");
}
}