-
-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
authhelper: Tweak auth report escaping
Signed-off-by: kingthorin <[email protected]>
- Loading branch information
1 parent
a86b697
commit 9945c29
Showing
5 changed files
with
113 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,9 +30,12 @@ | |
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import net.sf.json.JSONArray; | ||
import net.sf.json.JSONObject; | ||
import org.apache.commons.text.StringEscapeUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
|
@@ -118,7 +121,7 @@ void shouldGenerateEmptyAuthJsonReport() throws Exception { | |
assertThat(json.getString("@programName"), is(equalTo("ZAP"))); | ||
assertThat(json.getString("@version"), is(equalTo("Test Build"))); | ||
assertThat(json.getString("@generated").length(), is(greaterThan(20))); | ||
assertThat(json.getString("afEnv"), is(equalTo(""))); | ||
assertThat(json.getString("afEnv"), is(equalTo("null"))); | ||
assertThat(summaryItems.size(), is(equalTo(0))); | ||
assertThat(statistics.size(), is(equalTo(0))); | ||
} | ||
|
@@ -156,7 +159,7 @@ void shouldGenerateFilledAuthJsonReport() throws Exception { | |
password: [email protected] | ||
username: test123 | ||
name: test"""; | ||
ard.setAfEnv(StringEscapeUtils.escapeJson(afEnv)); | ||
ard.setAfEnv(afEnv); | ||
ard.addSummaryItem(true, "summary.1", "First Item"); | ||
ard.addSummaryItem(false, "summary.2", "Second Item"); | ||
ard.addStatsItem("stats.auth.1", "global", 123); | ||
|
@@ -197,6 +200,86 @@ void shouldGenerateFilledAuthJsonReport() throws Exception { | |
assertThat(statistics.getJSONObject(2).getInt("value"), is(equalTo(5678))); | ||
} | ||
|
||
@Test | ||
void shouldGenerateFilledAuthJsonReportHandlingSpecialCharacters() throws Exception { | ||
// Given | ||
ExtensionReports extRep = new ExtensionReports(); | ||
String templateName = "auth-report-json"; | ||
Template template = getTemplateFromYamlFile(templateName); | ||
File f = File.createTempFile(templateName, template.getExtension()); | ||
ReportData reportData = getGenericReportData(templateName); | ||
reportData.setSections(template.getSections()); | ||
AuthReportData ard = new AuthReportData(); | ||
reportData.addReportObjects("authdata", ard); | ||
|
||
ard.setSite("https://www.example.com"); | ||
String afEnv = | ||
""" | ||
env: | ||
contexts: | ||
name: 'some "quote" name' | ||
"""; | ||
ard.setAfEnv(afEnv); | ||
ard.addSummaryItem(true, "summary.1", "Bob's \"Item\""); | ||
ard.addSummaryItem(true, "summary.\"2\"", "Foo bar"); | ||
ard.addStatsItem("stats.auth.1", "foo \"random\" bar", 123); | ||
ard.addStatsItem("stats.foo.oops \"foo\" bar", "global", 0); | ||
// When | ||
File r = extRep.generateReport(reportData, template, f.getAbsolutePath(), false); | ||
String report = Files.readString(r.toPath()); | ||
|
||
// Then | ||
LocalDateTime localDateTime = LocalDateTime.now(); | ||
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault()); | ||
String current = zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME); | ||
String expected = | ||
""" | ||
{ | ||
\t\"@programName\": \"ZAP\", | ||
\t\"@version\": \"Test Build\", | ||
\t\"@generated\": \"@@@replace@@@\", | ||
\t\"site\": \"https:\\/\\/www.example.com\" | ||
\t\ | ||
\t,\"summaryItems\": [ | ||
\t\t{ | ||
\t\t\t\"description\": \"Bob's \\\"Item\\\"\", | ||
\t\t\t\"passed\": true, | ||
\t\t\t\"key\": \"summary.1\" | ||
\t\t}, | ||
\t\t{ | ||
\t\t\t\"description\": \"Foo bar\", | ||
\t\t\t\"passed\": true, | ||
\t\t\t\"key\": \"summary.\\\"2\\\"\" | ||
\t\t} | ||
\t] | ||
\t | ||
\t | ||
\t,\"afEnv\": \" env:\\n contexts:\\n name: 'some \\\"quote\\\" name'\\n\" | ||
\t | ||
\t | ||
\t,\"statistics\": [ | ||
\t\t{ | ||
\t\t\t\"key\": \"stats.auth.1\", | ||
\t\t\t\"scope\": \"foo \\\"random\\\" bar\", | ||
\t\t\t\"value\": 123 | ||
\t\t}, | ||
\t\t{ | ||
\t\t\t\"key\": \"stats.foo.oops \\\"foo\\\" bar\", | ||
\t\t\t\"scope\": \"global\", | ||
\t\t\t\"value\": 0 | ||
\t\t} | ||
\t] | ||
\t | ||
} | ||
""" | ||
.replace("@@@replace@@@", current); | ||
report = | ||
report.replaceAll( | ||
"[a-zA-Z]{3}, \\d{1,2} [a-zA-Z]{3} \\d{4} \\d{2}:\\d{2}:\\d{2}", current); | ||
assertThat(report, is(equalTo(expected))); | ||
} | ||
|
||
static Template getTemplateFromYamlFile(String templateName) throws Exception { | ||
return new Template( | ||
TestUtils.getResourcePath( | ||
|