Repository navigation
feat(go-api): migrate searchbot share detail endpoint to go - #16124
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds a new unauthenticated ChangesSearchbot Share-Detail Endpoint
Sequence Diagram(s)sequenceDiagram
participant Client
participant SearchBotHandler
participant UserService
participant APITokenDAO
participant SearchService
participant SearchDAO
rect rgba(100, 149, 237, 0.5)
note over Client,APITokenDAO: Beta Token Authentication
Client->>SearchBotHandler: GET /searchbots/detail?search_id=X (Authorization: Bearer <beta>)
SearchBotHandler->>UserService: GetUserByBetaAPIToken(authorization)
UserService->>APITokenDAO: GetByBeta(token)
APITokenDAO-->>UserService: *APIToken (TenantID)
UserService->>UserService: fetch user by TenantID, validate access_token
UserService-->>SearchBotHandler: *User or CodeUnauthorized
end
rect rgba(60, 179, 113, 0.5)
note over SearchBotHandler,SearchDAO: Share-Detail Retrieval
SearchBotHandler->>SearchService: GetSearchShareDetail(user.ID, searchID)
SearchService->>SearchService: GetSearchDetail (access control check)
SearchService->>SearchDAO: GetDetailByID(searchID)
SearchDAO-->>SearchService: *SearchDetailRow (joined user fields)
SearchService-->>SearchBotHandler: *SearchShareDetail
SearchBotHandler-->>Client: CodeSuccess + detail payload
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
internal/service/user_beta_test.go (1)
78-88: ⚡ Quick winAdd coverage for empty/whitespace Authorization headers.
Please add assertions for cases like
"Bearer "and" "returningcommon.CodeUnauthorized. This locks in the auth-parser boundary behavior and guards regressions.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/service/user_beta_test.go` around lines 78 - 88, The test TestUserServiceGetUserByBetaAPITokenRejectsInvalidToken only validates the "Bearer missing" case but lacks coverage for edge cases where the Authorization header contains only whitespace or empty values. Add additional assertions in this test function to verify that calling GetUserByBetaAPIToken with inputs like "Bearer " (Bearer followed by only spaces) and " " (only whitespace) both properly reject the request and return common.CodeUnauthorized, ensuring the auth-parser correctly handles these boundary conditions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/searchbot.go`:
- Around line 424-433: The default case in the error handling for the
GetSearchShareDetail call in the searchbot.go handler is exposing internal error
details directly to clients by passing err.Error() to the jsonError function.
Replace the default case to return a generic public error message (such as "An
error occurred" or similar) to the client, and instead log the actual error
details server-side using an appropriate logger to maintain debuggability while
preventing information disclosure of backend internals.
In `@internal/service/user.go`:
- Around line 1076-1089: The token extraction logic can accept empty or
whitespace-only tokens, which then gets passed to apiTokenDAO.GetByBeta(). After
extracting the token from the authorization string split, add validation to
check that the token is not empty or contains only whitespace before calling
apiTokenDAO.GetByBeta(). If the token is empty or blank, return an unauthorized
error without making the DAO call to avoid unnecessary database lookups.
---
Nitpick comments:
In `@internal/service/user_beta_test.go`:
- Around line 78-88: The test
TestUserServiceGetUserByBetaAPITokenRejectsInvalidToken only validates the
"Bearer missing" case but lacks coverage for edge cases where the Authorization
header contains only whitespace or empty values. Add additional assertions in
this test function to verify that calling GetUserByBetaAPIToken with inputs like
"Bearer " (Bearer followed by only spaces) and " " (only whitespace) both
properly reject the request and return common.CodeUnauthorized, ensuring the
auth-parser correctly handles these boundary conditions.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6a86e76f-b8bc-42e0-a3c6-78e55f356bd4
📒 Files selected for processing (11)
internal/dao/api_token.gointernal/dao/api_token_beta_test.gointernal/dao/search.gointernal/dao/search_detail_test.gointernal/handler/searchbot.gointernal/handler/searchbot_detail_test.gointernal/router/router.gointernal/service/search.gointernal/service/search_share_detail_test.gointernal/service/user.gointernal/service/user_beta_test.go
| detail, err := h.searchSvc.GetSearchShareDetail(user.ID, searchID) | ||
| if err != nil { | ||
| switch err.Error() { | ||
| case "has no permission for this operation": | ||
| jsonError(c, common.CodeOperatingError, "Has no permission for this operation.") | ||
| case "can't find this Search App!": | ||
| jsonError(c, common.CodeDataError, "Can't find this Search App!") | ||
| default: | ||
| jsonError(c, common.CodeServerError, err.Error()) | ||
| } |
There was a problem hiding this comment.
Do not expose internal error text in public responses.
The default branch returns err.Error() directly to clients, which can leak backend internals from DAO/service failures. Return a fixed public message and log the internal error server-side.
🔧 Suggested patch
detail, err := h.searchSvc.GetSearchShareDetail(user.ID, searchID)
if err != nil {
switch err.Error() {
case "has no permission for this operation":
jsonError(c, common.CodeOperatingError, "Has no permission for this operation.")
case "can't find this Search App!":
jsonError(c, common.CodeDataError, "Can't find this Search App!")
default:
- jsonError(c, common.CodeServerError, err.Error())
+ common.Warn("searchbot detail failed", zap.Error(err))
+ jsonError(c, common.CodeServerError, "Internal server error")
}
return
}📝 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.
| detail, err := h.searchSvc.GetSearchShareDetail(user.ID, searchID) | |
| if err != nil { | |
| switch err.Error() { | |
| case "has no permission for this operation": | |
| jsonError(c, common.CodeOperatingError, "Has no permission for this operation.") | |
| case "can't find this Search App!": | |
| jsonError(c, common.CodeDataError, "Can't find this Search App!") | |
| default: | |
| jsonError(c, common.CodeServerError, err.Error()) | |
| } | |
| detail, err := h.searchSvc.GetSearchShareDetail(user.ID, searchID) | |
| if err != nil { | |
| switch err.Error() { | |
| case "has no permission for this operation": | |
| jsonError(c, common.CodeOperatingError, "Has no permission for this operation.") | |
| case "can't find this Search App!": | |
| jsonError(c, common.CodeDataError, "Can't find this Search App!") | |
| default: | |
| common.Warn("searchbot detail failed", zap.Error(err)) | |
| jsonError(c, common.CodeServerError, "Internal server error") | |
| } | |
| return | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/handler/searchbot.go` around lines 424 - 433, The default case in
the error handling for the GetSearchShareDetail call in the searchbot.go handler
is exposing internal error details directly to clients by passing err.Error() to
the jsonError function. Replace the default case to return a generic public
error message (such as "An error occurred" or similar) to the client, and
instead log the actual error details server-side using an appropriate logger to
maintain debuggability while preventing information disclosure of backend
internals.
There was a problem hiding this comment.
@Hz-186 Please check this.
There was a problem hiding this comment.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
There was a problem hiding this comment.
⚠️ Potential issue | 🟠 Major | ⚡ Quick winDo not expose internal error text in public responses.
The default branch returns
err.Error()directly to clients, which can leak backend internals from DAO/service failures. Return a fixed public message and log the internal error server-side.
🔧 Suggested patch📝 Committable suggestion
🤖 Prompt for AI Agents
fixed
| parts := strings.Split(authorization, " ") | ||
| var token string | ||
| if len(parts) == 2 { | ||
| token = parts[1] | ||
| } else if len(parts) == 1 { | ||
| token = parts[0] | ||
| } else { | ||
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid authorization format") | ||
| } | ||
|
|
||
| apiTokenDAO := dao.NewAPITokenDAO() | ||
| userToken, err := apiTokenDAO.GetByBeta(token) | ||
| if err != nil { | ||
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid beta access token") |
There was a problem hiding this comment.
Reject empty/whitespace beta tokens before DAO lookup.
The current parser can pass an empty token (for example, "Bearer "), then query GetByBeta(""). Enforce non-empty token extraction and robust splitting before the DB call.
🔧 Suggested fix
- parts := strings.Split(authorization, " ")
+ parts := strings.Fields(authorization)
var token string
- if len(parts) == 2 {
- token = parts[1]
+ if len(parts) == 2 {
+ if !strings.EqualFold(parts[0], "Bearer") {
+ return nil, common.CodeUnauthorized, fmt.Errorf("invalid authorization format")
+ }
+ token = parts[1]
} else if len(parts) == 1 {
token = parts[0]
} else {
return nil, common.CodeUnauthorized, fmt.Errorf("invalid authorization format")
}
+ if strings.TrimSpace(token) == "" {
+ return nil, common.CodeUnauthorized, fmt.Errorf("authorization token is empty")
+ }📝 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.
| parts := strings.Split(authorization, " ") | |
| var token string | |
| if len(parts) == 2 { | |
| token = parts[1] | |
| } else if len(parts) == 1 { | |
| token = parts[0] | |
| } else { | |
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid authorization format") | |
| } | |
| apiTokenDAO := dao.NewAPITokenDAO() | |
| userToken, err := apiTokenDAO.GetByBeta(token) | |
| if err != nil { | |
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid beta access token") | |
| parts := strings.Fields(authorization) | |
| var token string | |
| if len(parts) == 2 { | |
| if !strings.EqualFold(parts[0], "Bearer") { | |
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid authorization format") | |
| } | |
| token = parts[1] | |
| } else if len(parts) == 1 { | |
| token = parts[0] | |
| } else { | |
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid authorization format") | |
| } | |
| if strings.TrimSpace(token) == "" { | |
| return nil, common.CodeUnauthorized, fmt.Errorf("authorization token is empty") | |
| } | |
| apiTokenDAO := dao.NewAPITokenDAO() | |
| userToken, err := apiTokenDAO.GetByBeta(token) | |
| if err != nil { | |
| return nil, common.CodeUnauthorized, fmt.Errorf("invalid beta access token") |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/service/user.go` around lines 1076 - 1089, The token extraction
logic can accept empty or whitespace-only tokens, which then gets passed to
apiTokenDAO.GetByBeta(). After extracting the token from the authorization
string split, add validation to check that the token is not empty or contains
only whitespace before calling apiTokenDAO.GetByBeta(). If the token is empty or
blank, return an unauthorized error without making the DAO call to avoid
unnecessary database lookups.
0534e37 to
55f6f1f
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16124 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
LGTM |
Summary
This PR migrates
GET /api/v1/searchbots/detailfrom Python to Go.Changes
/api/v1/searchbots/detailVerification
Compared Python and Go responses with real requests:
http://127.0.0.1:9380/api/v1/searchbots/detailhttp://127.0.0.1:9384/api/v1/searchbots/detailVerified: