You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在React SharePoint现代Web部件中安全使用dangerouslySetInnerHTML的正确性验证与测试方法咨询

SharePoint React Web Part: dangerouslySetInnerHTML Safety & Testing

First, let's recap your setup: you're building a SharePoint modern web part with rich text modals, using dangerouslySetInnerHTML and have added DOMPurify to sanitize user input. Let's break down your two questions clearly:


1. Is your current dangerouslySetInnerHTML security approach correct? Are there missing safeguards?

Your core implementation is solid—using DOMPurify to sanitize user-provided HTML before passing it to dangerouslySetInnerHTML is the industry-standard way to mitigate XSS risks with this React property. Here's a breakdown of what you did right, plus a few extra safeguards you might want to add:

What you got right:

  • You installed dompurify (the gold standard for client-side HTML sanitization) and eslint-plugin-risxss (to catch unsafe dangerouslySetInnerHTML usage during development/code reviews).
  • You replaced raw prop usage with sanitize(myprops.OurValue) to clean untrusted input before rendering, which is the critical first step to prevent XSS.

Potential missing safeguards:

  • Enforce ESLint rules: Make sure your ESLint config enables the risxss/no-dangerously-set-inner-html rule. This will throw errors if anyone uses dangerouslySetInnerHTML without sanitizing the input first, preventing accidental unsafe changes down the line.
  • Customize DOMPurify's whitelist: By default, DOMPurify allows most safe HTML tags, but you can restrict it to only the tags/attributes your users actually need (e.g., <p>, <strong>, <a> with valid href). This reduces the attack surface further:
    import { sanitize } from 'dompurify';
    
    // Restrict to only basic formatting tags
    const sanitizeConfig = {
      ALLOWED_TAGS: ['p', 'strong', 'em', 'a', 'ul', 'li'],
      ALLOWED_ATTR: ['href', 'target']
    };
    
    // Use in your component:
    <div dangerouslySetInnerHTML={{ __html: sanitize(myprops.OurValue, sanitizeConfig) }} />
    
  • Frontend input feedback: Add a basic warning in the property pane if users input potentially malicious code (like <script> tags). This isn't a replacement for sanitization, but it guides users away from risky input.
  • Server-side sanitization (optional): If you're storing this HTML in SharePoint lists, add server-side sanitization when saving the data. This adds a second layer of defense in case client-side sanitization fails or is bypassed.

2. How to test if the sanitize method is working?

You can validate DOMPurify's effectiveness in three practical ways:

Manual testing in the browser

  1. In your web part's property pane, input a malicious HTML snippet like:
    <script>alert('XSS Attack!');</script>
    <p>This is <strong>safe</strong> text, but the script should be removed.</p>
    <img src="fake.jpg" onerror="alert('Another XSS!')" />
    
  2. Open the modal and inspect the rendered DOM using your browser's developer tools.
  3. Verify that:
    • The <script> tag is completely removed.
    • The onerror attribute on the <img> tag is stripped.
    • The safe <p> and <strong> tags remain intact.

Unit testing with Jest (or your test runner)

Write test cases to automate validation that sanitization works as expected. Example:

import { sanitize } from 'dompurify';

describe('DOMPurify sanitization', () => {
  it('removes script tags entirely', () => {
    const maliciousInput = '<script>alert("XSS")</script>Hello World';
    const sanitizedOutput = sanitize(maliciousInput);
    expect(sanitizedOutput).not.toContain('<script>');
    expect(sanitizedOutput).toBe('Hello World');
  });

  it('strips dangerous event attributes', () => {
    const maliciousInput = '<img src="test.jpg" onerror="alert(\'XSS\')" />';
    const sanitizedOutput = sanitize(maliciousInput);
    expect(sanitizedOutput).toBe('<img src="test.jpg" />');
  });

  it('preserves safe HTML formatting', () => {
    const safeInput = '<p>This is <strong>bold</strong> and <em>italic</em> text.</p>';
    const sanitizedOutput = sanitize(safeInput);
    expect(sanitizedOutput).toBe(safeInput);
  });
});

Debug via console logging

In your React component, add a quick log to confirm the sanitized output before rendering:

const sanitizedContent = sanitize(myprops.OurValue);
console.log('Sanitized HTML Output:', sanitizedContent);

// Then render it:
<div dangerouslySetInnerHTML={{ __html: sanitizedContent }} />

Check your browser's console to confirm malicious code has been stripped before it's rendered to the DOM.


内容的提问来源于stack exchange,提问作者John John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 23:43:15