dipyamanroy / SimpleLMS

A Learning Management System that just works.
https://simplelms.vercel.app
5 stars 3 forks source link

Exposed course image route #1

Closed dipyamanroy closed 3 months ago

dipyamanroy commented 4 months ago

Expected Behavior

Route to upload course image, http://localhost:3000/api/uploadthing?slug=courseImage must be protected by Clerk authentication.

Current Behavior

The route is currently exposed and accessible without authentication, potentially exposing vulnerabilities.

Possible Solution

Modify middleware.ts to implement Clerk authentication middleware for the uploadthing route with slug=courseImage.

Steps to Reproduce

  1. Use Postman or any HTTP client to send a POST request to http://localhost:3000/api/uploadthing?slug=courseImage.
  2. Notice that the route is accessible without requiring authentication.

Context (Environment)

This issue affects the security of the upload functionality in this application. By securing this route, we aim to protect sensitive data and prevent unauthorized access to course image uploads.

Detailed Description

To address this issue, we need to implement Clerk authentication in our Node.js application. Clerk will provide the necessary authentication mechanism to verify and secure requests to the uploadthing API route with slug=courseImage.

Possible Implementation

  1. Set Up Clerk Authentication: Configure Clerk authentication in your project. Ensure you have the necessary API keys or credentials from Clerk.

  2. Modify Middleware: Update your middleware.ts file to include a middleware function (authenticateClerk) that verifies Clerk authentication tokens.

    
    // middleware.ts
    
    import { Request, Response, NextFunction } from 'express';
    import { verifyToken } from './clerkAuth'; // Implement based on Clerk authentication
    
    export const authenticateClerk = async (req: Request, res: Response, next: NextFunction) => {
     try {
       const token = req.headers.authorization?.split(' ')[1]; // Assuming token is in 'Bearer <token>' format
       if (!token) {
         return res.status(401).json({ message: 'Authorization token not found' });
       }
    
       // Verify token using Clerk or your authentication mechanism
       const user = await verifyToken(token); // Implement this function based on Clerk's verification process
    
       if (!user) {
         return res.status(401).json({ message: 'Unauthorized' });
       }
    
       // If token is valid, proceed to the next middleware or route handler
       req.user = user; // Optionally, attach the authenticated user to the request object
       next();
     } catch (error) {
       console.error('Authentication error:', error);
       return res.status(500).json({ message: 'Internal Server Error' });
     }
    };```
  3. Secure uploadthing Route: Apply the authenticateClerk middleware to the POST request handler for the uploadthing route with slug=courseImage.

    
    // routes.ts or wherever your routes are defined
    
    import { Router } from 'express';
    import { authenticateClerk } from './middleware';
    import { uploadCourseImage } from './controllers/uploadthingController'; // Replace with your controller function
    
    const router = Router();
    
    router.post('/api/uploadthing', authenticateClerk, async (req, res) => {
      // Handle courseImage upload logic here
       try {
         const slug = req.query.slug as string;
         if (slug === 'courseImage') {
           // Call your controller function for handling course image upload
          const result = await uploadCourseImage(req); // Implement this function in your controller
          res.status(200).json(result);
         } else {
          res.status(404).json({ message: 'Route not found' });
        }
      } catch (error) {
        console.error('Error uploading course image:', error);
        res.status(500).json({ message: 'Internal Server Error' });
      }
    });
    
    export default router;```
  4. Testing: Verify the implementation by testing the uploadthing route using Postman or similar tools. Ensure that requests without a valid Clerk authentication token are rejected.

dipyamanroy commented 4 months ago

Quite a common issue: https://docs.uploadthing.com/faq

dipyamanroy commented 3 months ago

Fixed in commit d57b794e3a6139413ef0dc9428428ff78bd50ab8.