Skip to content

Commit 1f58362

Browse files
fix(security): prevent privilege escalation during public user registration
- Ignore user-supplied role in req.body on POST /api/v1/auth/register to enforce default user role - Add unit test verifying that self-assigned roles during public registration are ignored Co-authored-by: SOURAV-ROY <8663561+SOURAV-ROY@users.noreply.github.com>
1 parent 4acc72a commit 1f58362

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

controllers/authController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const config = require("../config/config.json");
88
// @route POST /api/v1/auth/register
99
// @access Public
1010
exports.register = asyncHandler(async (req, res, next) => {
11-
const { name, email, password, role } = req.body;
11+
const { name, email, password } = req.body;
1212

1313
// Create user ************************************************
14+
// Public registration strictly defaults role to "user" to prevent privilege escalation
1415
const user = await User.create({
1516
name,
1617
email,
1718
password,
18-
role,
1919
});
2020

2121
// Create token ***********************************************

tests/auth.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,34 @@ describe("Auth Routes", () => {
2626
await connectDB();
2727
});
2828

29+
const publisherEmail = `testpublisher_${Date.now()}@example.com`;
30+
2931
afterAll(async () => {
3032
// Cleanup
3133
await User.deleteOne({ email: testUser.email });
34+
await User.deleteOne({ email: publisherEmail });
3235
await mongoose.connection.close();
3336
});
3437

38+
it("should ignore requested role on public registration and default to 'user'", async () => {
39+
await getCsrfToken();
40+
const res = await request(app)
41+
.post("/api/v1/auth/register")
42+
.set("x-csrf-token", csrfToken)
43+
.set("Cookie", cookies)
44+
.send({
45+
name: "Test Publisher",
46+
email: publisherEmail,
47+
password: "password123",
48+
role: "publisher",
49+
});
50+
expect(res.statusCode).toEqual(200);
51+
52+
const createdUser = await User.findOne({ email: publisherEmail });
53+
expect(createdUser).not.toBeNull();
54+
expect(createdUser.role).toEqual("user");
55+
});
56+
3557
it("should register a new user", async () => {
3658
await getCsrfToken();
3759
const res = await request(app)

0 commit comments

Comments
 (0)