Skip to content

Commit 486752e

Browse files
authored
Merge pull request #98 from SOURAV-ROY/fix/prevent-public-registration-privilege-escalation-12496333661946796975
🛡️ Sentinel: Fix privilege escalation during public registration
2 parents f8450c3 + 1f58362 commit 486752e

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)