Skip to content

Yuliusupwork/myc 4513 fix unauthorized access in get apimemoriesget enforce room#1624

Open
pradipthaadhi wants to merge 2 commits intotestfrom
yuliusupwork/myc-4513-fix-unauthorized-access-in-get-apimemoriesget-enforce-room
Open

Yuliusupwork/myc 4513 fix unauthorized access in get apimemoriesget enforce room#1624
pradipthaadhi wants to merge 2 commits intotestfrom
yuliusupwork/myc-4513-fix-unauthorized-access-in-get-apimemoriesget-enforce-room

Conversation

@pradipthaadhi
Copy link
Copy Markdown
Collaborator

@pradipthaadhi pradipthaadhi commented Apr 1, 2026

Summary

  • Merges latest test into this branch and aligns message loading with the current architecture: the app uses getChatMessagesGET {API}/api/chats/{roomId}/messages with Authorization: Bearer (Privy access token).
  • The removed Next route /api/memories/get is no longer used on test; authentication and room/account access for message history are enforced on the API (validateChatAccess on GET /api/chats/[id]/messages in recoupable/api).

What changed (chat)

  • hooks/useMessageLoader.ts — loads via getChatMessages, passes accessToken + optional apiOverride.
  • hooks/useVercelChat.ts — wires useMessageLoader with the correct arguments after the merge.

Supersedes / related

…ation

- Added header validation and room ownership checks to the GET /api/memories/get endpoint.
- Implemented error handling for missing room ID, unauthorized access, room not found, and forbidden access.
- Created unit tests for various scenarios including authentication, room existence, and memory retrieval success.
- Updated useMessageLoader and related hooks to include access token for fetching messages.
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Apr 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
recoup-chat Ready Ready Preview Apr 1, 2026 5:27am

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

Updated JSDoc documentation in useMessageLoader hook to clarify its API-loading behavior and document two parameters: accessToken (required for authentication) and apiOverride (optional URL override). Removed outdated return documentation. No runtime logic changes.

Changes

Cohort / File(s) Summary
Hook Documentation Update
hooks/useMessageLoader.ts
Enhanced JSDoc with two new parameter declarations (accessToken and apiOverride) and removed stale @returns documentation. Clarifies Bearer token authentication and optional API endpoint customization.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

📚 A hook's secrets now revealed,
With tokens and overrides sealed,
Bearer auth made crystal clear,
Documentation drawing near! ✨

🚥 Pre-merge checks | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Solid & Clean Code ⚠️ Warning JSDoc missing @returns documentation violates clean code principle of self-documenting code; hook return structure undefined. Add @returns documentation describing the return object structure with isLoading, error, and hasError properties to complete the public interface contract.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch yuliusupwork/myc-4513-fix-unauthorized-access-in-get-apimemoriesget-enforce-room

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
hooks/useMessageLoader.ts (2)

23-62: ⚠️ Potential issue | 🟠 Major

Include setMessages in the dependency array.

The setMessages callback is used inside the effect but missing from the dependency array. This violates React's exhaustive-deps rule and could cause stale closures if the callback reference changes.

🔧 Proposed fix
     loadMessages();
-  }, [userId, roomId, accessToken, apiOverride]);
+  }, [userId, roomId, accessToken, apiOverride, setMessages]);

