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

Potential fix for code scanning alert no. 116: DOM text reinterpreted as HTML #10754

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

rithviknishad
Copy link
Member

@rithviknishad rithviknishad commented Feb 21, 2025

Potential fix for https://github.com/ohcnetwork/care_fe/security/code-scanning/116

To fix the problem, we need to ensure that the content being used in the src attribute of the img tag is safe. One way to do this is to validate the file type and ensure it is an image before creating the object URL. Additionally, we can add a check to ensure that the preview variable contains a valid URL.

  • Validate the file type to ensure it is an image before setting selectedFile.
  • Add a check to ensure that the preview variable contains a valid URL before using it in the src attribute of the img tag.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Summary by CodeRabbit

  • New Features

    • Enhanced the avatar editing experience to securely sanitize image previews, ensuring that only safe content is displayed.
  • Refactor

    • Streamlined the file selection process for clearer and more reliable image preview handling.

… as HTML

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Copy link
Contributor

coderabbitai bot commented Feb 21, 2025

Walkthrough

This change introduces the DOMPurify library in the Avatar Edit Modal component. The file selection logic has been updated to store the selected file in a dedicated variable before performing type checks. Additionally, the image preview now conditionally sanitizes URLs that start with "blob:" for enhanced safety. Overall, these updates improve code clarity and maintain functional parity while ensuring better sanitization of file URLs.

Changes

File(s) Change Summary
src/components/Common/AvatarEditModal.tsx Added DOMPurify import and updated file selection logic to store the file in a variable before type-checking. Revised image preview logic to sanitize blob URLs with DOMPurify, defaulting to imageUrl for other cases.

Sequence Diagram(s)

sequenceDiagram
    participant User as User
    participant Modal as AvatarEditModal
    participant Purify as DOMPurify

    User->>Modal: Upload file via input event
    activate Modal
    Modal->>Modal: Store selected file in variable
    Modal->>Modal: Check if file is a valid image type
    Modal->>Modal: Generate image preview URL
    alt URL starts with "blob:"
        Modal->>Purify: Sanitize preview URL
        Purify-->>Modal: Return sanitized URL
    else
        Modal->>Modal: Use original imageUrl
    end
    deactivate Modal
Loading

Possibly related PRs

Suggested labels

tested, P1

Suggested reviewers

  • Jacobjeevan

Poem

I'm a rabbit with paws that code all day,
Hopping through files in a magical way,
With DOMPurify, I snip away the taint,
And clear up previews with a gentle saint,
My fur twitches with glee at changes so bright,
Celebrating each commit with a rabbit delight!
🐰

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Feb 21, 2025

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit 738a005
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/67b8bddd3c576d00087ac734
😎 Deploy Preview https://deploy-preview-10754.preview.ohc.network
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

Deploying care-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: c5088fb
Status: ✅  Deploy successful!
Preview URL: https://744398a5.care-fe.pages.dev
Branch Preview URL: https://alert-autofix-116.care-fe.pages.dev

View logs

Copy link

Deploying care-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: 738a005
Status:⚡️  Build in progress...

View logs

@rithviknishad rithviknishad marked this pull request as ready for review February 21, 2025 17:54
@rithviknishad rithviknishad requested a review from a team as a code owner February 21, 2025 17:54
Copy link

Deploying care-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: 738a005
Status: ✅  Deploy successful!
Preview URL: https://e1a80d44.care-fe.pages.dev
Branch Preview URL: https://alert-autofix-116.care-fe.pages.dev

View logs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/components/Common/AvatarEditModal.tsx (2)

220-224: Simplify the condition using optional chaining.

The condition can be simplified for better readability.

-preview && preview.startsWith("blob:")
+preview?.startsWith("blob:")
🧰 Tools
🪛 Biome (1.9.4)

[error] 221-221: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


380-380: Consider sanitizing webcam preview for consistency.

While the webcam preview comes from a trusted source, consider sanitizing it for consistency with other image sources and defense in depth.

-<img src={previewImage} />
+<img src={DOMPurify.sanitize(previewImage ?? "")} />
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 569fecb and 738a005.

📒 Files selected for processing (1)
  • src/components/Common/AvatarEditModal.tsx (3 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/components/Common/AvatarEditModal.tsx

[error] 221-221: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: cypress-run (1)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: Cloudflare Pages: care-fe
🔇 Additional comments (2)
src/components/Common/AvatarEditModal.tsx (2)

1-1: LGTM! Good choice of sanitization library.

Adding DOMPurify is a good security practice for sanitizing HTML content.


135-141: LGTM! Improved file handling logic.

The changes enhance code readability by storing the file in a variable and performing type checking before setting it.

Copy link

cypress bot commented Feb 21, 2025

CARE    Run #4925

Run Properties:  status check passed Passed #4925  •  git commit 738a005bd2: Potential fix for code scanning alert no. 116: DOM text reinterpreted as HTML
Project CARE
Branch Review alert-autofix-116
Run status status check passed Passed #4925
Run duration 07m 03s
Commit git commit 738a005bd2: Potential fix for code scanning alert no. 116: DOM text reinterpreted as HTML
Committer Rithvik Nishad
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 11
View all changes introduced in this branch ↗︎

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.

2 participants