Skip to content

Commit dd6071c

Browse files
Simplify ghostscript util test for -dSAFER command
With the simple test that only checks that malicious code is not executed we can theoretically have false positive result if command itself will not work. The idea is that we run more simplistic commands which are expected to work the same way in more environments. QA-12194
1 parent 2c8594c commit dd6071c

File tree

3 files changed

+10
-37
lines changed

3 files changed

+10
-37
lines changed

io/src/test/java/com/itextpdf/io/util/GhostscriptHelperTest.java

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,20 @@ public void runGhostScriptTestForAllPages() throws IOException, InterruptedExcep
167167

168168
@Test
169169
public void dSaferParamInGhostScriptHelperTest() throws IOException, InterruptedException {
170-
String cmpPdf = sourceFolder + "maliciousPsInvokingCalcExe.ps";
171-
String maliciousPsInvokingCalcExe = destinationFolder + "maliciousPsInvokingCalcExe.png";
172-
int majorVersion = 0;
173-
int minorVersion = 0;
174-
boolean isWindows = identifyOsType().toLowerCase().contains("win");
175-
if (isWindows) {
176-
String gsExec = SystemUtil.getPropertyOrEnvironmentVariable(GhostscriptHelper.GHOSTSCRIPT_ENVIRONMENT_VARIABLE);
177-
if (gsExec == null) {
178-
gsExec = SystemUtil.getPropertyOrEnvironmentVariable(GhostscriptHelper.GHOSTSCRIPT_ENVIRONMENT_VARIABLE_LEGACY);
179-
}
180-
String[] pathParts = gsExec.split("\\d\\.\\d\\d");
181-
for (int i = 0; i < pathParts.length; i++) {
182-
gsExec = gsExec.replace(pathParts[i], "");
183-
}
184-
String[] version = gsExec.split("\\.");
185-
majorVersion = Integer.parseInt(version[0]);
186-
minorVersion = Integer.parseInt(version[1]);
187-
}
170+
String input = sourceFolder + "unsafePostScript.ps";
171+
String outputName = "unsafePostScript.png";
172+
String maliciousResult1 = destinationFolder + "output1.txt";
173+
String maliciousResult2 = destinationFolder + "output2.txt";
188174
try {
189175
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
190-
ghostscriptHelper.runGhostScriptImageGeneration(cmpPdf, destinationFolder, "maliciousPsInvokingCalcExe.png");
191-
if (isWindows) {
192-
Assert.assertTrue((majorVersion > 9 || (majorVersion == 9 && minorVersion >= 50)));
193-
}
176+
ghostscriptHelper.runGhostScriptImageGeneration(input, destinationFolder, outputName);
194177
} catch (GhostscriptHelper.GhostscriptExecutionException e) {
195-
if (isWindows) {
196-
Assert.assertTrue((majorVersion < 9 || (majorVersion == 9 && minorVersion < 50)));
197-
}
178+
System.out.println("Error code was returned on processing of malicious script with -dSAFER option enabled. "
179+
+ "This is expected for some environments and ghostscript versions. "
180+
+ "We assert only the absence of malicious script result (created file).\n");
198181
}
199-
Assert.assertFalse(FileUtil.fileExists(maliciousPsInvokingCalcExe));
182+
Assert.assertFalse(FileUtil.fileExists(maliciousResult1));
183+
Assert.assertFalse(FileUtil.fileExists(maliciousResult2));
200184
}
201185

202186
@Test
@@ -214,15 +198,4 @@ public void ghostScriptImageGenerationTest() throws IOException, InterruptedExce
214198
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
215199
Assert.assertTrue(imageMagickHelper.runImageMagickImageCompare(resultantImage, cmpResultantImage, diff));
216200
}
217-
218-
/**
219-
* Identifies type of current OS and return it (win, linux).
220-
*
221-
* @return type of current os as {@link java.lang.String}
222-
*/
223-
private static String identifyOsType() {
224-
String os = System.getProperty("os.name") == null
225-
? System.getProperty("OS") : System.getProperty("os.name");
226-
return os.toLowerCase();
227-
}
228201
}
Binary file not shown.

0 commit comments

Comments
 (0)