Name | Progress |
---|---|
Introduction | Not Read |
๐ฐ Player | Not Read |
Name | Progress |
---|---|
Software Installation | Not Read |
Project Setup | Not Read |
Running and Testing | Not Read |
React and ReactDOM | Not Read |
๐ก Assignment 1: Welcome Message | Not Attempted |
Submissions | Not Read |
Name | Progress |
---|---|
JSX and Components | Not Read |
Props | Not Read |
๐พ Exercise: Props | Not Attempted |
CSS Styles | Not Read |
useState and Hooks | Not Read |
๐พ Exercise: useState | Not Attempted |
Conditional Rendering | Not Read |
Lists | Not Read |
๐พ Exercise: Lists | Not Attempted |
Forms and Events | Not Read |
๐พ Exercise: Forms | Not Attempted |
๐ก Assignment 2: Front End | Not Attempted |
Name | Progress |
---|---|
Pure Components - memo | Not Read |
Lifecycle - useEffect | Not Read |
Expensive? - useMemo | Not Read |
DOM Interactions - useRef | Not Read |
forwardRef | Not Read |
useImperativeHandle | Not Read |
๐พ useImperativeHandle | Not Attempted |
Context | Not Read |
useCallback | Not Read |
useId | Not Read |
useReducer | Not Read |
Name | Progress |
---|---|
Database | Not Read |
NextAuth and Github Authentication | Not Read |
Prisma and ORM | Not Read |
Project Setup | Not Read |
Project Authentication | Not Read |
Name | Progress |
---|---|
tRPC | Not Read |
tRPC - Routers | Not Read |
tRPC - Server Rendering | Not Read |
tRPC - Client Rendering | Not Read |
Persisting Data | Not Read |
Assignment 3: APIs | Not Read |
APIs (Application Programming Interfaces) are the backbone of modern applications, enabling systems to communicate and exchange data. However, this connectivity also opens up APIs to potential security threats, making it crucial to implement measures to protect both your data and services. This chapter explores common security challenges and practical strategies for securing your APIs.
APIs are exposed to various risks that can compromise sensitive data or disrupt services:
Data Leakage: Sensitive data may be exposed due to improper validation or insufficient access controls.
Unauthorised Access: Attackers can gain access to API endpoints that lack proper authentication and authorisation.
Injection Attacks: Malicious data input, such as SQL injection, can manipulate backend systems.
DDoS (Distributed Denial of Service): High volumes of traffic can overwhelm your API, rendering it unresponsive.
Authentication ensures that only valid users can access the API, while authorisation determines which resources each user can access.
OAuth 2.0: An industry-standard protocol used for authorisation, allowing applications to access user data without exposing passwords.
JWT (JSON Web Token): A compact, self-contained token that verifies user identity and access rights.
API Keys: Simple tokens assigned to clients for identifying valid applications. Should be used with other security measures.
Example: In an Express.js API, you might issue check for a JWT token like this:
const jwt = require('jsonwebtoken');
const SECRET_KEY = 'YOUR_SECRET_KEY';
function issueToken() {
// call this during login to issuse the secure token as a cookie
const token = jwt.sign({ username: 'admin' }, SECRET_KEY, { expiresIn: '1h' });
// Set token as HTTP-only cookie
res.cookie('authToken', token, { httpOnly: true, secure: true });
}
function verifyToken(req, res, next) {
const token = req.cookies.authToken;
if (!token) return res.status(401).json({ message: 'Access denied' });
try {
const verified = jwt.verify(token, SECRET_KEY);
req.user = verified;
next();
} catch (err) {
res.status(400).json({ message: 'Invalid token' });
}
}
HTTPS (Hypertext Transfer Protocol Secure) encrypts data transmitted over the network, protecting against eavesdropping or man-in-the-middle attacks.
TLS (Transport Layer Security): Ensure that your API endpoints are configured with TLS certificates for encrypted connections.
Prevent abuse by limiting the number of requests clients can make in a given period.
Rate Limiting: Enforces a maximum number of requests per client within a timeframe, preventing DDoS attacks.
Throttling: Reduces the rate at which a client can make requests, ensuring fair resource allocation.
Example: Using the express-rate-limit package for rate limiting in an Express.js API:
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // Limit each IP to 100 requests per windowMs
});
app.use('/api', limiter);
Avoid injection attacks by validating and sanitizing incoming data.
Validation: Check that incoming data conforms to the expected structure, type, and format.
Sanitization: Strip out dangerous characters or patterns to prevent SQL injection, XSS (Cross-Site Scripting), and other attacks.
Example: In Node.js, you can use the express-validator package:
const { body, validationResult } = require('express-validator');
app.post('/register', [
body('email').isEmail(),
body('password').isLength({ min: 6 })
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
// Register the user...
});
Monitor API usage to detect anomalies and identify potential threats.
Logging: Record all access attempts, including IP addresses and request paths.
Monitoring: Use analytics tools to monitor traffic and identify unusual patterns.
Securing your API requires a layered approach that encompasses various strategies, including authentication, encryption, rate limiting, and validation. By understanding the risks and implementing appropriate safeguards, you can ensure that your API remains a robust and reliable channel for data exchange. Incorporating security from the start will also protect your applicationโs reputation and help build trust with your users.