Security Review
Relevant source files
The following files were used as context for generating this wiki page:
The Security Review is an experimental feature within Dyad that leverages an AI agent to perform automated security analysis on an application's codebase. It identifies potential vulnerabilities, categorizes them by severity, and presents them in a dedicated UI panel. Users can review the findings and then instruct the AI to generate and apply fixes for individual or multiple issues, streamlining the process of identifying and remediating security risks.
The feature is designed to focus on vulnerabilities that could lead to data breaches, leaks, or unauthorized access, such as injection attacks, authentication flaws, and exposed client-side secrets.
Sources: src/components/preview_panel/SecurityPanel.tsx:23-26, src/prompts/security_review_prompt.ts:2-3
Core Workflow
The security review process is initiated by the user and involves interaction between the Dyad frontend, the backend IPC client, and the AI model. The user can run a review, see the results, and then choose to fix one or more identified issues.
The following diagram illustrates the sequence of events when a user runs a security review and then requests a fix for an issue.
AI Prompting
The core of the security review is driven by a specialized system prompt that instructs the AI model on how to behave.
Sources: src/prompts/security_review_prompt.ts
System Prompt Configuration
The AI is assigned the role of a "Security expert" and is given specific instructions on what to look for and how to format its output.
- Role: Security expert identifying vulnerabilities that could lead to data breaches, leaks, or unauthorized access.
- Instructions:
- Find real, exploitable vulnerabilities.
- Prioritize client-side exposed secrets and data leaks.
- De-prioritize availability-only issues.
- Use plain language with specific file paths.
- Flag private API keys exposed client-side as critical.
Sources: src/prompts/security_review_prompt.ts:2-3, src/prompts/security_review_prompt.ts:50-55
Focus Areas
The prompt directs the AI to concentrate on high-impact vulnerability categories:
- Authentication & Authorization: Bypass, broken access controls, insecure sessions, JWT/OAuth flaws, privilege escalation.
- Injection Attacks: SQL injection, XSS, command injection.
- API Security: Unauthenticated endpoints, missing authorization, excessive data exposure, IDOR.
- Client-Side Secrets: Exposed private API keys or tokens.
Sources: src/prompts/security_review_prompt.ts:7-19
Custom Security Rules
Users can provide project-specific context to the AI by editing a SECURITY_RULES.md file. This file can be used to describe accepted risks, known non-issues, or environment details to improve the accuracy of the review and reduce false positives. The content of this file is appended to the system prompt.
Sources: src/components/preview_panel/SecurityPanel.tsx:854-860, e2e-tests/snapshots/security_review.spec.ts_security-review---edit-and-use-knowledge-1.txt:93-95
Findings Structure
The AI communicates its findings using a custom XML format, <dyad-security-finding>. The frontend parses this format to display the results in the UI.
<dyad-security-finding> Element
| Attribute / Tag | Description | Example |
|---|---|---|
title (attr) | A brief, descriptive title for the vulnerability. | "SQL Injection in User Lookup" |
level (attr) | The severity of the finding. See Severity Levels. | "critical" |
**What** | A plain-language explanation of the vulnerability. | User input flows directly into database queries... |
**Risk** | The potential impact if the vulnerability is exploited. | An attacker could steal all customer data... |
**Potential Solutions** | A ranked list of options to address the issue. | 1. Use parameterized queries... |
**Relevant Files** | A list of file paths related to the finding. | src/api/users.ts |
Sources: src/prompts/security_review_prompt.ts:21-26, e2e-tests/fixtures/security-review/findings.md:31-40
Example Finding
<dyad-security-finding title="SQL Injection in User Lookup" level="critical">
**What**: User input flows directly into database queries without validation, allowing attackers to execute arbitrary SQL commands
**Risk**: An attacker could steal all customer data, delete your entire database, or take over admin accounts by manipulating the URL
**Potential Solutions**:
1. Use parameterized queries: `db.query('SELECT * FROM users WHERE id = ?', [userId])`
2. Add input validation to ensure `userId` is a number
3. Implement an ORM like Prisma or TypeORM that prevents SQL injection by default
**Relevant Files**: `src/api/users.ts`
</dyad-security-finding>
Sources: src/prompts/security_review_prompt.ts:28-40
Severity Levels
Findings are categorized into four severity levels, each with a specific definition to guide prioritization.
| Level | Definition |
|---|---|
| critical | Actively or trivially exploitable, leading to full system or data compromise with no mitigation. |
| high | Exploitable with some conditions; could lead to significant data exposure, account takeover, or service disruption. |
| medium | Increases exposure or weakens defenses, but exploitation requires multiple steps or sophistication. |
| low | Low immediate risk; typically requires local access, an unlikely chain of events, or only violates best practices. |
Sources: src/prompts/security_review_prompt.ts:42-48
User Interface (SecurityPanel.tsx)
The SecurityPanel is the main React component for the Security Review feature. It orchestrates the UI for running reviews, displaying findings, and initiating fixes.
This diagram shows the primary components within the SecurityPanel.
Key UI Components
| Component | Description | Source File |
|---|---|---|
SecurityHeader | Displays the title, "Run Review" button, "Edit Rules" button, and a summary of the last review. | src/components/preview_panel/SecurityPanel.tsx:210-288 |
RunReviewButton | A button to initiate a new security scan. Shows a loading state while running. | src/components/preview_panel/SecurityPanel.tsx:146-179 |
FindingsTable | A table that lists all identified vulnerabilities, sorted by severity. Allows for selection and initiation of fixes. | src/components/preview_panel/SecurityPanel.tsx:478-615 |
SeverityBadge | A visual indicator of a finding's severity level, with distinct colors and icons. | src/components/preview_panel/SecurityPanel.tsx:133-143 |
FindingDetailsDialog | A modal dialog that shows the full details of a selected security finding. | src/components/preview_panel/SecurityPanel.tsx:617-688 |
EditRulesDialog | A modal for viewing and editing the SECURITY_RULES.md file. | src/components/preview_panel/SecurityPanel.tsx:852-885 |
User Actions
The SecurityPanel component contains the logic for handling all user interactions.
- Run Security Review: The
handleRunSecurityReviewfunction creates a new chat, navigates to it, and sends the/security-reviewprompt to the AI.
Sources: src/components/preview_panel/SecurityPanel.tsx:690-711 - Fix Single Issue: The
handleFixIssuefunction also creates a new chat and sends a detailed prompt asking the AI to fix the specific selected vulnerability.
Sources: src/components/preview_panel/SecurityPanel.tsx:713-741 - Fix Selected Issues: The
handleFixSelectedfunction gathers all selected issues, constructs a single comprehensive prompt, and sends it to the AI in a new chat session to fix them all at once.
Sources: src/components/preview_panel/SecurityPanel.tsx:782-823
Reporting Security Vulnerabilities
For reporting vulnerabilities found in the Dyad project itself, the SECURITY.md file outlines the official policy. It directs individuals to email security@dyad.sh and explicitly requests not to open public GitHub issues for security-related matters.
Sources: SECURITY.md