Skip to content

Kotlin: Adapt PathSanitizer#11550

Merged
atorralba merged 9 commits into
github:mainfrom
atorralba:atorralba/kotlin/adapt-path-sanitizer
Dec 7, 2022
Merged

Kotlin: Adapt PathSanitizer#11550
atorralba merged 9 commits into
github:mainfrom
atorralba:atorralba/kotlin/adapt-path-sanitizer

Conversation

@atorralba
Copy link
Copy Markdown
Contributor

@atorralba atorralba commented Dec 2, 2022

Adapts PathSanitizer.qll so that it works properly on Kotlin codebases too. Mainly handles $default methods (which have a different name) and extension methods (which have a different declaring type and receiver position).

Note the considerable amount of MISSING/SPURIOUS alerts in the Kotlin test: all are due to an extraction error where String.equals and Path.toString are extracted as Object.equals and Object.toString respectively, causing the local taint flow calculations (which are needed for many sanitizers/guards) to fail.

@atorralba atorralba requested a review from a team as a code owner December 2, 2022 17:19
@github-actions github-actions Bot added the Java label Dec 2, 2022
Copy link
Copy Markdown
Contributor

@intrigus-lgtm intrigus-lgtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is intended to fix, #11460 (comment)
shouldn't there be a test for https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/starts-with.html?
From the CodeQL code it seems that this is modeled, but could also be that I just missed the tests for it :D


private void blockListGuardValidation(String path) throws Exception {
if (path.contains("..") || !path.startsWith("/data"))
if (path.contains("..") || path.startsWith("/data"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a blocklist guard should check for forbidden paths + path normalization/check for ... So it was wrong before this change — of course, the Java test should've failed before, I still need to figure out why it didn't.

@intrigus-lgtm
Copy link
Copy Markdown
Contributor

@atorralba atorralba force-pushed the atorralba/kotlin/adapt-path-sanitizer branch from 089d494 to 47d61e0 Compare December 5, 2022 10:54
tamasvajk
tamasvajk previously approved these changes Dec 6, 2022
Copy link
Copy Markdown
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I added some nitpicking comments for consideration.

Comment thread java/ql/test/library-tests/pathsanitizer/TestKt.kt Outdated
Comment thread java/ql/test/library-tests/pathsanitizer/TestKt.kt Outdated
Comment thread java/ql/lib/semmle/code/java/security/PathSanitizer.qll Outdated
Comment thread java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Comment thread java/ql/lib/semmle/code/java/security/PathSanitizer.qll Outdated
Comment thread java/ql/lib/semmle/code/java/security/PathSanitizer.qll Outdated
@atorralba
Copy link
Copy Markdown
Contributor Author

atorralba commented Dec 7, 2022

Thanks for the thorough review @tamasvajk! I added your suggestions in 2f622ad, it looks much better now. I also went a step further and added another predicate getVisualArgument(ma, argPos) to get an argument by position regardless of the callable being an extension method or not. Let me know if something doesn't look correct to you.

BTW, I think getVisualQualifier/Argument and getSourceMethod are valuable enough to have them globally available as predicates of MethodAccess and Method respectively.

Comment thread java/ql/lib/semmle/code/java/security/PathSanitizer.qll Outdated
Copy link
Copy Markdown
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
I'll need to adjust the indexing a bit after #11588 is merged. There are some special cases of $default extension methods.

@atorralba atorralba merged commit 321a2f5 into github:main Dec 7, 2022
@atorralba atorralba deleted the atorralba/kotlin/adapt-path-sanitizer branch December 7, 2022 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants