Skip to content

Commit

Permalink
Merge pull request #266 from ShiftLeftSecurity/claudiu/fix/whitespace
Browse files Browse the repository at this point in the history
Add quotes around dir for `ExternalCommand`
  • Loading branch information
ursachec authored Mar 23, 2023
2 parents c8dbd83 + 3386ddf commit 5526606
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/scala/io/shiftleft/js2cpg/io/ExternalCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
import scala.sys.process.{Process, ProcessLogger}
import scala.util.{Failure, Success, Try}
import scala.jdk.CollectionConverters._
import org.apache.commons.lang.StringUtils

object ExternalCommand {

Expand All @@ -18,9 +19,10 @@ object ExternalCommand {
val stdErrOutput = new ConcurrentLinkedQueue[String]
val processLogger = ProcessLogger(stdOutOutput.add, stdErrOutput.add)
val commands = command.split(COMMAND_AND).toSeq
commands
.map(cmd => Try(Process(cmd, dir, extraEnv.toList: _*).!(processLogger)).getOrElse(1))
.sum match {
commands.map { cmd =>
val cmdWithQuotesAroundDir = StringUtils.replace(cmd, inDir, s"'$inDir'")
Try(Process(cmdWithQuotesAroundDir, dir, extraEnv.toList: _*).!(processLogger)).getOrElse(1)
}.sum match {
case 0 =>
Success(stdOutOutput.asScala.mkString(System.lineSeparator()))
case _ =>
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/io/shiftleft/js2cpg/io/ExternalCommandTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.shiftleft.js2cpg.io

import better.files.File
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class ExternalCommandTest extends AnyWordSpec with Matchers {
"ExternalCommand" should {
"run an external command with ProcessBuilder and no spaces in the directory name" in {
File.usingTemporaryDirectory("js2cpgTest") { sourceDir =>
val cmd = "ls " + sourceDir.pathAsString
ExternalCommand.run(cmd, sourceDir.pathAsString) should be a Symbol("success")
}
}

"run an external command with ProcessBuilder and spaces in the directory name" in {
File.usingTemporaryDirectory("js2cpg Test") { sourceDir =>
val cmd = "ls " + sourceDir.pathAsString
ExternalCommand.run(cmd, sourceDir.pathAsString) should be a Symbol("success")
}
}
}
}

0 comments on commit 5526606

Please sign in to comment.