Skip to content

Fix broken images #2195

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

Merged
merged 2 commits into from
Jan 30, 2025
Merged

Fix broken images #2195

merged 2 commits into from
Jan 30, 2025

Conversation

ADecametre
Copy link
Contributor

@ADecametre ADecametre commented Jan 30, 2025

  • Allowed img tags to be used in summaries

My initial pull request (#2186) did not fix images (from issue #2182).
I hope this one will.
Please test this locally before merging just in case, so that I don't have to make a third pull request. 😅

Summary by CodeRabbit

  • New Features
    • Enhanced HTML sanitization by expanding the list of allowed HTML tags, including semantic and structural elements like address, article, aside, footer, and header.
    • Added support for image tags in the sanitization process.

- Allowed img tags to be used in summaries
Copy link

coderabbitai bot commented Jan 30, 2025

📝 Walkthrough

Walkthrough

The pull request modifies the sanitize function in the string.ts utility file by expanding the range of allowed HTML tags. The changes enhance the function's capability to process more semantic and structural HTML elements, including tags like address, article, aside, footer, header, and img. This modification provides more flexibility in handling HTML content while maintaining the existing sanitization logic.

Changes

File Change Summary
libs/utils/src/namespaces/string.ts Extended allowedTags array to include additional semantic HTML tags and image tags; function signature remains unchanged.

Possibly related PRs

Poem

🐰 A Rabbit's Ode to HTML Sanitization

Tags dancing free, no longer confined,
Semantic elements now intertwined,
From header to footer, article's might,
Sanitize function shines ever so bright!
Clean HTML flows with newfound grace 🌟


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8ad5458 and 817ec96.

📒 Files selected for processing (1)
  • libs/utils/src/namespaces/string.ts (1 hunks)
🔇 Additional comments (3)
libs/utils/src/namespaces/string.ts (3)

64-64: LGTM! Fixed the unsafe optional chaining.

The implementation now safely handles undefined options with nullish coalescing.


66-72: Well-organized comprehensive list of HTML tags.

The implementation provides a well-structured and complete list of semantic HTML5 tags, properly organized with helpful comments.


74-74: Consider adding security and performance constraints for images.

While the implementation correctly enables images with essential attributes, consider adding:

  1. URL validation for src attributes to prevent XSS attacks
  2. Size constraints to prevent performance issues with large images

Example implementation:

{
  allowedTags: [
    // ... other tags ...
    "img",
  ],
  allowedAttributes: {
    // ... other attributes ...
    img: {
      src: (value: string) => {
        // Validate URL and domain
        const url = new URL(value);
        return ['trusted-domain.com', 'another-trusted.com'].includes(url.hostname);
      },
      alt: true,
      width: true,
      height: true,
    },
  },
  allowedSchemes: ['https'],
}

Also applies to: 78-79

✅ Verification successful

Review comment is accurate - security and performance constraints are missing

The current sanitization implementation allows any URL in image src attributes and lacks size constraints. The suggested improvements for URL validation, domain whitelisting, and size constraints would enhance security and performance.

  • libs/utils/src/namespaces/string.ts: Current implementation only validates presence of src and alt attributes without any security checks.
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for sanitization configuration
echo "=== Searching for sanitization config ==="
rg -l "allowedTags|allowedAttributes|allowedSchemes" --type ts

echo -e "\n=== Checking complete configuration ==="
rg "allowedTags.*img.*|allowedAttributes.*img.*|allowedSchemes" -A 5 -B 5 --type ts

echo -e "\n=== Looking for URL validation ==="
rg "validateUrl|urlValidation|isValidUrl" --type ts

echo -e "\n=== Checking for image constraints ==="
rg "imageSize|maxSize|maxWidth|maxHeight" --type ts

Length of output: 1503


Script:

#!/bin/bash
# Get the complete sanitization configuration
echo "=== Complete sanitization config ==="
cat libs/utils/src/namespaces/string.ts

Length of output: 3027

✨ 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

@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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a87c5ed and 8ad5458.

📒 Files selected for processing (1)
  • libs/utils/src/namespaces/string.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
libs/utils/src/namespaces/string.ts

[error] 64-64: Unsafe usage of optional chaining.

If it short-circuits with 'undefined' the evaluation will throw TypeError here:

(lint/correctness/noUnsafeOptionalChaining)

🪛 ESLint
libs/utils/src/namespaces/string.ts

[error] 64-64: Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError.

(no-unsafe-optional-chaining)

🔇 Additional comments (2)
libs/utils/src/namespaces/string.ts (2)

73-74: Well-implemented image tag support! ✨

The implementation correctly:

  • Explicitly allows the img tag
  • Restricts attributes to only src and alt
  • Maintains security by not allowing potentially dangerous attributes like onerror

Also applies to: 81-81


66-72: Verify security implications of allowing extensive HTML tags.

While the added semantic HTML tags provide better structure, they increase the attack surface. Please ensure this aligns with your security requirements.

Run this script to check for any security discussions or previous incidents:

✅ Verification successful

HTML tags usage is secure through sanitize-html implementation

The HTML tags are used as a whitelist for the sanitize-html library, a well-maintained security package. This implementation follows security best practices by using a whitelist approach and allowing configuration through options. The tags listed match the default allowed tags from the sanitize-html package.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for security-related discussions about HTML sanitization
rg -i "sanitize|xss|html.*security" --type md

Length of output: 47


Script:

#!/bin/bash
# Check package.json for HTML sanitization libraries
echo "=== Checking package dependencies ==="
fd package.json --type f --exec cat {} \; | grep -i "sanitize\|dompurify\|xss\|html"

echo -e "\n=== Checking HTML processing code ==="
# Look for HTML processing/sanitization logic
rg -i "(sanitize|purify|clean|escape).*html" --type ts -A 3

echo -e "\n=== Finding usage of HTML tags array ==="
# Find where these HTML tags are used
ast-grep --pattern 'const $_ = [$$$"address"$$$]'

Length of output: 1751

@AmruthPillai AmruthPillai merged commit 54ed067 into AmruthPillai:main Jan 30, 2025
1 check passed
@ADecametre
Copy link
Contributor Author

It works, thank you. ❤️

@AmruthPillai
Copy link
Owner

Thank you for the quick fixes and proactive PRs. It's been a hectic week for me as well at work and wasn't able to give Reactive Resume the love it deserves, but you helped a ton.

@stuurman
Copy link

stuurman commented Feb 5, 2025

Thank you for the quick fixes and proactive PRs. It's been a hectic week for me as well at work and wasn't able to give Reactive Resume the love it deserves, but you helped a ton.

Thank you both for this incredible product

@ADecametre
Copy link
Contributor Author

Thank you both for this incredible product

Don't thank me.
I just changed a few lines of code to fix a bug, he made this amazing product.

@ronaldomoraesf
Copy link

É. ÓTIMO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants