Skip to content

Commit 507fcef

Browse files
committed
secured current delete-review endpoint and updated update-review-status
- this endpoint is not used, just added authentication to make sure api is robust. - adjusted authentication checks in update-review-status for security. - improved documentation on permissions
1 parent 1462c5e commit 507fcef

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

backend/src/app.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,21 @@ app.post('/api/add-like', authenticate, likeHandler(false));
490490
app.post('/api/remove-like', authenticate, likeHandler(true));
491491

492492
// Endpoint to delete a review by its document ID
493-
app.put('/api/delete-review/:reviewId', async (req, res) => {
493+
app.put('/api/delete-review/:reviewId', authenticate, async (req, res) => {
494+
if (!req.user) throw new Error('Not authenticated');
494495
const { reviewId } = req.params; // Extract the review document ID from the request parameters
496+
const { uid, email } = req.user;
497+
// Check if the user is an admin or the creator of the review
498+
const reviewDoc = reviewCollection.doc(reviewId);
499+
const reviewData = (await reviewDoc.get()).data();
500+
if (!reviewData) {
501+
res.status(404).send('Review not found');
502+
return;
503+
}
504+
if (reviewData?.userId !== uid && !(email && admins.includes(email))) {
505+
res.status(403).send('Unauthorized');
506+
return;
507+
}
495508
try {
496509
// Update the status of the review document to 'DELETED'
497510
await reviewCollection.doc(reviewId).update({ status: 'DELETED' });
@@ -706,8 +719,9 @@ app.post('/api/remove-saved-landlord', authenticate, saveLandlordHandler(false))
706719
* Sends an email to the user if the review is approved.
707720
*
708721
* Permissions:
709-
* User must be an admin to update a review to approved, declined, or deleted
710-
* However, all users can update a review from approved to pending
722+
* - An admin can update a review from any status to any status
723+
* - A regular user can only update their own reviews from any status to deleted
724+
* - A regular user cannot update other users' reviews
711725
*
712726
* @param reviewDocId - The document ID of the review to update
713727
* @param newStatus - The new status to set for the review
@@ -722,11 +736,7 @@ app.put('/api/update-review-status/:reviewDocId/:newStatus', authenticate, async
722736
if (!req.user) throw new Error('Not authenticated');
723737
const { reviewDocId, newStatus } = req.params; // Extracting parameters from the URL
724738
const { uid, email } = req.user;
725-
// Checking if the user is authorized to update the review's status
726-
if (newStatus !== 'PENDING' && !(email && admins.includes(email))) {
727-
res.status(403).send('Unauthorized');
728-
return;
729-
}
739+
const isAdmin = email && admins.includes(email);
730740
const statusList = ['PENDING', 'APPROVED', 'DECLINED', 'DELETED'];
731741
try {
732742
// Validating if the new status is within the allowed list
@@ -735,7 +745,14 @@ app.put('/api/update-review-status/:reviewDocId/:newStatus', authenticate, async
735745
return;
736746
}
737747
const reviewDoc = reviewCollection.doc(reviewDocId);
738-
const currentStatus = (await reviewDoc.get()).data()?.status || '';
748+
const reviewData = (await reviewDoc.get()).data();
749+
const currentStatus = reviewData?.status || '';
750+
const reviewOwnerId = reviewData?.userId || '';
751+
// Check if user is authorized to change this review's status
752+
if (!isAdmin && (uid !== reviewOwnerId || newStatus !== 'DELETED')) {
753+
res.status(403).send('Unauthorized');
754+
return;
755+
}
739756
// Updating the review's status in Firestore
740757
await reviewDoc.update({ status: newStatus });
741758
res.status(200).send('Success'); // Sending a success response

0 commit comments

Comments
 (0)