Skip to content

Commit c43ac65

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
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 Autoported commit. Original commit hash: [dd6071c3a]
1 parent 00a7062 commit c43ac65

File tree

4 files changed

+11
-39
lines changed

4 files changed

+11
-39
lines changed

itext.tests/itext.io.tests/itext/io/util/GhostscriptHelperTest.cs

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -146,38 +146,21 @@ public virtual void RunGhostScriptTestForAllPages() {
146146

147147
[NUnit.Framework.Test]
148148
public virtual void DSaferParamInGhostScriptHelperTest() {
149-
String cmpPdf = sourceFolder + "maliciousPsInvokingCalcExe.ps";
150-
String maliciousPsInvokingCalcExe = destinationFolder + "maliciousPsInvokingCalcExe.png";
151-
int majorVersion = 0;
152-
int minorVersion = 0;
153-
bool isWindows = IdentifyOsType().ToLowerInvariant().Contains("win");
154-
if (isWindows) {
155-
String gsExec = SystemUtil.GetEnvironmentVariable(GhostscriptHelper.GHOSTSCRIPT_ENVIRONMENT_VARIABLE);
156-
if (gsExec == null) {
157-
gsExec = SystemUtil.GetEnvironmentVariable(GhostscriptHelper.GHOSTSCRIPT_ENVIRONMENT_VARIABLE_LEGACY);
158-
}
159-
String[] pathParts = iText.IO.Util.StringUtil.Split(gsExec, "\\d\\.\\d\\d");
160-
for (int i = 0; i < pathParts.Length; i++) {
161-
gsExec = gsExec.Replace(pathParts[i], "");
162-
}
163-
String[] version = iText.IO.Util.StringUtil.Split(gsExec, "\\.");
164-
majorVersion = Convert.ToInt32(version[0], System.Globalization.CultureInfo.InvariantCulture);
165-
minorVersion = Convert.ToInt32(version[1], System.Globalization.CultureInfo.InvariantCulture);
166-
}
149+
String input = sourceFolder + "unsafePostScript.ps";
150+
String outputName = "unsafePostScript.png";
151+
String maliciousResult1 = destinationFolder + "output1.txt";
152+
String maliciousResult2 = destinationFolder + "output2.txt";
167153
try {
168154
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
169-
ghostscriptHelper.RunGhostScriptImageGeneration(cmpPdf, destinationFolder, "maliciousPsInvokingCalcExe.png"
170-
);
171-
if (isWindows) {
172-
NUnit.Framework.Assert.IsTrue((majorVersion > 9 || (majorVersion == 9 && minorVersion >= 50)));
173-
}
155+
ghostscriptHelper.RunGhostScriptImageGeneration(input, destinationFolder, outputName);
174156
}
175157
catch (GhostscriptHelper.GhostscriptExecutionException) {
176-
if (isWindows) {
177-
NUnit.Framework.Assert.IsTrue((majorVersion < 9 || (majorVersion == 9 && minorVersion < 50)));
178-
}
158+
System.Console.Out.WriteLine("Error code was returned on processing of malicious script with -dSAFER option enabled. "
159+
+ "This is expected for some environments and ghostscript versions. " + "We assert only the absence of malicious script result (created file).\n"
160+
);
179161
}
180-
NUnit.Framework.Assert.IsFalse(FileUtil.FileExists(maliciousPsInvokingCalcExe));
162+
NUnit.Framework.Assert.IsFalse(FileUtil.FileExists(maliciousResult1));
163+
NUnit.Framework.Assert.IsFalse(FileUtil.FileExists(maliciousResult2));
181164
}
182165

183166
[NUnit.Framework.Test]
@@ -194,16 +177,5 @@ public virtual void GhostScriptImageGenerationTest() {
194177
NUnit.Framework.Assert.IsTrue(imageMagickHelper.RunImageMagickImageCompare(resultantImage, cmpResultantImage
195178
, diff));
196179
}
197-
198-
/// <summary>Identifies type of current OS and return it (win, linux).</summary>
199-
/// <returns>
200-
/// type of current os as
201-
/// <see cref="System.String"/>
202-
/// </returns>
203-
private static String IdentifyOsType() {
204-
String os = Environment.GetEnvironmentVariable("os.name") == null ? Environment.GetEnvironmentVariable("OS"
205-
) : Environment.GetEnvironmentVariable("os.name");
206-
return os.ToLowerInvariant();
207-
}
208180
}
209181
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2c8594cff3a3178071365f6c0c984876ab3af968
1+
dd6071c3ac05b5901e1b4b66e94949f6817eca66

0 commit comments

Comments
 (0)