Back to all reviewers

Secure Fastify Code Implementation

fastify/fastify
Based on 2 comments
TypeScript

This review focuses on secure implementation patterns when using the Fastify web framework in TypeScript: prevent Prototype Pollution Attacks by avoiding direct property access on untrusted objects, and protect Against Denial-of-Service Attacks by configuring appropriate request timeouts.

Fastify TypeScript

Reviewer Prompt

This review focuses on secure implementation patterns when using the Fastify web framework in TypeScript:

  1. Prevent Prototype Pollution Attacks: Avoid direct property access on untrusted objects. Instead, use Object.prototype methods to safely access object properties:
// Vulnerable approach
fastify.get('/route', (req, reply) => {
  console.log(req.params.hasOwnProperty('name')); // Potential prototype pollution vulnerability
  return { hello: req.params.name };
});

// Secure approach
fastify.get('/route', (req, reply) => {
  console.log(Object.prototype.hasOwnProperty.call(req.params, 'name')); // Safe property access
  return { hello: req.params.name };
}); 
  1. Protect Against Denial-of-Service Attacks: Configure appropriate request timeouts, especially when deploying Fastify without a reverse proxy:
const fastify = Fastify({
  requestTimeout: 120000 // Set a non-zero timeout (e.g., 120 seconds)
});

These security measures help mitigate common web application vulnerabilities when using the Fastify framework.

2
Comments Analyzed
TypeScript
Primary Language
Fastify
Category

Source Discussions