-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: the post /reset endpoint in the real-time servi... in git_routes.ts #41898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release
Are you sure you want to change the base?
Changes from all commits
23011a8
07ef29e
254ee2f
fd6f47b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { Request, Response, NextFunction } from "express"; | ||
|
|
||
| export function requireInternalAuth(req: Request, res: Response, next: NextFunction) { | ||
| const secret = process.env.APPSMITH_RTS_SECRET; | ||
| if (!secret) { | ||
| console.warn("[AuthMiddleware] APPSMITH_RTS_SECRET is not set; rejecting request to protected endpoint"); | ||
| return res.status(401).json({ success: false, message: "Unauthorized" }); | ||
| } | ||
| const header = req.headers["x-rts-secret"]; | ||
| if (!header || header !== secret) { | ||
| return res.status(401).json({ success: false, message: "Unauthorized" }); | ||
| } | ||
| next(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,19 @@ | ||
| import express from "express"; | ||
| import { body } from "express-validator"; | ||
| import GitController from "@controllers/git"; | ||
| import { Validator } from "@middlewares/Validator"; | ||
| import { requireInternalAuth } from "@middlewares/AuthMiddleware"; | ||
|
|
||
| const router = express.Router(); | ||
| const gitController = new GitController(); | ||
| const validator = new Validator(); | ||
|
|
||
| router.post("/reset", validator.validateRequest, gitController.reset); | ||
| router.post( | ||
| "/reset", | ||
| body("repoPath").isString().notEmpty().withMessage("repoPath is required and must be a string"), | ||
| requireInternalAuth, | ||
| validator.validateRequest, | ||
| gitController.reset, | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| export default router; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import request from 'supertest'; | ||
| import express from 'express'; | ||
| import gitRoutes from '../../../src/routes/git_routes'; | ||
|
|
||
| describe('Protected endpoints reject unauthenticated requests', () => { | ||
| let app: express.Application; | ||
| let originalSecret: string | undefined; | ||
|
|
||
| beforeAll(() => { | ||
| originalSecret = process.env.APPSMITH_RTS_SECRET; | ||
| process.env.APPSMITH_RTS_SECRET = 'test-secret'; | ||
| app = express(); | ||
| app.use(express.json()); | ||
| app.use('/git', gitRoutes); | ||
| }); | ||
|
Comment on lines
+9
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win Add a regression case for an unset Line 11 forces the secret for every case, so this suite never exercises the new fail-closed branch that should return 🧰 Tools🪛 ast-grep (0.44.0)[warning] 11-11: Express application should use Helmet (missing-helmet-typescript) 🤖 Prompt for AI Agents |
||
|
|
||
| afterAll(() => { | ||
| if (originalSecret === undefined) { | ||
| delete process.env.APPSMITH_RTS_SECRET; | ||
| } else { | ||
| process.env.APPSMITH_RTS_SECRET = originalSecret; | ||
| } | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const payloads = [ | ||
| { name: 'missing_auth_header', headers: {}, expectedStatus: 401 }, | ||
| { name: 'invalid_token', headers: { 'x-rts-secret': 'invalid_token_xyz' }, expectedStatus: 401 }, | ||
| { name: 'expired_token', headers: { 'x-rts-secret': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDAwMDAwMDB9.invalid' }, expectedStatus: 401 }, | ||
| { name: 'malformed_auth', headers: { 'x-rts-secret': 'InvalidScheme token123' }, expectedStatus: 401 }, | ||
| { name: 'empty_token', headers: { 'x-rts-secret': '' }, expectedStatus: 401 }, | ||
| ]; | ||
|
|
||
| test.each(payloads)( | ||
| 'POST /reset rejects unauthenticated request: $name', | ||
| async ({ headers, expectedStatus }) => { | ||
| const response = await request(app) | ||
| .post('/git/reset') | ||
| .set(headers) | ||
| .send({ repoPath: 'test-repo' }); | ||
|
|
||
| expect(response.status).toBe(expectedStatus); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.