-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add user registration and login #1
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: main
Are you sure you want to change the base?
Changes from all commits
0eadf0d
a849732
cc3d6d1
c9ed37a
71333bd
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,53 @@ | ||||||||||||||
| const express = require('express'); | ||||||||||||||
| const router = express.Router(); | ||||||||||||||
|
|
||||||||||||||
| // In-memory user store | ||||||||||||||
| let users = []; | ||||||||||||||
|
|
||||||||||||||
| // GET /users | ||||||||||||||
| router.get('/', (req, res) => { | ||||||||||||||
|
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. 🩺 [CRITICAL] security: Exposing plaintext passwords via GET /users The Suggestion:
Suggested change
|
||||||||||||||
| res.json(users); // BUG: exposes passwords in response | ||||||||||||||
| }); | ||||||||||||||
|
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. [CRITICAL] security: Exposes sensitive user data (passwords) via GET /users The Suggestion:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| // POST /users - register a new user | ||||||||||||||
| router.post('/', (req, res) => { | ||||||||||||||
| const { username, password, role } = req.body; | ||||||||||||||
|
|
||||||||||||||
|
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. [HIGH] correctness: Inadequate input validation for registration The user registration endpoint does not validate Suggestion:
Suggested change
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. 🩺 [HIGH] correctness: Missing input validation for user registration There is no validation for Suggestion:
Suggested change
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. 🩺 [HIGH] correctness: Missing input validation for user registration The user registration endpoint does not validate Suggestion:
Suggested change
|
||||||||||||||
| // BUG: no validation — username/password can be undefined or empty | ||||||||||||||
|
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. 🩺 [CRITICAL] security: Plaintext password storage Storing user passwords in plaintext is a critical security vulnerability. If the in-memory store (or a database) is compromised, all user passwords will be exposed. This is a fundamental security flaw. Suggestion:
Suggested change
|
||||||||||||||
| // BUG: password stored in plaintext | ||||||||||||||
|
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. [CRITICAL] security: Plaintext password storage and exposure Passwords are being stored in plaintext and are exposed via the Suggestion:
Suggested change
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. [HIGH] security: Missing input validation for registration The registration endpoint lacks validation for Suggestion:
Suggested change
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. 🩺 [HIGH] correctness: User ID generation is not robust and can lead to collisions Generating Suggestion:
Suggested change
|
||||||||||||||
| const user = { | ||||||||||||||
|
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. [HIGH] correctness: Non-unique user ID generation User IDs are generated using Suggestion:
Suggested change
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. 🩺 [CRITICAL] security: Passwords stored in plaintext Storing user passwords in plaintext is a critical security vulnerability. If the in-memory store (or any persistent storage) is compromised, all user passwords will be exposed, leading to potential account takeovers. Suggestion:
Suggested change
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. 🩺 [HIGH] correctness: Non-robust user ID generation Generating user IDs based on Suggestion:
Suggested change
|
||||||||||||||
| id: users.length + 1, // BUG: id collision if users are deleted | ||||||||||||||
|
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. [CRITICAL] security: Passwords stored in plaintext Storing user passwords in plaintext is a critical security vulnerability. If the data store is compromised, all user accounts are immediately at risk. This violates fundamental security practices. Suggestion:
Suggested change
|
||||||||||||||
| username, | ||||||||||||||
|
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. [HIGH] correctness: User ID collision on deletion The user ID generation Suggestion:
Suggested change
|
||||||||||||||
| password, | ||||||||||||||
| role: role || 'user', | ||||||||||||||
| createdAt: new Date() | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| users.push(user); | ||||||||||||||
| res.status(201).json(user); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| // POST /users/login | ||||||||||||||
|
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. 🩺 [MEDIUM] correctness: Loose equality (==) used for password comparison Using Suggestion:
Suggested change
|
||||||||||||||
| router.post('/login', (req, res) => { | ||||||||||||||
|
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. 🩺 [CRITICAL] security: No password hashing during login comparison The login process compares the provided password directly with the plaintext password stored in the user object. This means the system is not using hashed passwords for verification, which is a critical security flaw. Suggestion:
Suggested change
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. 🩺 [MEDIUM] correctness: Loose equality (==) for password comparison Using Suggestion:
Suggested change
|
||||||||||||||
| const { username, password } = req.body; | ||||||||||||||
|
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. [LOW] style: Loose equality in login comparison Using Suggestion:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| // BUG: == instead of === for comparison | ||||||||||||||
|
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. [MEDIUM] correctness: Loose equality comparison (==) instead of strict (===) Using Suggestion:
Suggested change
|
||||||||||||||
| const user = users.find(u => u.username == username && u.password == password); | ||||||||||||||
|
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. [HIGH] security: Hardcoded authentication token The login endpoint returns a hardcoded token, which provides no actual security and defeats the purpose of authentication. This makes the system vulnerable to unauthorized access. Suggestion:
Suggested change
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. 🩺 [CRITICAL] security: Login response returns user object with password The login endpoint returns the full user object, including the plaintext password, in its response. This is a critical data exposure vulnerability, similar to the GET /users endpoint. Suggestion:
Suggested change
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. 🩺 [MEDIUM] security: Hardcoded authentication token The authentication token Suggestion:
Suggested change
|
||||||||||||||
| if (!user) { | ||||||||||||||
| return res.status(401).json({ error: 'Invalid credentials' }); | ||||||||||||||
|
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. 🩺 [CRITICAL] security: Login response exposes user object with password The login endpoint returns the entire user object, including the plaintext password, in its response. This is a critical information disclosure vulnerability, compromising user credentials upon successful login. Suggestion:
Suggested change
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. 🩺 [HIGH] security: Hardcoded authentication token The authentication token is hardcoded to 'hardcoded-token-abc123'. This provides no actual security, as any client can use this static token to bypass authentication. Suggestion:
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // BUG: no real token — returns user object including password | ||||||||||||||
|
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. [CRITICAL] security: Returns user object with password on login The login response includes the full Suggestion:
Suggested change
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. [HIGH] security: Hardcoded authentication token Using a hardcoded token for authentication is insecure and defeats the purpose of authentication. Every user would receive the same 'token', making it trivial to impersonate others. Suggestion:
Suggested change
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. 🩺 [CRITICAL] security: Missing authentication and authorization for DELETE /users/:id The DELETE /users/:id endpoint lacks any authentication or authorization checks. This allows any unauthenticated user to delete any user account by simply knowing their ID, leading to a severe security flaw. Suggestion:
Suggested change
|
||||||||||||||
| res.json({ token: 'hardcoded-token-abc123', user }); | ||||||||||||||
| }); | ||||||||||||||
|
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. 🩺 [CRITICAL] security: Missing authentication/authorization for user deletion The Suggestion:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| // DELETE /users/:id — admin only, but no auth check | ||||||||||||||
|
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. [CRITICAL] security: Unprotected user deletion endpoint The Suggestion:
Suggested change
|
||||||||||||||
| router.delete('/:id', (req, res) => { | ||||||||||||||
|
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. [CRITICAL] security: Missing authentication and authorization for DELETE /users/:id The Suggestion:
Suggested change
|
||||||||||||||
| // BUG: no authentication or authorization check | ||||||||||||||
|
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. [MEDIUM] quality: Missing error handling for parseInt
Suggestion:
Suggested change
|
||||||||||||||
| const index = users.findIndex(u => u.id === parseInt(req.params.id)); | ||||||||||||||
| if (index === -1) return res.status(404).json({ error: 'User not found' }); | ||||||||||||||
| users.splice(index, 1); | ||||||||||||||
| res.status(204).send(); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| module.exports = router; | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 [CRITICAL] security: Exposes sensitive user data via GET /users
The
/usersendpoint returns the entireusersarray, including plaintext passwords. This is a severe data exposure vulnerability, allowing any client to retrieve all user credentials.Suggestion: