-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add config to TestCpg and filter php files #2733
Open
johannescoetzee
wants to merge
3
commits into
master
Choose a base branch
from
johannes/php-dir-excludes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 57 additions & 0 deletions
57
joern-cli/frontends/php2cpg/src/main/scala/io/joern/php2cpg/utils/PathFilter.scala
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.joern.php2cpg.utils | ||
|
||
import io.joern.php2cpg.utils.PathFilter.standardisePath | ||
import org.slf4j.LoggerFactory | ||
|
||
import java.io.File | ||
import java.nio.file.Paths | ||
import scala.util.matching.Regex | ||
|
||
class PathFilter(excludeOverrides: Option[List[String]]) { | ||
private val logger = LoggerFactory.getLogger(this.getClass) | ||
|
||
private val sep = if (File.separator == "/") File.separator else "\\\\" | ||
private val startOrSep = s"^([^$sep]+$sep)*" | ||
private val anyDirSuffix = s"$sep.*" | ||
|
||
def dirExclude(dirName: String): Regex = { | ||
val path = escapeSeparators(standardisePath(dirName)) | ||
s"${startOrSep}$path${anyDirSuffix}".r | ||
} | ||
|
||
private def escapeSeparators(filename: String): String = { | ||
if (File.separator == "/") | ||
filename | ||
else | ||
filename.replaceAll(raw"\\", raw"\\\\") | ||
} | ||
|
||
private val excludeRegexes = { | ||
excludeOverrides.getOrElse(PathFilter.DefaultExcludes).map(dirExclude) | ||
} | ||
|
||
def excluded(filename: String): Boolean = { | ||
excludeRegexes.exists(_.matches(filename)) | ||
} | ||
} | ||
|
||
object PathFilter { | ||
val DefaultExcludes: List[String] = List( | ||
"tests", | ||
// For composer projects | ||
"vendor", | ||
".git" | ||
) | ||
|
||
def standardisePath(filename: String): String = { | ||
// Synthetic filename, so don't correct | ||
if (filename.contains("<")) | ||
filename | ||
else | ||
Paths.get(filename).toString | ||
} | ||
|
||
def apply(excludes: Option[List[String]]): PathFilter = { | ||
new PathFilter(excludes) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
joern-cli/frontends/php2cpg/src/test/scala/io/joern/php2cpg/querying/FileTests.scala
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.joern.php2cpg.querying | ||
|
||
import io.joern.php2cpg.testfixtures.PhpCode2CpgFixture | ||
import io.joern.php2cpg.utils.PathFilter | ||
import io.joern.x2cpg.testfixtures.TestCpg | ||
import io.shiftleft.semanticcpg.language._ | ||
import io.joern.php2cpg.Config | ||
|
||
class FileTests extends PhpCode2CpgFixture { | ||
val testFiles = List( | ||
"a.php", | ||
"src/b.php", | ||
"src/sub/c.php", | ||
"tests/d.php", | ||
"tests/sub/e.php", | ||
"vendor/autoload.php", | ||
"vendor/dep/f.php", | ||
"misc/g.php", | ||
".git/h.php", | ||
"sub/tests/i.php", | ||
"nottests/j.php" | ||
) | ||
|
||
private def getCpg(): TestCpg = { | ||
val cpg = code("", fileName = testFiles.head) | ||
testFiles.tail.foreach(path => cpg.moreCode("", fileName = PathFilter.standardisePath(path))) | ||
cpg | ||
} | ||
|
||
"test, vendor, and .git directories should be filtered out by default" in { | ||
val cpg = getCpg() | ||
|
||
cpg.file.name.toSet shouldBe Set("a.php", "src/b.php", "src/sub/c.php", "misc/g.php", "nottests/j.php", "<unknown>") | ||
.map(PathFilter.standardisePath) | ||
} | ||
|
||
"custom excludes should exclude the correct directories" in { | ||
val config = Config(excludeOverrides = Some(List("dep", "src/sub/", "doesntexist"))) | ||
val cpg = getCpg().config(config) | ||
|
||
cpg.file.name.toSet shouldBe Set( | ||
"a.php", | ||
"src/b.php", | ||
"tests/d.php", | ||
"tests/sub/e.php", | ||
"vendor/autoload.php", | ||
"misc/g.php", | ||
".git/h.php", | ||
"sub/tests/i.php", | ||
"nottests/j.php", | ||
"<unknown>" | ||
).map(PathFilter.standardisePath) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with other frontends we've seen bugs with filtering for untested combinations of absolute and relative paths for a) the input path and b) the excluded path. Can you add some tests for here to ensure that works as intended in all cases? |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we split this in two? I think from an end-user's perspective they either want to disable the default exclusions or add additional exclusions, but rarely both at the same time.