Skip to content
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

[Bug] changeExtensionTo does not account for multiple dots in file name in jssrc #2614

Closed
pr0me opened this issue Apr 28, 2023 · 2 comments · Fixed by #2618
Closed

[Bug] changeExtensionTo does not account for multiple dots in file name in jssrc #2614

pr0me opened this issue Apr 28, 2023 · 2 comments · Fixed by #2618
Labels
bug Something isn't working

Comments

@pr0me
Copy link
Contributor

pr0me commented Apr 28, 2023

The changeExtensionTo method (part of better.files.File) splits file names on the first occurrence of a dot and hence will incorrectly rename any file with multiple extensions, resulting in a java.io.FileNotFoundException when trying to read the file.

To Reproduce
Steps to reproduce the behavior:

  1. importCode the attached example in joern
    example.zip

  2. Observe the following error:

java.io.FileNotFoundException: /home/pr0me/Documents/code/phd/xample/script.ejs (No such file or directory)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
	at scala.io.Source$.fromFile(Source.scala:94)
	at scala.io.Source$.fromFile(Source.scala:79)
	at io.shiftleft.utils.IOUtils$.readLinesInFile(IOUtils.scala:77)
	at io.joern.jssrc2cpg.parser.BabelJsonParser$.readFile(BabelJsonParser.scala:35)
	at io.joern.jssrc2cpg.passes.AstCreationPass.$anonfun$runOnPart$1(AstCreationPass.scala:48)

The original file name is script.js.ejs.

Expected behavior
File renaming should account for the possibility of multiple file extensions.

Desktop (please complete the following information):

  • OS: Linux pop-os 6.0.12-76060006-generic 22.04
  • Joern Version: 1.1.1646
  • javac 11.0.17

Additional context
I confirmed that the error is due to changeExtensionTo.
This

val test_file_0 = File("./xx.js.md")
val altered_0 = test_file_0.changeExtensionTo(".js")
logger.error(s"TEST 0: ${test_file_0} -> ${altered_0}")
val test_file_1 = File("./xx.md.md")
val altered_1 = test_file_0.changeExtensionTo(".js")
logger.error(s"TEST 1: ${test_file_1} -> ${altered_1}")

will result in

TEST 0: ./xx.js.md -> ./xx.js
TEST 1: ./xx.md.md -> ./xx.js

meaning that is also not only due to the extension and the replacement being the same.
From what I saw, changeExtensionTo is only used once in joern but I still was not sure what the best place to fix this would be.

@pr0me pr0me added the bug Something isn't working label Apr 28, 2023
@max-leuthaeuser
Copy link
Contributor

max-leuthaeuser commented Apr 28, 2023

Nice find. Might be something for their bug tracker: https://github.com/pathikrit/better-files/issues

I will look for a quick fix next week. Or we could simply use File.rename from Java IO.

@pr0me
Copy link
Contributor Author

pr0me commented Apr 30, 2023

Good point, I've opened an issue for better-files: pathikrit/better-files#603

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants