Skip to main content

Security Best Practices for Napacitor

Every Napacitor developer is responsible for making sure their app is following security best practices. Without proper care, major security issues can crop up which can prove extremely damaging and expensive.

Security is a wide topic, but there are a number of areas that Napacitor developers should audit for security compliance, including Data, Authentication/Deep Linking, Network, and Web View security.

Data Security​

Data security involves data stored locally as well as data in the app code.

Avoid Embedding Secrets in Your Code​

One of the most important security tips for Napacitor apps, and any frontend app for that matter, is to never embed secrets in your app code. That means make sure your code never contains secret API keys, encryption keys, or any other sensitive data that could be easily stolen using basic app analysis techniques. Watch for environment variable plugins that could be injecting sensitive values into your app code at build time.

Instead, move most operations that require keys or tokens to the server side, where they can be protected and requests can be forwarded from the server. This can be a serverless function or a traditional server-side application process.

For apps that must store sensitive keys or tokens on the client (such as authentication tokens or encryption keys), the recommended practice is to handle the value in memory only (i.e. never write it to disk), or use the secure keychain/keystore techniques described below.

Storing Encryption Keys, Session Tokens, etc.​

Modern mobile devices and operating systems provide powerful security APIs and dedicated security hardware for storing sensitive values on device. This is how apps provide biometric or secure passcode authentication while managing highly sensitive values such as encryption keys or session tokens.

The APIs that provide this functionality are the iOS Keychain Services and Android Keystore APIs. These APIs are complex and low-level, so you will likely want to find a plugin that uses them for you. The @napacitor/security plugin wraps the .NET MAUI SecureStorage API in C# — iOS/Mac Catalyst uses the Keychain, Android uses EncryptedSharedPreferences with keys protected by the Android Keystore, and Windows uses DataProtectionProvider — exposing secure key/value reads and writes to web code. One plugin covers all MAUI platforms; for special requirements you can also write a custom Napacitor plugin.

Authentication and Deep Linking​

Authentication flows in native apps require extra care, since authentication often happens through the use of Custom URL Schemes. Custom URL Schemes, such as instagram://, are not globally controlled like web domains are, so it's possible that a malicious app could intercept a request meant for one app by defining and overriding a custom URL scheme. Imagine sending a secure token to the wrong app!

Generally, an app should never send sensitive data through a Custom URL scheme deep link (newer techniques such as Universal Links are more secure as they rely on real web domain ownership).

This is especially important for oAuth2 flows, where the last step in the authentication experience relies on a deep link back to the app. To mitigate the possibility of a malicious app receiving a token, PKCE must be used for oAuth2 in Napacitor apps.

For more information, see this excellent oAuth2 Best Practices for Native Apps guide.

Network Security​

Network security ensures that network requests go to trusted endpoints and are encrypted, avoiding sending sensitive data (such as passwords) in plain text.

SSL​

Apps should only make requests to endpoints with SSL enabled. This means never requesting http:// endpoints and always using https:// instead. This ensures data is never sent in plain text.

Web View Security​

Content Security Policy​

Content Security Policy (CSP) is a set of security features available in the browser (and, thus, your Napacitor Web View). CSP can be used to limit the resources the user agent is allowed to load in the Web View (such as images, XHR, videos, Web Sockets, etc).

CSP can be configured in your Napacitor app by adding a meta tag to the <head> with an acceptable CSP format (CSP can be configured both server and client side using the same format). For example, this configuration would allow all requests to the current origin and foo.com:

<meta
http-equiv="Content-Security-Policy"
content="default-src 'self' foo.com"
/>

CSP supports a wide variety of configurations, and the CSP Reference is required reading. Another helpful resource is content-security-policy.com.

JavaScript Security Tips​

Since the bulk of a Napacitor app is a web app using JavaScript, typical JS security techniques apply.

JS security is beyond the scope of this document, and there are many resources available on JS and web app security tips. Here is a good one to get started.