What’s an API?
Application Programming Interface (API) is a piece of software that can be used by another piece of software, in order to allow applications to talk to each other.
What is REST Achitecture?
REpresentational State Transfer is a way of building web API’s in a logical way, making them easy to consume.
What are the meanings for HTML status codes, that we sent when setting up API responses?
What is the difference between application and business logic?
One of the big goals of MVC pattern is to separate business logic from the application logic.
Application logic:
Business logic:
How can we make a distinction when it comes to error types?
How JSON Web Token (JWT) Authentication works?
JWT are a stateless solution for authentication, there is no need to store any session state on the server.
When a user logs in into the app
1. Users client, through the app makes a post request
POST /login {email, password}
2. The server creates an unique JWT (using a secret string) if user && password match
3. The server sends the JWT back to the client
4. The client will store the JWT either in cookie or in localStorage.
5. The user is authenticated and logged into our application, without leaving any state on the server.
6. Each time the user requests some protected route, the server will check if the JWT is valid, if it is - it will allow access.
In fact, the server does not know which users are logged in into the system.
How is JWT created?
JWT consists of
HEADER
PAYLOAD
and SECRET
The whole process is called signing. Because of the Secret that is stored on the server, ew are ensured that the token is safe from being altered by a 3rd party.
What is the standard of sending a JWT token in http headers?
The standard is to set
Authorization: ‘Bearer JSON_WEB_TOKEN_LONG_STRING’
What are the steps to protect a route by verifying JWT web tokens?
What are the known email services that can be used in order to send emails in your production apps?
Sendgrid,
mailgun
What are the security best practices to defend from different type of attacks?
DataBase:
- strongly encrypt password with salt and hash (bcrypt)
- strongly encrypt password reset tokens (SHA256 algorithm available in crypto module)
Brute force attacks:
- implement rate limiting (express-rate-limit)
- implement maximum login attempts
- use bcrypt (to make login requests slow)
CROSS-SITE SCRIPTING (XSS) Attacks (the attacker tries to inject their scripts in our app to run their malicious code)
- Store JWT in HTTPOnly cookie (NEVER store JWT in localStorage) (HTTPOnly cookie makes it that the browser can only receive or send the cookie, but cannot modify the cookie in any way).
- Sanitize user input data ()
- Set special HTTP headers (helmet package)
DENIAL OF SERVICE DOS Attack
- implement rate limiting
- limit body payload (in body parser)
- avoid evil regular expressions
NOSQL QUERY INJECTION
- use mongoose for mongodb (because of schema types)
- sanitize user input data
Other best known security practices and suggestions
What is a cookie?
Cookie is a small piece of text that a server can send to clients, when a client receives the cookie, it will automatically store it and automatically send it along with all future requests to that server.
What is data modelling?
Data modelling is a process of taking unstructured data generated by the real world scenario and modelling it into structured model that is put in the database. We do that according to a set of criteria.
Why is it important to include in the mail options, text version of the file along with the HTML?
We want to attach text version of the email into the email, because it’s better for email delivery rates and spam filters.
To do this in express, we need a package called: html-to-text
In the mail options, we should pass:
const mailOptions = {
from: this.from,
to: this.to,
subject,
html,
text: htmlToText.fromString(html)
};