Security Things That Matter

Security is dull & boring until you get hacked. Then it's REALLY interesting. Node is great at making it easy to create APIs overnight, but that also makes it easy to do it wrong.

I have seen others get compromised because of:

I am sure you have wondered what you'd do if someone hacked your app. Let’s look at some suggestions to avoid getting hacked.

The Basics

I found this out for myself when someone crashed my application by placing an emoji within the username field. Fun times.

Auth has two halves: ensuring that the user is who they say they are (authentication) and ensuring that they can do what they're trying to do (authorization).

And DO NOT forget to place your .env file to .gitignore.

Real Security Problems I've Seen

When someone attempts ?name=x'; DROP TABLE users; -- you will be happy you utilized parameters.

const rateLimit = require('express-rate-limit');

// Basic protection for all routes
const limiter = rateLimit({
  windowMs: 15 * 60 * 1000,  // 15 minutes
  max: 100  // limit per IP
});

app.use(limiter);

// Extra protection for login attempts
const loginLimiter = rateLimit({
  windowMs: 60 * 60 * 1000,  // 1 hour
  max: 5  // 5 login attempts per hour
});

app.use('/login', loginLimiter);

Use Helmet for HTTP Headers

One line of code that fixes several issues:

const helmet = require('helmet');
app.use(helmet());  // Adds security headers

FAQs that every Developer should know

Q: What do you need to fix first that is most important?

A: Input validation. Most attacks start there.

Q: What is the best way to know if my API security is sufficient?

A: Have someone attempt to break it. Or you can try a tool like OWASP ZAP.

Q: How can I prevent security vulnerabilities caused by dependencies?
A: Schedule a calendar event to update your dependencies in a timely manner. To detect these vulnerabilities, consider using tools like npm audit, snyk, or dependable.

Q: What is the best way to know if my API security is sufficient?

A: Have someone attempt to break it. Or you can try a tool like OWASP ZAP.

Final Thoughts - Lessons to learn from

Security is not an afterthought - it should be built into the code from the outset.

Start with these basics:

So, which gap will you be closing today?