How to fix Command Injection
in Dart Frog
Executive Summary
Command injection in Dart Frog occurs when untrusted request data is passed directly to system processes via 'Process.run' or 'Process.start'. Hackers exploit this by injecting shell metacharacters like ';', '&', or '|' to execute arbitrary code on the host OS. If you're using 'runInShell: true' or manual string concatenation for commands, your backend is a pivot point for RCE.
The Vulnerable Pattern
import 'dart:io'; import 'package:dart_frog/dart_frog.dart';Future
onRequest(RequestContext context) async { final params = context.request.uri.queryParameters; final filename = params[‘file’] ?? ‘default.txt’; // VULNERABLE: Using runInShell with string interpolation allows command chaining final result = await Process.run( ‘sh’, [‘-c’, ‘ls -la $filename’], runInShell: true, );
return Response(body: result.stdout.toString()); }
The Secure Implementation
The vulnerability stems from invoking a subshell ('sh -c') which interprets shell-specific characters. In the secure version, we invoke the 'ls' binary directly and pass 'filename' as a separate argument. By setting 'runInShell: false' (the default), the operating system handles the arguments as a literal array, making it impossible for an attacker to break out of the intended command using characters like '; rm -rf /'. Always prefer direct binary execution over shell scripts and implement strict input whitelisting for extra defense-in-depth.
import 'dart:io'; import 'package:dart_frog/dart_frog.dart';Future
onRequest(RequestContext context) async { final params = context.request.uri.queryParameters; final filename = params[‘file’] ?? ‘default.txt’; // SECURE: Avoid the shell. Pass arguments as a discrete list. // This ensures the OS treats the input as a literal string, not a command. final result = await Process.run( ‘ls’, [‘-la’, filename], runInShell: false, );
return Response(body: result.stdout.toString()); }
Your Dart Frog API
might be exposed to Command Injection
74% of Dart Frog apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.