-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add pf-security-scan skill to code-review plugin #139
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
Open
dlabaj
wants to merge
1
commit into
patternfly:main
Choose a base branch
from
dlabaj:security-patterns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| --- | ||
| name: pf-security-scan | ||
| description: Scan PatternFly React code for security anti-patterns — XSS via dangerouslySetInnerHTML, unsanitized user input in tooltips/labels, and insecure href patterns. Use when reviewing PF code for security vulnerabilities or auditing user-controlled content in PF components. | ||
| --- | ||
|
|
||
| # PF Security Scan | ||
|
|
||
| Audit PatternFly React usage for security anti-patterns that turn UI components into XSS or open-redirect vectors. This skill focuses on **PF-specific hotspots** — not general app security (auth, CSRF, secrets). | ||
|
|
||
| ## PatternFly MCP | ||
|
|
||
| If `@patternfly/patternfly-mcp` is available, use it to confirm which props accept HTML or render links for unfamiliar components. This skill defines **what to flag**; the MCP fills in API details. | ||
|
|
||
| ## How to run | ||
|
|
||
| 1. Ask the user which directory or files to scan. Default to the current working directory. | ||
| 2. Limit scope to files importing from `@patternfly/react-core`, `@patternfly/react-table`, `@patternfly/react-component-groups`, or `@patternfly/react-templates`. | ||
| 3. Run targeted searches (see [references/anti-patterns.md](references/anti-patterns.md) for grep patterns), then read flagged files for context. | ||
| 4. Trace user-controlled data: API responses, URL params, query strings, form input, localStorage, WebSocket payloads, and props passed from parent components. | ||
| 5. Report findings grouped by file with line numbers. End with a summary count. | ||
|
|
||
| ## Anti-patterns to detect | ||
|
|
||
| ### 1. XSS via `dangerouslySetInnerHTML` | ||
|
|
||
| | Severity | Flag when | | ||
| |----------|-----------| | ||
| | **CRITICAL** | `dangerouslySetInnerHTML` with a value that is not a compile-time constant | | ||
| | **CRITICAL** | `__html` built from user/API data without a recognized sanitizer (DOMPurify, sanitize-html, or project-approved equivalent) | | ||
| | **WARN** | `dangerouslySetInnerHTML` anywhere — even with sanitization, confirm the sanitizer config allows only safe tags | | ||
|
|
||
| Also flag PF components that accept HTML strings when fed unsanitized data: | ||
|
|
||
| - `innerHTML`-style props or render callbacks that inject raw HTML | ||
| - Rich-text editors or markdown renderers whose output flows into PF `Text`, `Title`, `Alert`, or table cells without sanitization | ||
|
|
||
| ### 2. Unsanitized user input in tooltips, labels, and text surfaces | ||
|
|
||
| User-controlled strings rendered in PF components are XSS vectors when the data can contain HTML or script payloads. | ||
|
|
||
| | Severity | Component / prop | Flag when | | ||
| |----------|------------------|-----------| | ||
| | **CRITICAL** | `Tooltip` / `Popover` — `content`, `bodyContent`, `headerContent` | Value from user/API state without sanitization | | ||
| | **CRITICAL** | `Label`, `Badge`, `Chip`, `ChipGroup` — children or `text` | User-controlled value rendered directly | | ||
| | **HIGH** | `Alert` — `title`, `titleHeadingLevel`, children | Message from API/error payload without sanitization | | ||
| | **HIGH** | `Title`, `Text`, `Content` — children | User or CMS content interpolated without escaping | | ||
| | **HIGH** | `FormGroup` — `label`, `HelperText` — children | Label or helper text from user input | | ||
| | **HIGH** | `EmptyState` — `titleText`, `body` | Dynamic content from external source | | ||
| | **HIGH** | `DescriptionListDescription`, `DataListCell`, `Td` | Cell/description values from API without sanitization | | ||
| | **WARN** | `MenuItem`, `DropdownItem`, `SelectOption` — children | Display text from user data (lower risk if React text nodes, but still audit source) | | ||
|
|
||
| **Safe patterns** (do not flag): | ||
|
|
||
| - React text children (`<Label>{userName}</Label>`) — React escapes by default | ||
| - Values passed through a project-approved sanitizer at the boundary | ||
| - Compile-time string literals | ||
|
|
||
| **Unsafe patterns** (always flag): | ||
|
|
||
| - `dangerouslySetInnerHTML` in or near PF text surfaces | ||
| - `innerHTML` assignment on DOM nodes inside PF component refs | ||
| - Rendering markdown/HTML from user input without sanitization pipeline | ||
|
|
||
| ### 3. Insecure URL / href patterns | ||
|
|
||
| PF components frequently render links via `Button`, `NavItem`, `BreadcrumbItem`, `DropdownItem`, `MenuItem`, and standalone `<a>` inside PF layouts. | ||
|
|
||
| | Severity | Flag when | | ||
| |----------|-----------| | ||
| | **CRITICAL** | `href` or `to` set to `javascript:` URL (literal or constructed) | | ||
| | **CRITICAL** | `href` built from unsanitized user input without URL validation | | ||
| | **HIGH** | `href` using `data:` scheme with non-image MIME types | | ||
| | **HIGH** | Dynamic `href` from API/params with no allowlist or `URL` constructor validation | | ||
| | **WARN** | `target="_blank"` without `rel="noopener noreferrer"` on external links | | ||
| | **WARN** | `Button component="a"` with dynamic `href` — same rules as `<a>` | | ||
|
|
||
| **PF-specific link surfaces to check:** | ||
|
|
||
| | Component | Props | | ||
| |-----------|-------| | ||
| | `Button` | `href`, `component="a"` | | ||
| | `NavItem` / `NavLink` | `to`, `href` | | ||
| | `BreadcrumbItem` | `to`, `href` | | ||
| | `DropdownItem` | `href`, `to` | | ||
| | `MenuItem` | `to`, `href` | | ||
| | `JumpLinksItem` | `href` | | ||
| | `ExternalLink` (if used) | `href` | | ||
|
|
||
| **Safe patterns:** | ||
|
|
||
| ```tsx | ||
| // Allowlisted origin | ||
| const safeUrl = allowedOrigins.includes(new URL(userUrl).origin) ? userUrl : '#'; | ||
|
|
||
| // React Router internal navigation only | ||
| <NavItem to={`/users/${userId}`}>...</NavItem> | ||
|
|
||
| // External link with tab-nabbing protection | ||
| <Button component="a" href={trustedUrl} target="_blank" rel="noopener noreferrer"> | ||
| ``` | ||
|
|
||
| **Unsafe patterns:** | ||
|
|
||
| ```tsx | ||
| // javascript: injection | ||
| <Button component="a" href={userSuppliedUrl}>...</Button> | ||
|
|
||
| // Unvalidated redirect | ||
| <DropdownItem href={searchParams.get('redirect')}>...</DropdownItem> | ||
|
|
||
| // String concat into href from API | ||
| <BreadcrumbItem href={`/docs/${apiResponse.path}`}>...</BreadcrumbItem> | ||
| ``` | ||
|
|
||
| ## Report format | ||
|
|
||
| For each finding: | ||
|
|
||
| ``` | ||
| [CRITICAL|HIGH|WARN] file/path.tsx:42 - Unsanitized API data in Tooltip content | ||
| Found: <Tooltip content={apiResponse.description}> | ||
| Risk: XSS — attacker-controlled HTML in tooltip popper | ||
| Fix: Sanitize with DOMPurify before render, or render as React text child if HTML is not required | ||
| ``` | ||
|
|
||
| End with: | ||
|
|
||
| ``` | ||
| Scanned: 18 files | ||
| Critical: 2 (across 1 file) | ||
| High: 3 (across 2 files) | ||
| Warnings: 1 (across 1 file) | ||
| ``` | ||
|
|
||
| Group findings by anti-pattern category (XSS, unsanitized input, insecure URLs) so patterns are visible. | ||
|
|
||
| ## Applying fixes | ||
|
|
||
| 1. **Prefer React text nodes** over HTML rendering when formatting is not required. | ||
| 2. **Sanitize at the boundary** — one approved sanitizer at the data ingress point, not scattered per component. | ||
| 3. **Validate URLs** — parse with `new URL()`, allowlist origins or paths, reject `javascript:`, `data:`, and protocol-relative `//` from untrusted input. | ||
| 4. **Add `rel="noopener noreferrer"`** to every `target="_blank"` link. | ||
| 5. Do not weaken sanitizer configs to "make it work" — flag and ask the user if legitimate HTML is required. | ||
|
|
||
| Only apply fixes when the remediation is unambiguous. If a finding needs a product decision (e.g., rich-text support), report it and ask. | ||
|
|
||
| ## Additional resources | ||
|
|
||
| - Grep patterns and component checklist: [references/anti-patterns.md](references/anti-patterns.md) |
70 changes: 70 additions & 0 deletions
70
plugins/code-review/skills/pf-security-scan/references/anti-patterns.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # PF Security Anti-Patterns Reference | ||
|
|
||
| ## Scope grep commands | ||
|
|
||
| Run from the project root. Adjust paths as needed. | ||
|
|
||
| ```bash | ||
| # Files using PatternFly React | ||
| rg "from '@patternfly/react" --glob '*.{tsx,ts,jsx,js}' | ||
|
|
||
| # dangerouslySetInnerHTML (all occurrences need review) | ||
| rg "dangerouslySetInnerHTML" --glob '*.{tsx,ts,jsx,js}' | ||
|
|
||
| # __html object pattern | ||
| rg "__html" --glob '*.{tsx,ts,jsx,js}' | ||
|
|
||
| # javascript: URLs (literal) | ||
| rg "javascript:" --glob '*.{tsx,ts,jsx,js}' | ||
|
|
||
| # data: URLs | ||
| rg "href=.*data:" --glob '*.{tsx,ts,jsx,js}' | ||
|
|
||
| # target="_blank" candidates (manual review for rel attribute) | ||
| rg 'target="_blank"' --glob '*.{tsx,ts,jsx,js}' | ||
|
|
||
| # Dynamic href/to bindings (manual review for validation) | ||
| rg 'href=\{' --glob '*.{tsx,ts,jsx,js}' | ||
| rg 'to=\{' --glob '*.{tsx,ts,jsx,js}' | ||
| ``` | ||
|
|
||
| ## PF component checklist | ||
|
|
||
| Components that commonly receive user-controlled strings or URLs: | ||
|
|
||
| | Category | Components | Props / children | | ||
| |----------|------------|------------------| | ||
| | Tooltips / popovers | `Tooltip`, `Popover` | `content`, `bodyContent`, `headerContent` | | ||
| | Labels / chips | `Label`, `Badge`, `Chip`, `ChipGroup` | children, `text` | | ||
| | Links / navigation | `Button`, `NavItem`, `NavLink`, `BreadcrumbItem`, `DropdownItem`, `MenuItem`, `JumpLinksItem` | `href`, `to` | | ||
| | Text surfaces | `Title`, `Text`, `Content`, `Alert`, `HelperText`, `FormGroup` | children, `label`, `title` | | ||
| | Data display | `Td`, `DataListCell`, `DescriptionListDescription`, `EmptyState` | children, `dataLabel` | | ||
| | Menus / selects | `SelectOption`, `DropdownItem`, `MenuItem` | children | | ||
|
|
||
| ## Sanitizer recognition | ||
|
|
||
| Treat these as approved sanitization when applied to the flagged value **before** render: | ||
|
|
||
| - `DOMPurify.sanitize(...)` / `dompurify` | ||
| - `sanitize-html(...)` / `sanitizeHtml` | ||
| - Project-specific `sanitize*`, `cleanHtml`, `stripHtml` utilities — verify implementation if unfamiliar | ||
|
|
||
| Absence of sanitization between user/API source and render target is a finding. | ||
|
|
||
| ## URL validation patterns | ||
|
|
||
| | Pattern | Risk | Action | | ||
| |---------|------|--------| | ||
| | `href={userInput}` | Open redirect, `javascript:` injection | Validate origin/path against allowlist | | ||
| | `href={\`https://${domain}/...\`}` | Domain injection if `domain` is user-controlled | Allowlist domains | | ||
| | `href={searchParams.get('next')}` | Open redirect | Reject external URLs; allow relative paths only | | ||
| | `to={location.pathname + userInput}` | Path traversal / XSS in router | Validate path segments | | ||
| | `component="a"` + dynamic `href` | Same as `<a href>` | Apply URL rules | | ||
|
|
||
| ## Severity guide | ||
|
|
||
| | Severity | Criteria | | ||
| |----------|----------| | ||
| | **CRITICAL** | Confirmed or highly likely exploitable XSS or `javascript:` execution path | | ||
| | **HIGH** | User-controlled content in PF render path without sanitization; unvalidated external URLs | | ||
| | **WARN** | Missing `rel` on `target="_blank"`; `dangerouslySetInnerHTML` with sanitization present but unverified; dynamic href with partial validation | | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🔴 Critical | ⚡ Quick win
Include double-quoted imports in the scan scope.
This command only matches
from '@patternfly/react…'. BecauseSKILL.mduses this step to identify files for scanning, valid imports such asfrom "@patternfly/react-core"can be excluded entirely, producing a false “clean” result. Match both quote styles (and any supportedrequireform).🤖 Prompt for AI Agents