@@ -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}
0 commit comments