BLOG-119 Restricted access to unpublished posts #124
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch "BLOG-119_restrict_unpublished_post_access"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
This PR introduces an authorization layer for the post feature. It ensures that create, update, and read operations for posts are properly controlled based on user authentication status and post visibility (published vs. unpublished).
Key Changes:
Restricted Access to Unpublished Posts:
GET /post/{id}
endpoint. Attempting to do so will now result in anHTTP 401 Unauthorized
error.get_all_post_info
endpoint is now aware of the user's authentication status to correctly filter posts.Authentication Required for Modifications:
POST /post
) and updating (PUT /post/{id}
) posts now requires an authenticated user. Theuser_id
is passed from the web handler through the controller to the use cases.New Error Type:
PostError::Unauthorized
variant has been added to handle access control failures gracefully.API & Core Logic Updates:
PostController
, use cases (GetFullPostUseCase
,GetAllPostInfoUseCase
, etc.), and web handlers have been updated to accept and process theuser_id
.GetFullPostUseCase
now contains the primary logic to prevent unauthenticated access to draft posts.Package Changes
No response
Screenshots
No response
Reference
Resolves #119
Checklist
/improve
PR Code Suggestions ✨
Correct post visibility for users
The current logic for
is_published_only
is flawed. Ifuser_id
is present, itincorrectly forces
is_published_only
totrue
, preventing authenticated users fromseeing their own unpublished posts. More critically, if
is_published_only
isfalse
and
user_id
isNone
, this line would allow unauthenticated users to view unpublishedposts, which is a security vulnerability. The
is_published_only
flag passed to therepository should be
true
for unauthenticated users, and retain its original valuefor authenticated users.
backend/feature/post/src/application/use_case/get_all_post_info_use_case.rs [36]
Suggestion importance[1-10]: 10
__
Why: The suggestion correctly identifies a security vulnerability where unauthenticated users could potentially view unpublished posts. The proposed
improved_code
accurately fixes this by ensuringis_published_only
istrue
for unauthenticated users, while allowing authenticated users to see their own unpublished posts.Verify post ownership for access
The current check only prevents unauthenticated users from viewing unpublished
posts. However, it does not verify if an authenticated user (
user_id.is_some()
) isthe author of an unpublished post. An authenticated user should only be able to
view their own unpublished posts, not any unpublished post. Add a check to ensure
user_id
matchespost.author_id
when the post is unpublished anduser_id
is present.backend/feature/post/src/application/use_case/get_full_post_use_case.rs [30-32]
Suggestion importance[1-10]: 10
__
Why: This suggestion points out a critical security flaw where any authenticated user could view any unpublished post. The
improved_code
correctly adds a check to ensure that if a post is unpublished, only its author (or an unauthenticated user, who would be denied) can access it, preventing unauthorized access.Addressed in
50c9234fc3
.The author hasn't been implemented.