secure_code_scanner 1.0.2
secure_code_scanner: ^1.0.2 copied to clipboard
Enterprise-grade Flutter/Dart SAST tool with 18 security detectors. Finds hardcoded secrets, insecure storage, SQL injection, missing SSL pinning, Firebase misconfigs, weak JWT handling, WebView flaws [...]
π secure_code_scanner #
A professional Static Application Security Testing (SAST) tool for Flutter/Dart projects.
Detect security vulnerabilities in your Dart code before they ship β hardcoded secrets, insecure HTTP connections, unsafe storage, dangerous Android permissions, and more.
Features #
- π Recursive project scanning β scans all
.dartfiles,AndroidManifest.xml,pubspec.yaml,build.gradle, and Firebase*.rules - π‘οΈ 15 vulnerability detectors built-in (Enterprise SAST tier):
- Hardcoded API Keys: Detects API keys and secrets hardcoded in the source code.
- Hardcoded Passwords: Finds raw passwords and secrets stored as strings.
- Insecure HTTP Usage: Alerts on using
http://instead of the securehttps://. - Sensitive Data Logging: Detects
printorlogstatements that leak sensitive info. - SharedPreferences Insecure Storage: Checks for unsafe local storage usage.
- Unsafe File Storage: Monitors for dangerous file storage operations on the device.
- Dangerous Android Permissions: Scans
AndroidManifest.xmlfor excessive/dangerous permissions. - Missing Encryption Indicators: Identifies plaintext storage where encryption is needed.
- Input Validation Risks: Detects missing validation in
TextFields. - Firebase Misconfiguration: Scans Firebase setup for connection and rule vulnerabilities.
- SSL / Certificate Pinning Missing: Identifies missing SSL Pinning to prevent MITM attacks.
- SQL Injection: Detects weak patterns in local SQLite database queries.
- WebView Security Misconfiguration: Alerts on unsafe WebView configurations.
- JWT Weak Handling: Detects weak or insecure handling of JSON Web Tokens.
- Dependency Vulnerability Scan: Checks
pubspec.yamlfor known weak packages.
- π Security Score from 0β100 with letter grade (A+ β F)
- π₯ Colored terminal report with per-vulnerability fix recommendations and secure code examples
- π Plain-text & JSON report export for CI/CD pipeline integration (
--output/--json) - βοΈ CI/CD ready β exits with code
2when project is NOT SECURE - π§© Extensible β add custom detectors by extending
BaseDetector
Getting Started #
As a CLI Tool #
Add to your pubspec.yaml dev dependencies:
dev_dependencies:
secure_code_scanner: ^1.0.2
- First, add the package to your project: Open your terminal inside the root directory of your app and add the package as a development tool:
flutter pub add dev:secure_code_scanner
Then run: 2. Second, run the scan: While you are still in the same project directory, run this simple command:
dart pub get
dart run secure_code_scanner scan
If you are outside the project directory and want to scan a different project, you can pass the folder path using the --path flag like this:
dart run secure_code_scanner scan --path C:\Projects\MyFlutterApp
This accurately triggers the CI/CD pipeline to fail automatically until the security problems are resolved.
To get a plain text report (without terminal colors, making it easily readable in server logs):
dart run secure_code_scanner scan --plain
To list help options and check available flags anytime, use this command:
dart run secure_code_scanner --help
As a Dart Package #
import 'package:secure_code_scanner/secure_code_scanner.dart';
void main() async {
final service = ScanService();
final report = await service.scan('./my_flutter_project');
print('Score : ${report.securityScore}/100');
print('Grade : ${service.gradeForScore(report.securityScore)}');
print('Status: ${report.projectStatus.label}');
print('Issues: ${report.totalVulnerabilities}');
}
Usage #
CLI Commands #
# Scan current directory
dart run secure_code_scanner scan
# Scan a specific project path
dart run secure_code_scanner scan --path /path/to/my_flutter_app
# Output plain text (great for CI console logs)
dart run secure_code_scanner scan --plain
# Export report to plain-text file
dart run secure_code_scanner scan --output report.txt
# Export report to JSON format (great for automated dashboards)
dart run secure_code_scanner scan --json report.json
# Show version
dart run secure_code_scanner --version
# Show help
dart run secure_code_scanner --help
Programmatic API #
import 'package:secure_code_scanner/secure_code_scanner.dart';
void main() async {
final service = ScanService();
// Full scan + colored terminal report
final report = await service.scanAndPrint('./my_project');
// Scan + get plain text report as String
final text = await service.scanToString('./my_project');
// Scan only β inspect results programmatically
final report2 = await service.scan('./my_project');
for (final vuln in report2.vulnerabilities) {
print('[${vuln.riskLevel.label}] ${vuln.type} @ ${vuln.filePath}:${vuln.lineNumber}');
print('Fix: ${vuln.recommendation}');
}
// Export plain-text report to file
await service.scanAndExport('./my_project', outputPath: 'report.txt');
// Export JSON report to file
await service.scanAndExportJson('./my_project', outputPath: 'report.json');
// Group issues by file
report2.byFile.forEach((file, issues) {
print('$file β ${issues.length} issue(s)');
});
}
Using Individual Detectors #
import 'package:secure_code_scanner/secure_code_scanner.dart';
void main() {
final detector = ApiKeyDetector();
final code = '''
const apiKey = "sk-1234567890abcdef";
''';
final issues = detector.detect('lib/api/service.dart', code);
for (final issue in issues) {
print(issue); // [HIGH] Hardcoded API Key @ lib/api/service.dart:2
}
}
Writing a Custom Detector #
import 'package:secure_code_scanner/secure_code_scanner.dart';
class InsecureRandomDetector extends BaseDetector {
@override
String get name => 'InsecureRandomDetector';
static final _pattern = RegExp(r'\bRandom\(\)');
@override
List<Vulnerability> detect(String filePath, String content) {
return scanLines(content, (line) => _pattern.hasMatch(line))
.map((m) => Vulnerability(
type: 'Insecure Random Number Generator',
riskLevel: RiskLevel.medium,
filePath: filePath,
lineNumber: m.lineNumber,
lineContent: m.lineContent,
description: 'dart:math Random() is not cryptographically secure.',
whyInsecure: 'Random() is predictable and must not be used for security-sensitive values.',
recommendation: 'Use dart:math Random.secure() for cryptographic purposes.',
secureCodeExample: 'final random = Random.secure();',
))
.toList();
}
}
Sample Report Output #
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π SECURE CODE SCANNER β SAST REPORT v2.0 β
β Flutter / Dart Static Application Security Test β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Project: /Users/dev/my_flutter_app
π Files Scanned: 47
π Vulnerabilities Found: 3
β± Scan Duration: 312ms
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π‘οΈ DETECTOR COVERAGE (15 modules active):
β Hardcoded API Keys β Missing Encryption
β Hardcoded Passwords β Input Validation
...
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
VULNERABILITIES DETECTED:
1. π [CRITICAL] Hardcoded Password
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π File: lib/api/auth_service.dart
π Line: 14
π» Code: String password = "admin123";
π Issue: A password is hardcoded in source code.
β Why Dangerous: Anyone with source access can extract it.
π‘ Fix: Use flutter_secure_storage for credentials.
β
Secure Code Example:
β final storage = const FlutterSecureStorage();
β await storage.write(key: 'password', value: password);
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Security Score: 65 / 100 (C β Fair)
[ββββββββββββββββββββ]
π· Project Status: MODERATE β οΈ
π Breakdown by Severity:
π CRITICAL 1 issue
π΄ HIGH 1 issue
π MEDIUM 1 issue
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β‘ Scan complete. Address all CRITICAL and HIGH issues first.
π Re-run after fixes: dart run secure_code_scanner scan
π Export report: dart run secure_code_scanner scan --output report.txt
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Vulnerability Detectors #
| Detector | Risk Level | What It Finds |
|---|---|---|
ApiKeyDetector |
π΄ HIGH | Hardcoded API keys, access keys |
PasswordDetector |
π CRITICAL | Hardcoded passwords, secrets, tokens |
EncryptionDetector |
π CRITICAL | Plaintext storage of sensitive data |
SslPinningDetector |
π CRITICAL | Missing SSL pinning / badCertificateCallback |
SqlInjectionDetector |
π CRITICAL | SQL injection in raw SQLite queries |
DependencyDetector |
π΄ HIGH | Known insecure package versions (pubspec) |
FirebaseDetector |
π CRITICAL | Insecure Firebase rules (allow read: true) |
JwtDetector |
π΄ HIGH | JWT stored insecurely or missing expiry check |
WebviewDetector |
π΄ HIGH | WebView lacking JS or Navigation constraints |
HttpDetector |
π΄ HIGH | Insecure http:// URLs usage |
InputValidationDetector |
π MEDIUM | Missing input validation on text fields |
| SensitiveLogDetector | π MEDIUM | print/debugPrint leaking tokens/passwords |
| SharedPrefsDetector | π΄ HIGH | Sensitive data in SharedPreferences |
| FileStorageDetector | π΄ HIGH | Sensitive data written to local files |
| ManifestPermissionDetector | CRITICALβLOW | Dangerous Android permissions |
Security Score #
| Score | Grade | Status |
|---|---|---|
| 90β100 | A+ | Excellent |
| 80β89 | A | Secure β |
| 70β79 | B | Good |
| 60β69 | C | Fair |
| 50β59 | D | Needs Improvement |
| 30β49 | E | Poor β οΈ |
| 0β29 | F | Critical Risk β |
CI/CD Integration #
The tool exits with code 2 when the project status is NOT SECURE, making it
easy to fail pipelines on security issues:
# GitHub Actions example
- name: Security Scan
run: dart run secure_code_scanner scan --plain
# Exits 0 = SECURE, 2 = NOT SECURE (fails the pipeline)
Project Structure #
lib/
βββ secure_code_scanner.dart # Public API entry point
βββ src/
βββ models/
β βββ risk_level.dart # RiskLevel enum (low/medium/high/critical)
β βββ vulnerability.dart # Vulnerability model
β βββ scan_report.dart # ScanReport + ProjectStatus
βββ core/
β βββ file_scanner.dart # Recursive file/config discovery (`ProjectFiles`)
β βββ vulnerability_detector.dart # Orchestrates all 15 detectors
β βββ report_generator.dart # Terminal, Text, and JSON report generation
β βββ risk_calculator.dart # Score, grade, severity breakdown
βββ detectors/
β βββ base_detector.dart # Abstract base logic and line scanning
β βββ api_key_detector.dart
β βββ sql_injection_detector.dart
β βββ ssl_pinning_detector.dart
β βββ firebase_detector.dart
β βββ ... # (15 detector modules in total)
βββ services/
βββ scan_service.dart # High-level scan & export pipeline
bin/
βββ secure_code_scanner.dart # CLI entry point
Additional Information #
- Issues & feedback: Open an issue on the repository.
- Contributing: New detectors can be added by extending
BaseDetectorβ PRs are welcome. - Roadmap: AI-powered analysis, PDF reports, VS Code extension, OWASP Top 10 detection.
- License: MIT