Missing Tls in Adonisjs with Mongodb
Missing Tls in Adonisjs with Mongodb — how this specific combination creates or exposes the vulnerability
When an AdonisJS application connects to a MongoDB backend without Transport Layer Security (TLS), credentials, session tokens, and query data traverse the network in cleartext. This specific combination—AdonisJS as the web framework and MongoDB as the database—commonly occurs in internal services, containerized environments, or development setups where TLS is assumed unnecessary inside the network perimeter.
Without TLS, connections between the AdonisJS server and the MongoDB instance are vulnerable to network-level interception and tampering. Attackers on the same network segment can perform passive sniffing to recover database credentials, authentication tokens, or sensitive payloads. They can also conduct active tampering, altering queries or responses in transit. Many MongoDB deployments bind to localhost or internal IPs and disable TLS to simplify configuration, which inadvertently exposes these channels when accessed through AdonisJS routes or background jobs.
The risk is compounded when AdonisJS applications run in orchestrated environments where service discovery routes traffic across pods or containers without enforced encryption. Even if MongoDB is configured to accept TLS, an AdonisJS client that does not explicitly require and validate certificates may fall back to an unencrypted connection, creating a weak link. This misconfiguration maps directly to the OWASP API Top 10 category 'Broken Object Level Authorization' and related data exposure risks, as sensitive information can be harvested from unencrypted database traffic.
Tools like middleBrick detect Missing TLS by scanning the API surface and inspecting service-to-service communication patterns. It flags scenarios where AdonisJS endpoints interact with MongoDB without enforced encryption and highlights the absence of certificate validation, providing remediation guidance tied to real-world attack vectors rather than theoretical weaknesses.
Mongodb-Specific Remediation in Adonisjs — concrete code fixes
Securing the AdonisJS to MongoDB path requires explicit TLS configuration on the MongoDB client, certificate validation, and secure deployment practices. Below are concrete, working examples for AdonisJS that enforce TLS when connecting to MongoDB.
First, ensure your MongoDB server has a valid TLS certificate issued by a trusted Certificate Authority (CA). Then configure the AdonisJS application to use the MongoDB Node.js driver with strict TLS options.
// start/server.js or a dedicated MongoDB client provider
const { MongoClient } = require('mongodb');
const fs = require('fs');
const tlsOptions = {
tls: true,
tlsCertificateKeyFile: '/path/to/client.key',
tlsCertificateFile: '/path/to/client.crt',
tlsCAFile: '/path/to/ca.pem',
tlsAllowInvalidCertificates: false,
tlsAllowInvalidHostnames: false
};
const uri = 'mongodb+srv://user:password@cluster.example.com/dbname?retryWrites=true&w=majority';
const client = new MongoClient(uri, tlsOptions);
async function connectToMongo() {
try {
await client.connect();
console.log('Connected to MongoDB with TLS');
const db = client.db('dbname');
const users = await db.collection('users').find({}).toArray();
console.log(users);
} catch (err) {
console.error('MongoDB TLS connection failed:', err);
process.exit(1);
} finally {
await client.close();
}
}
connectToMongo();
For AdonisJS applications using environment variables, load the certificate paths securely and avoid hardcoding sensitive values. Use AdonisJS provider bindings to manage the MongoDB client lifecycle:
// app/Providers/MongoDBProvider.js
const { MongoClient } = require('mongodb');
const Env = use('Env');
class MongoDBProvider {
constructor() {
this.client = null;
}
async boot() {
const tlsConfig = {
tls: true,
tlsCertificateKeyFile: Env.get('MONGO_TLS_KEY_PATH'),
tlsCertificateFile: Env.get('MONGO_TLS_CERT_PATH'),
tlsCAFile: Env.get('MONGO_TLS_CA_PATH'),
tlsAllowInvalidCertificates: false,
tlsAllowInvalidHostnames: false
};
this.client = new MongoClient(Env.get('MONGO_URI'), tlsConfig);
await this.client.connect();
}
get database() {
return this.client.db(Env.get('MONGO_DB_NAME'));
}
}
module.exports = MongoDBProvider;
In production, enforce network-level controls alongside TLS. Use VPC peering, private endpoints, or service meshes to restrict exposure, and rotate certificates regularly. middleBrick’s scans can verify that TLS is enforced and that certificate validation is strict, reducing the attack surface for data interception in this AdonisJS and MongoDB workflow.
Related CWEs: encryption
| CWE ID | Name | Severity |
|---|---|---|
| CWE-319 | Cleartext Transmission of Sensitive Information | HIGH |
| CWE-295 | Improper Certificate Validation | HIGH |
| CWE-326 | Inadequate Encryption Strength | HIGH |
| CWE-327 | Use of a Broken or Risky Cryptographic Algorithm | HIGH |
| CWE-328 | Use of Weak Hash | HIGH |
| CWE-330 | Use of Insufficiently Random Values | HIGH |
| CWE-338 | Use of Cryptographically Weak PRNG | MEDIUM |
| CWE-693 | Protection Mechanism Failure | MEDIUM |
| CWE-757 | Selection of Less-Secure Algorithm During Negotiation | HIGH |
| CWE-261 | Weak Encoding for Password | HIGH |