Fix all open code scanning alerts: weak PRNG, bare asserts, swallowed exceptions#48
Closed
Fix all open code scanning alerts: weak PRNG, bare asserts, swallowed exceptions#48
Conversation
…allowed exceptions Agent-Logs-Url: https://github.com/Clarifai/clarifai-python-datautils/sessions/2f6a225b-0e99-4d58-9831-b149d5e68787 Co-authored-by: sanjaychelliah <65780631+sanjaychelliah@users.noreply.github.com>
…sage Agent-Logs-Url: https://github.com/Clarifai/clarifai-python-datautils/sessions/2f6a225b-0e99-4d58-9831-b149d5e68787 Co-authored-by: sanjaychelliah <65780631+sanjaychelliah@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix all currently open code scanning alerts in repository
Fix all open code scanning alerts: weak PRNG, bare asserts, swallowed exceptions
May 5, 2026
sanjaychelliah
approved these changes
May 5, 2026
There was a problem hiding this comment.
Pull request overview
This PR addresses GitHub code scanning alerts (CWE-338, CWE-703) by replacing a weak PRNG usage with a CSPRNG and replacing runtime assert checks / swallowed exceptions with explicit exceptions and debug logging.
Changes:
- Replaced
random.randint(...)withsecrets.randbelow(...)when generatinginput_idfor image summarization. - Replaced runtime
assertstatements with explicitValueError/TypeErrorraises to ensure checks remain enforced underpython -O. - Replaced
except Exception: passblocks in extractors withlogger.debug(...)to avoid silent exception swallowing while preserving resilient behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
clarifai_datautils/multimodal/pipeline/summarizer.py |
Switches input_id generation from random to secrets for stronger randomness. |
clarifai_datautils/multimodal/pipeline/extractors.py |
Replaces a runtime assert with ValueError and logs previously swallowed exceptions at debug level. |
clarifai_datautils/multimodal/pipeline/base.py |
Replaces a runtime assert with a TypeError for invalid files input type. |
clarifai_datautils/image/annotation_conversion/loaders.py |
Replaces a runtime assert with a ValueError for mismatched bbox/concept counts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| element.metadata.update( | ||
| ElementMetadata.from_dict({ | ||
| 'input_id': f'{random.randint(1000000, 99999999)}' | ||
| 'input_id': str(secrets.randbelow(89000000) + 1000000) |
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.
Six open code scanning alerts (CWE-338, CWE-703) across four files. All fixed with minimal, targeted changes that preserve existing behavior.
Changes
summarizer.py— CWE-338: Cryptographically weak PRNGrandom.randint(1000000, 99999999)→secrets.randbelow(89000000) + 1000000randomis predictable;secretsuses the OS CSPRNG. Numeric format preserved.loaders.py·base.py·extractors.py— CWE-703:assertused for runtime checksassertstatements replaced with explicitraise ValueError/raise TypeErrorassertis silently stripped underpython -O, making the checks disappear in optimized bytecodeextractors.py— CWE-703: Bareexcept Exception: passExtractTextAfterandExtractTextBeforereplaced withexcept Exception as exc: logger.debug(...)Original prompt
This pull request was created from Copilot chat.