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

[WEB-4178] HTML notice fixes #612

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

kennethkalmer
Copy link
Member

@kennethkalmer kennethkalmer commented Jan 22, 2025

Jira Ticket Link / Motivation

A bit of TLC and some fixes for rendering HTML notices from the website's notice API. This is a relatively unused feature, will mostly be triggered by staff members, but still.

This depends on a companion PR in the website that will inject the CSRF tokens into the Voltaire responses so that we have these meta tags available everywhere we use notices (just like Rails does).

It also adds some additional smarts to the notice rendering to be able to handle Rails UJS-style markup, which we do return from the notice API.

Summary of changes

This pull request includes several changes to improve type safety, sanitize HTML content, and refactor the codebase. The most important changes include updating the package.json version, refining the Flash component, sanitizing the Notice component, and adding a custom hook for handling Rails UJS links.

Version Update:

  • package.json: Updated the version from 15.3.0 to 15.3.1-dev.b451a94.

Flash Component Refinement:

  • src/core/Flash.tsx: Updated FlashesProps to use Pick<FlashProps, "type" | "content">[] and removed the removeFlash prop from the Flash component. [1] [2] [3]

Notice Component Sanitization:

  • src/core/Notice.tsx: Added DOMPurify to sanitize bodyText and used dangerouslySetInnerHTML to render sanitized content. Introduced a custom hook useRailsUjsLinks for handling Rails UJS links. [1] [2] [3] [4]

Custom Hook Addition:

  • src/core/hooks/use-rails-ujs-hooks.ts: Created a new custom hook useRailsUjsLinks to manage Rails UJS links, including CSRF token handling and form submission for POST and DELETE methods.

Meganav Component Refactoring:

  • src/core/Meganav.tsx: Refactored MeganavProps and related types, and added type import for NoticeProps. Removed unused properties from MeganavPaths. [1] [2] [3]

How do you manually test this?

The companion PR on the website has all the instructions and a prepared environment to test this.

Merge/Deploy Checklist

  • Written automated tests for implemented features/fixed bugs
  • Rebased and squashed commits
  • Commits have clear descriptions of their changes
  • Checked for any performance regressions

Frontend Checklist

  • No frontend changes in this PR
  • Added before/after screenshots for changes
  • Tested on different platforms/browsers with Browserstack
  • Compared with the initial design / our brand guidelines
  • Checked the code for accessibility issues (VoiceOver User Guide)?

Summary by CodeRabbit

  • Version Update

    • Updated package version to 15.3.1-dev.b451a94
  • Type Changes

    • Modified FlashesProps type to restrict flash message properties
    • Updated Meganav component types, introducing NoticeApiProps
    • Made Notice props type publicly exportable
  • New Features

    • Added useRailsUjsLinks hook for handling Rails UJS links
    • Implemented HTML sanitization for notice component using DOMPurify
  • Improvements

    • Streamlined MeganavPaths type structure
    • Enhanced notice component content rendering

Copy link
Contributor

coderabbitai bot commented Jan 22, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request introduces several modifications across multiple files in the core components. The changes primarily focus on type refinements, HTML content sanitization, and the introduction of a new Rails UJS links hook. The package version has been updated to a development version, signaling ongoing work. Key modifications include updating type definitions in Meganav.tsx, enhancing HTML rendering safety in Notice.tsx, and creating a new custom hook for handling Rails UJS links in use-rails-ujs-hooks.ts.

Changes

File Change Summary
package.json Version updated from "15.3.0" to "15.3.1-dev.b451a94"
src/core/Flash.tsx Modified FlashesProps type to use Pick for items property
src/core/Meganav.tsx Added NoticeApiProps type, updated MeganavPaths and MeganavProps types
src/core/Notice.tsx Added DOMPurify for HTML sanitization, exported NoticeProps type
src/core/hooks/use-rails-ujs-hooks.ts New hook useRailsUjsLinks for handling Rails UJS links

Assessment against linked issues

Objective Addressed Explanation
Fix HTML escaping issues with notices

Possibly related PRs

Suggested reviewers

  • jamiehenson

Poem

🐰 A rabbit's tale of code so bright,
Sanitizing HTML with all my might
Types refined, hooks dancing free
In Voltaire's realm of harmony
Code leaps forward, clean and neat! 🚀


🪧 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
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: 2

🧹 Nitpick comments (2)
src/core/hooks/use-rails-ujs-hooks.ts (2)

5-11: Consider implementing the data-confirm attribute handling.

The RailsUjsLink interface includes a confirm property in the dataset, but the implementation doesn't handle confirmation dialogs.

 const handleClick = (event: MouseEvent): void => {
   const target = event.target as HTMLElement;
   const link = target.closest<RailsUjsLink>("a[data-method]");
   if (!link) return;
+
+  if (link.dataset.confirm && !window.confirm(link.dataset.confirm)) {
+    return;
+  }

   // Check if the clicked link is within this component's container

43-44: Consider supporting additional HTTP methods.

The comment suggests future expansion, but currently only POST and DELETE methods are supported. Consider adding support for PUT and PATCH methods as they are common in REST APIs.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cb7fabd and 177455e.

📒 Files selected for processing (5)
  • package.json (1 hunks)
  • src/core/Flash.tsx (3 hunks)
  • src/core/Meganav.tsx (2 hunks)
  • src/core/Notice.tsx (4 hunks)
  • src/core/hooks/use-rails-ujs-hooks.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • package.json
🧰 Additional context used
🪛 Biome (1.9.4)
src/core/Notice.tsx

[error] 89-89: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🔇 Additional comments (3)
src/core/Meganav.tsx (1)

86-93: LGTM! Well-structured type definition.

The new NoticeApiProps type provides better type safety and clearer structure for notice configuration.

src/core/Flash.tsx (2)

20-20: LGTM! Good use of Pick utility type.

Using Pick to restrict the items array to only include necessary properties improves type safety.


207-207: Verify the removal of removeFlash prop.

The changes suggest that removeFlash is being added to the flash items array but then not used in the Flash component render. This could lead to memory leaks if the removal functionality is broken.

Also applies to: 218-218

src/core/hooks/use-rails-ujs-hooks.ts Show resolved Hide resolved
src/core/Notice.tsx Show resolved Hide resolved
@kennethkalmer kennethkalmer force-pushed the WEB-4178-html-notice-fixes branch from 177455e to ba000bc Compare January 22, 2025 12:20
@kennethkalmer kennethkalmer force-pushed the WEB-4178-html-notice-fixes branch from 7407f6e to deeb09c Compare February 5, 2025 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant