Security: Use of assert statements for input validation in production code#435
Merged
m-albert merged 1 commit intoMay 27, 2026
Conversation
The file `dask_image/ndfourier/_utils.py` uses Python `assert` statements for input validation (e.g., lines checking `issubclass(dtype, numbers.Real)`). Assert statements can be disabled globally when Python is run with the `-O` (optimize) flag, which would bypass these checks entirely. This could allow invalid inputs to propagate through the code, potentially causing crashes, incorrect results, or undefined behavior in production environments where optimized Python execution is used. Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR hardens dask_image.ndfourier utilities by replacing assert-based input validation (which can be disabled with python -O) with explicit runtime checks that raise appropriate exceptions.
Changes:
- Replace
assert len(shape) == len(chunks)with an explicitValueErrorin_get_freq_grid. - Replace
assert-baseddtypevalidation with explicitTypeErrorchecks in_get_freq_gridand_get_ang_freq_grid.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Collaborator
|
Thanks for this PR @tomaioo. I wasn't aware of this consequence of using asserts! LGTM, mering. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Security: Use of assert statements for input validation in production code
Problem
Severity:
Medium| File:dask_image/ndfourier/_utils.py:L18The file
dask_image/ndfourier/_utils.pyuses Pythonassertstatements for input validation (e.g., lines checkingissubclass(dtype, numbers.Real)). Assert statements can be disabled globally when Python is run with the-O(optimize) flag, which would bypass these checks entirely. This could allow invalid inputs to propagate through the code, potentially causing crashes, incorrect results, or undefined behavior in production environments where optimized Python execution is used.Solution
Replace
assertstatements with properifconditions that raise appropriate exceptions (e.g.,ValueError,TypeError). For example, instead ofassert (issubclass(dtype, numbers.Real) and not issubclass(dtype, numbers.Integral)), useif not (issubclass(dtype, numbers.Real) and not issubclass(dtype, numbers.Integral)): raise TypeError("dtype must be a non-integral real number").Changes
dask_image/ndfourier/_utils.py(modified)