As per coding guidelines: "For custom hooks, ensure: Use proper dependency arrays" - all values referenced inside the effect must be included.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hooks/useMessageLoader.ts` around lines 23 - 62, The useEffect inside
useMessageLoader references the setMessages callback but doesn't include it in
the dependency array; update the effect's dependency array to include
setMessages so the effect re-runs if that callback changes (the effect that
defines loadMessages and calls getChatMessages should list setMessages along
with userId, roomId, accessToken, and apiOverride).

49-49: ⚠️ Potential issue | 🟠 Major

Add explicit return type to getChatMessages and remove the type assertion.

The getChatMessages function lacks an explicit return type annotation, and the cast as UIMessage[] bypasses type checking. The returned data structure { id, role, content } may not fully align with the UIMessage interface from the "ai" package (which may require additional fields or constrained role values like 'user' | 'assistant').

Add a return type to getChatMessages and ensure the mapped data matches UIMessage requirements, or create a proper type-safe transformation function rather than relying on a cast.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hooks/useMessageLoader.ts` at line 49, The getChatMessages function must
declare an explicit return type (e.g., UIMessage[] or a local mapped type) and
stop using the unsafe cast at the setMessages call; update getChatMessages to
return a properly typed array by mapping each source object to the UIMessage
shape required by the "ai" package (ensuring role is constrained to 'user' |
'assistant', include any required fields like id/content/role), then call
setMessages with that typed return value instead of using "as UIMessage[]";
adjust or add a dedicated transformer function if needed to guarantee the mapped
objects conform to UIMessage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@hooks/useMessageLoader.ts`:
- Around line 5-12: Add a `@returns` block to the JSDoc for the useMessageLoader
hook that documents the returned object shape: include descriptions for
isLoading (boolean indicating loading state), error (Error | null for any fetch
error), and hasError (boolean derived from error) so consumers understand the
hook's consistent interface; update the JSDoc above the useMessageLoader
function to explicitly list each property and its type/meaning.

---

Outside diff comments:
In `@hooks/useMessageLoader.ts`:
- Around line 23-62: The useEffect inside useMessageLoader references the
setMessages callback but doesn't include it in the dependency array; update the
effect's dependency array to include setMessages so the effect re-runs if that
callback changes (the effect that defines loadMessages and calls getChatMessages
should list setMessages along with userId, roomId, accessToken, and
apiOverride).
- Line 49: The getChatMessages function must declare an explicit return type
(e.g., UIMessage[] or a local mapped type) and stop using the unsafe cast at the
setMessages call; update getChatMessages to return a properly typed array by
mapping each source object to the UIMessage shape required by the "ai" package
(ensuring role is constrained to 'user' | 'assistant', include any required
fields like id/content/role), then call setMessages with that typed return value
instead of using "as UIMessage[]"; adjust or add a dedicated transformer
function if needed to guarantee the mapped objects conform to UIMessage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 668dc66e-a328-4919-87c2-6aa8b4c33011

📥 Commits

Reviewing files that changed from the base of the PR and between 7c82c73 and 5dd822f.

📒 Files selected for processing (1)
  • hooks/useMessageLoader.ts

Comment on lines 5 to 12
/**
* Hook for loading existing messages from a room
* Hook for loading existing messages from a room via the chats API (Bearer auth).
* @param roomId - The room ID to load messages from (undefined to skip loading)
* @param userId - The current user ID (messages won't load if user is not authenticated)
* @param accessToken - Privy access token for the API request
* @param apiOverride - Optional API base URL override (e.g. preview)
* @param setMessages - Callback function to set the loaded messages
* @returns Loading state and error information
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add @returns documentation to complete the JSDoc.

The JSDoc is missing documentation for the return value. The hook returns an object with isLoading, error, and hasError properties that consumers need to understand.

📝 Proposed documentation addition
 /**
  * Hook for loading existing messages from a room via the chats API (Bearer auth).
  * `@param` roomId - The room ID to load messages from (undefined to skip loading)
  * `@param` userId - The current user ID (messages won't load if user is not authenticated)
  * `@param` accessToken - Privy access token for the API request
  * `@param` apiOverride - Optional API base URL override (e.g. preview)
  * `@param` setMessages - Callback function to set the loaded messages
+ * `@returns` Object containing loading state, error state, and error flag
  */

As per coding guidelines: "For custom hooks, ensure: Return consistent interface" - documenting the return value helps maintain clarity about the hook's consistent interface.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* Hook for loading existing messages from a room
* Hook for loading existing messages from a room via the chats API (Bearer auth).
* @param roomId - The room ID to load messages from (undefined to skip loading)
* @param userId - The current user ID (messages won't load if user is not authenticated)
* @param accessToken - Privy access token for the API request
* @param apiOverride - Optional API base URL override (e.g. preview)
* @param setMessages - Callback function to set the loaded messages
* @returns Loading state and error information
*/
/**
* Hook for loading existing messages from a room via the chats API (Bearer auth).
* `@param` roomId - The room ID to load messages from (undefined to skip loading)
* `@param` userId - The current user ID (messages won't load if user is not authenticated)
* `@param` accessToken - Privy access token for the API request
* `@param` apiOverride - Optional API base URL override (e.g. preview)
* `@param` setMessages - Callback function to set the loaded messages
* `@returns` Object containing loading state, error state, and error flag
*/
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hooks/useMessageLoader.ts` around lines 5 - 12, Add a `@returns` block to the
JSDoc for the useMessageLoader hook that documents the returned object shape:
include descriptions for isLoading (boolean indicating loading state), error
(Error | null for any fetch error), and hasError (boolean derived from error) so
consumers understand the hook's consistent interface; update the JSDoc above the
useMessageLoader function to explicitly list each property and its type/meaning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant