Strengthening Flutter application security should be a top priority for every developer. Let’s be honest—no one wants their app making headlines due to a security breach. As mobile developers, we spend a significant amount of time perfecting UI/UX, optimizing performance, and ensuring seamless user flows. But what about security?
With the ever-increasing range of cyber threats, a poorly secured Flutter app is not only a problem, it is an open call for data breaches, credential theft, and unwanted financial repercussions. Whether you are working on an eCommerce platform, a fintech mobile application, or even a simple social networking application, securing your users’ information is equally important as making your application aesthetically pleasing and functional.
This article outlines practical, developer-friendly security practices to help make Flutter apps more secure and less vulnerable to attacks. We’ll cover from API shielding to sensitive data encryption, ensuring your application remains reliable, safe, and trusted.
Table of Contents
Encrypt locally stored sensitive data
Prevent attacker collect sensitive data such as username/email, password, access token, etc
Implementation
Create local database with Sembast and implement SembastCodec
SembastCodec
Example: https://github.com/tekartik/sembast.dart/blob/master/sembast_test/lib/encrypt_codec.dart
Database Creation Example:
Future<Database> createDatabase(String filename) async {
final appDocDir = await getApplicationDocumentsDirectory();
final codec = getEncryptSembastCodec(password: 'your-password-key');
return databaseFactoryIo.openDatabase('${appDocDir.path}/$filename',codec: codec);
}
Reference
Set allowBackup to false in AndroidManisfest.xml
Prevent attacker to backup APK and application data by using Android Debugger Bridge (ADB)
Implementation
in AndroidManifest.xml set
<application android:allowBackup="false">
Reference
- https://mas.owasp.org/MASTG/tests/android/MASVS-STORAGE/MASTG-TEST-0009/
- https://medium.com/@lucideus/security-review-of-android-manifest-file-part-i-ecb5ca51eb6
Set debuggable to false in AndroidManisfest.xml
Prevent attacker to do reverse engineering to see stack trace and access debugging helper classes
Implementation
in AndroidManifest.xml set
<application android:debuggable="false">
Reference
- https://mas.owasp.org/MASTG/tests/android/MASVS-RESILIENCE/MASTG-TEST-0046
- https://mas.owasp.org/MASTG/tests/android/MASVS-RESILIENCE/MASTG-TEST-0039
Jail break, root, and emulator detection
Prevent users install the app in unsafe device
Implementation
use https://pub.dev/packages/safe_device and check if the device is safe
//detect if jail broken
final bool isJailBroken = await SafeDevice.isJailBroken;
//detect if real device or emulator
final bool isRealDevice = await SafeDevice.isRealDevice;
//check all of the above
final bool isSafeDevice = await SafeDevice.isSafeDevice;
Reference
- https://github.com/OWASP/owasp-mstg/blob/master/Document/0x06j-Testing-Resiliency-Against-Reverse-Engineering.md
- https://www.trustwave.com/en-us/resources/blogs/spiderlabs-blog/jailbreak-detection-methods
- https://blog.omni-bridge.com/5-ways-to-secure-your-Android-application-769c8c73dafb
- https://mas.owasp.org/MASVS/controls/MASVS-RESILIENCE-4
- https://mas.owasp.org/MASTG/tests/android/MASVS-RESILIENCE/MASTG-TEST-0049
Implement SSL Pinning
Prevent man-in-the-middle attack which can intercept any network traffic in the app
Implementation
Save SSL Fingerprint string from SSL certificate provider and validate in dio interceptor
final sslFingerprint = dotenv.env['sslFingerprint'];
...
...
dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
final HttpClient client =
HttpClient(context: SecurityContext(withTrustedRoots: false));
client.badCertificateCallback = (cert, host, port) => true;
return client;
},
validateCertificate: (cert, host, port) {
if (cert == null) {
return false;
}
return sslFingerprint == sha256.convert(cert.der).toString();
},
);
Pro Tip: Please note that SSL Fingerprint string may change in certain period. Better save it in dynamic place such as Firebase Remote Config
Reference
- https://blog.omni-bridge.com/5-ways-to-secure-your-Android-application-769c8c73dafb
- https://github.com/OWASP/owasp-mstg/blob/master/Document/0x06j-Testing-Resiliency-Against-Reverse-Engineering.md
- https://www.trustwave.com/en-us/resources/blogs/spiderlabs-blog/jailbreak-detection-method
Detect & disallow Fake GPS usage
If your apps has a feature which collect user location, add a Fake GPS detector and prevent the user to use it.
Implementation
use https://pub.dev/packages/geolocator package and add condition for user to use Fake GPS
final locationData = await Geolocator.getCurrentPosition();
if (locationData.isMocked) {
//add a warning dialog
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Warning'),
content: const Text('You are not allowed using Fake GPS'),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
} else {
//continue your operation
...
...
}
Final Thoughts: How to Protect Your Flutter Application Accurately
Security is not an afterthought anymore. It starts from encrypting local data all the way to figuring out if GPS data is fake. Everything done towards making your Flutter app more secure needs to be done to lessen the probability of exploitation.
Outdated tools are as good as no tools which is why the perpetually changing technology landscape requires an up to date educator. Some of the measures that can be taken to make an app secure from common vulnerabilities include SSL pinning, blocking unauthorized backups, and reversing engineering prevention.
As developers, we can not compromise security for everything. Making the application secure while making sure that the user experience is not impacted needs to be the goal. Removing security loopholes should enhance rather than constrain the user experience.
Now that you have these security strategies at your disposal, it’s time to apply them to your Flutter projects. Your users trust you with their data—let’s make sure that trust is never broken.
If you’re looking for a secure and reliable mobile app development service, LOGIQUE is here to help. We have extensive experience in developing Flutter apps with top-level security to ensure user data remains protected. Contact us now for more information!