Skip to content

Potential fix for code scanning alert no. 7: DOM text reinterpreted as HTML#61

Merged
Ndevu12 merged 1 commit intomainfrom
alert-autofix-7
Mar 29, 2026
Merged

Potential fix for code scanning alert no. 7: DOM text reinterpreted as HTML#61
Ndevu12 merged 1 commit intomainfrom
alert-autofix-7

Conversation

@Ndevu12
Copy link
Copy Markdown
Owner

@Ndevu12 Ndevu12 commented Mar 29, 2026

Potential fix for https://github.com/Ndevu12/RichTextEditor/security/code-scanning/7

In general, the fix is to avoid feeding raw, unvalidated user input directly into DOM sinks that the browser interprets, especially where URL-like data is concerned. For this component, the simplest safe approach is to validate and normalize the user-entered URL before using it in previewSrc (and therefore before putting it into the <img src> attribute). We should restrict preview URLs to safe schemes (e.g., http:, https:, and data: for images) and treat any other scheme as invalid, so it never reaches the DOM.

Concretely, we can introduce a small helper function within ImageDialog.tsx to compute a safe preview URL from the current source, filePreview, and url. This helper will:

  • Return filePreview unchanged when source === 'file' (it is produced by readFileAsBase64, which should be a data URL and not attacker-controlled beyond file content).
  • For the URL case, trim the string, reject empty values, parse with new URL(...) or a simple regex, and only allow http, https, or data schemes.
  • Return null if the URL is missing or uses a disallowed scheme.

We then replace the direct computation:

const previewSrc = source === 'file' ? filePreview : url.trim() || null;

with a call to this new helper. No new imports are strictly required; we can use the built-in URL constructor for validation. The change is confined to src/components/Dialogs/ImageDialog.tsx around the preview computation and the addition of the helper function nearby.

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

…s HTML

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Jean Paul Elisa NIYOKWIZERWA <140616733+Ndevu12@users.noreply.github.com>
@Ndevu12 Ndevu12 marked this pull request as ready for review March 29, 2026 14:30
@Ndevu12 Ndevu12 merged commit c934e49 into main Mar 29, 2026
8 checks passed
@Ndevu12 Ndevu12 deleted the alert-autofix-7 branch March 29, 2026 14:31
@Ndevu12 Ndevu12 self-assigned this Mar 29, 2026
@Ndevu12 Ndevu12 added the fix Providing fixes for some issues/bugs label Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Providing fixes for some issues/bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant