api-reviewer
API Reviewer
Expert guidance for reviewing and securing Frappe API endpoints to prevent security vulnerabilities.
Overview
Frappe makes it easy to expose API endpoints using the @frappe.whitelist() decorator. However, this convenience can lead to security holes if proper permission checks aren't implemented. This skill helps identify and fix common API security issues.
Common Security Issues
The most common API security problems in Frappe applications:
- Missing role validation: Endpoints accessible to any authenticated user
- Using frappe.get_all instead of frappe.get_list: Bypassing permission checks
- No document permission checks: Modifying records without validation
- SQL injection: Unsafe query construction with user input
- Unrestricted data access: Exposing sensitive information
See references/security-best-practices.md for detailed explanations and examples of each issue.
Security Review Workflow
1. Scan for API Endpoints
Use the included script to discover all @frappe.whitelist() decorated functions:
cd .github/skills/api-reviewer/scripts
python3 scan_api_endpoints.py --path /path/to/app
The script creates/updates docs/api-review.yaml (at the app root) with:
- Function name and location
- Function arguments
- Detected security checks
- Review status and notes
2. Review Security Checks
For each endpoint in the YAML file, verify:
Role Restrictions:
- Does the endpoint use
frappe.only_for("Role")? - Is the role appropriate for the operation?
Permission Checks:
- Does it use
frappe.has_permission()before accessing documents? - Are permission checks comprehensive?
Safe Queries:
- Does it use
frappe.get_list()instead offrappe.get_all()? - Are SQL queries parameterized (not concatenated)?
Input Validation:
- Is user input validated and sanitized?
- Are there checks for malicious input?
3. Document Findings
Update the YAML file with review results:
endpoints:
- function: update_document
file: custom/utils/documents.py
line: 45
reviewed: true
notes: "ISSUE: No permission check before modifying document. Needs frappe.has_permission() call."
4. Fix Security Issues
Apply appropriate security measures based on the findings. Common fixes:
Add role restriction:
@frappe.whitelist()
def admin_function():
frappe.only_for("System Manager")
# Implementation
Add permission check:
@frappe.whitelist()
def update_record(doctype, name, data):
if not frappe.has_permission(doctype, "write", name):
frappe.throw("No permission")
# Implementation
Switch to frappe.get_list:
@frappe.whitelist()
def get_records(doctype):
return frappe.get_list(doctype, fields=["name", "title"]) # Respects permissions
Quick Security Checklist
When reviewing any API endpoint:
- Uses
frappe.only_for()if admin/role-specific - Uses
frappe.has_permission()for document operations - Uses
frappe.get_list()instead offrappe.get_all() - Parameterizes SQL queries (no string concatenation)
- Validates and sanitizes user inputs
- Doesn't expose sensitive data
- Implements pagination for expensive queries
Automated Detection
The scan script automatically detects these security patterns:
has_frappe_only_for: Presence offrappe.only_for()has_frappe_get_list: Usage offrappe.get_list()has_frappe_has_permission: Usage offrappe.has_permission()has_permission_check: Generic permission checking patterns
Review endpoints with false values for these checks more carefully.
Resources
scripts/scan_api_endpoints.py
Python script that scans Python files for @frappe.whitelist() decorators and extracts endpoint information into a YAML file.
Features:
- AST-based parsing for accurate detection
- Detects security patterns automatically
- Preserves review notes when re-scanning
- Generates summary statistics
docs/api-review.yaml
YAML database of discovered API endpoints with security analysis. Located at the app root in the docs/ directory. Updated by the scan script and manually annotated during review.
Structure:
scan_info: Statistics (total, reviewed, unreviewed)endpoints: List of all discovered endpoints with metadata
references/security-best-practices.md
Comprehensive guide to API security in Frappe, including:
- Detailed explanations of common security issues
- Bad vs. good code examples
- Security checklist
- Common security functions reference
- Review workflow guidance
Usage Examples
Example 1: Initial security audit
# Scan the app
cd .github/skills/api-reviewer/scripts
python3 scan_api_endpoints.py --path tweaks
# Review generated YAML file
# Look for endpoints with all security_checks: false
# Fix identified issues and mark as reviewed
Example 2: Regular security monitoring
# Re-scan after adding new features
python3 scan_api_endpoints.py --path tweaks
# Check scan_info.unreviewed count
# Review only new/unreviewed endpoints
Example 3: Reviewing specific endpoint
# Find endpoint in docs/api-review.yaml
# Check security_checks flags
# Read the actual code at the file:line location
# Apply fixes based on security-best-practices.md
# Mark reviewed: true and add notes
More from kehwar/frappe_tweaks
frappe-tweaks-power-query-expert
Expert guidance for connecting Power Query (Power BI, Excel) to Frappe apps and reports. Use when building Power Query M code for Frappe data access, integrating Frappe reports with Power BI/Excel, implementing authentication for Power Query connections, handling heavy/long-running reports with report_long_polling API to avoid timeouts, applying column types and transformations, or troubleshooting Power Query caching and connection issues.
6open-observe-api-expert
Expert guidance for OpenObserve API integration in Frappe Tweaks. Use when creating, configuring, or troubleshooting OpenObserve API DocType, implementing send_logs() or search_logs() functionality, integrating with Server Scripts/Business Logic/Client-side code, debugging connection issues, or implementing logging, monitoring, error tracking, performance metrics, or audit trail use cases.
5frappe-ci-expert
Expert guidance for setting up CI/CD tests for Frappe apps. Use when users ask about GitHub Actions workflows, CI test setup, continuous integration for Frappe apps, running tests in CI environments, database setup for CI, bench configuration in CI, or automating tests for Frappe/ERPNext applications.
4workflow-expert
Expert guidance on Frappe Workflow system including workflow structure, states and transitions, workflow actions, email notifications, permission hooks (before_transition, after_transition, filter_workflow_transitions, has_workflow_action_permission), and best practices. Use when creating workflows, implementing workflow logic, understanding state transitions, working with workflow actions, configuring email notifications, or troubleshooting workflow-related issues.
4report-expert
Expert guidance on Frappe reports including report types, structure, creation workflow, and best practices. Use when creating standard script reports, query reports, understanding report structure, working with columns and filters, or troubleshooting report-related issues.
4skill-importer
Import and synchronize skills from remote GitHub repositories. Use this skill when you need to copy skills from other repositories, maintain a list of remote skill sources, or update local skills with fresh copies from upstream sources.
4