-
Notifications
You must be signed in to change notification settings - Fork 0
security: Add input validation for file path in FFmpegVideoSource constructor #3
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.opensourcephysics.cabrillo.tracker.video; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. 🔵 [suggestion] PR title incorrectly states constructor instead of open method The PR title says "Add input validation for file path in FFmpegVideoSource constructor" but the diff modifies the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.awt.image.BufferedImage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.nio.file.Files; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.nio.file.Path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -67,8 +68,21 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void open(Path file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lock.writeLock().lock(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 71 in src/main/java/org/opensourcephysics/cabrillo/tracker/video/FFmpegVideoSource.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. 🟡 [warning] Blocking filesystem I/O held inside write lock Lines 71-77 perform Suggested fix:
Suggested change
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. 🟡 [warning] TOCTOU race condition between exists() and isReadable() checks Line 71 checks |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (file == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 73 in src/main/java/org/opensourcephysics/cabrillo/tracker/video/FFmpegVideoSource.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. 🟡 [warning] Missing validation for non-video file extensions The planner rationale explicitly mentions "point to non-video files, potentially causing native crashes in FFmpeg." However, the implementation does not validate that the file has a video extension (e.g., 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. 🟡 [warning] TOCTOU race between Files.exists and Files.isReadable Lines 73 and 76 check Suggested fix:
Suggested change
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. 🟡 [warning] file.toString() may produce platform-specific string before trim check On line 73, Suggested fix:
Suggested change
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. 🔴 [critical] Validation order bug: empty path check unreachable due to prior existence check On line 73, Suggested fix:
Suggested change
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. 🟡 [warning] Path.toString().trim().isEmpty() is not a valid empty-path test for Path objects Line 73 uses Suggested fix:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Video file path cannot be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!file.toString().trim().isEmpty() && !Files.exists(file)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 76 in src/main/java/org/opensourcephysics/cabrillo/tracker/video/FFmpegVideoSource.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. 🔴 [critical] Empty path check ordered after Files.exists() call risks InvalidPathException On line 73, the condition Mechanism: Fix: Reorder validations: null check → empty check → exists check → readable check. Remove the redundant empty check. Suggested fix:
Suggested change
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. 🟡 [warning] Wrong exception type for I/O permission failure Line 76 throws Suggested fix:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Video file does not exist: " + file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Files.exists(file) && !Files.isReadable(file)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Video file is not readable: " + file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. 🟡 [warning] isOpen() called under write lock may deadlock or behave unexpectedly On line 80, Suggested fix:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (file.toString().trim().isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException("Video file path cannot be empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+76
to
+84
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. 🟡 Empty-path validation is ordered after filesystem checks, causing wrong error message The empty-path check at line 82 is placed after
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback.
Comment on lines
+76
to
+84
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. 📝 Info: Using trim() to detect empty paths may reject valid whitespace-named files The validation uses Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isOpen()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
86
to
88
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. 📝 Info: ReentrantReadWriteLock reentrancy is safe here The Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🚩 Pre-existing: getFrame() accesses shared state without holding any lock
In the pre-existing
getFrame()method, the read lock is released aftervalidateOpen()at line 176, but subsequent operations — bounds checking viametadata.frameCount()(line 180), cache access (line 186), and frame decoding viadecodeFrameAt()(lines 192-195, which accessesgrabberandconverter) — all happen without any lock. Ifclose()is called concurrently,metadatacould be reset,grabber/converterset to null, etc. This is a pre-existing thread-safety gap not introduced by this PR, but worth noting since the PR touches the same class.(Refers to lines 171-197)
Was this helpful? React with 👍 or 👎 to provide feedback.