Repository navigation
fix: replace sanitize_email with sanitize_text_field on DELETE/refresh routes - #11
Conversation
…h routes sanitize_email is too strict for lookup operations: it strips characters valid in some email addresses (underscores in domain subdomains, local-only domains like 'localhost') and returns '' for anything it considers invalid. When the sanitized value is '', removeAccount() finds no match and returns false, causing the REST endpoint to return 404 'Account not found in pool.' — which the frontend surfaces as 'Failed to remove account.' The email was already validated by sanitize_email when the account was added (via the /exchange endpoint). The DELETE and /refresh routes only need to look up an existing email, so sanitize_text_field is the correct callback. Also: - removeAccount() now returns savePool()'s result instead of always true, so a DB-level save failure is surfaced rather than silently ignored. - listAccounts() now includes validity: null so the StatusBadge component receives a consistent field (null = not checked; use /health to validate).
📝 WalkthroughWalkthroughThe changes update OAuth pool REST API sanitization and enhance the PoolManager to provide additional account status information. The Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/OAuthPool/PoolManager.php`:
- Line 425: The current removeAccount() return value conflates "not found" and
"save failed" because it returns false from savePool(); change the contract so
removeAccount() returns a status enum (e.g., STATUS_NOT_FOUND, STATUS_SAVED,
STATUS_SAVE_ERROR) or a distinct sentinel for each outcome, update savePool()
(or its caller) to propagate a SAVE_ERROR status when DB persistence fails, and
then update the REST handler that calls removeAccount() to map STATUS_NOT_FOUND
-> 404 and STATUS_SAVE_ERROR -> 500 (keep STATUS_SAVED as 200/204); use explicit
symbols like removeAccount(), savePool(), and the new enum constants to locate
and modify the code.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: d4ad51e9-f008-49d1-a33c-24384594c257
📒 Files selected for processing (2)
inc/rest-api.phpsrc/OAuthPool/PoolManager.php
|
|
||
| $this->savePool($pool); | ||
| return true; | ||
| return $this->savePool($pool); |
There was a problem hiding this comment.
Differentiate “not found” vs “save failed” outcomes from removeAccount().
At Line 425, false now conflates two states. Downstream, inc/rest-api.php Line 265 treats all false as 404, so DB save failures are misreported as “not found” instead of server errors.
Proposed contract fix (status enum) and REST mapping
diff --git a/src/OAuthPool/PoolManager.php b/src/OAuthPool/PoolManager.php
@@
- public function removeAccount(string $email): bool
+ public function removeAccount(string $email): string
{
@@
- if (count($pool['accounts']) === $original) {
- return false;
+ if (count($pool['accounts']) === $original) {
+ return 'not_found';
}
- return $this->savePool($pool);
+ return $this->savePool($pool) ? 'removed' : 'save_failed';
}diff --git a/inc/rest-api.php b/inc/rest-api.php
@@
function rest_remove_account( \WP_REST_Request $request ) {
$email = $request->get_param( 'email' );
$pool = PoolManager::getInstance();
+ $result = $pool->removeAccount( $email );
- if ( ! $pool->removeAccount( $email ) ) {
+ if ( 'not_found' === $result ) {
return new \WP_Error(
'not_found',
__( 'Account not found in pool.', 'ai-provider-for-anthropic-max' ),
[ 'status' => 404 ]
);
}
+ if ( 'save_failed' === $result ) {
+ return new \WP_Error(
+ 'pool_save_failed',
+ __( 'Failed to persist account removal.', 'ai-provider-for-anthropic-max' ),
+ [ 'status' => 500 ]
+ );
+ }
return rest_ensure_response( [
'success' => true,
'count' => $pool->count(),
] );
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/OAuthPool/PoolManager.php` at line 425, The current removeAccount()
return value conflates "not found" and "save failed" because it returns false
from savePool(); change the contract so removeAccount() returns a status enum
(e.g., STATUS_NOT_FOUND, STATUS_SAVED, STATUS_SAVE_ERROR) or a distinct sentinel
for each outcome, update savePool() (or its caller) to propagate a SAVE_ERROR
status when DB persistence fails, and then update the REST handler that calls
removeAccount() to map STATUS_NOT_FOUND -> 404 and STATUS_SAVE_ERROR -> 500
(keep STATUS_SAVED as 200/204); use explicit symbols like removeAccount(),
savePool(), and the new enum constants to locate and modify the code.
Root cause
Changes
Testing
Merged via PR #11 to main. aidevops.sh v3.6.235 spent 13m on this as a headless bash routine. |
Addresses CodeRabbit review feedback from PR #11. removeAccount() previously returned false for both outcomes: - account not found in pool - account found but database save failed This made the REST handler conflate the two, always returning 404. Changes: - Add REMOVE_NOT_FOUND, REMOVE_OK, REMOVE_SAVE_ERROR constants to PoolManager - Change removeAccount() return type from bool to string (one of the constants) - Update rest_remove_account() to map REMOVE_NOT_FOUND → 404 and REMOVE_SAVE_ERROR → 500 Fixes #17
…17) Addresses CodeRabbit review feedback from PR #11. removeAccount() previously returned false for both outcomes: - account not found in pool - account found but database save failed This made the REST handler conflate the two, always returning 404. Changes: - Add REMOVE_NOT_FOUND, REMOVE_OK, REMOVE_SAVE_ERROR constants to PoolManager - Change removeAccount() return type from bool to string (one of the constants) - Update rest_remove_account() to map REMOVE_NOT_FOUND → 404 and REMOVE_SAVE_ERROR → 500 Fixes #17
Root cause
sanitize_emailwas used as thesanitize_callbackfor theemailURL path parameter on theDELETE /accounts/{email}andPOST /accounts/{email}/refreshroutes.sanitize_emailis a validator, not just a sanitizer — it returns''(empty string) for any email it considers invalid, including:user@my_company.com)user@localhost)When the sanitized value is
'',removeAccount('')finds no account with email'', returnsfalse, and the REST endpoint returns 404Account not found in pool.— which the frontend surfaces as "Failed to remove account."The email was already validated by
sanitize_emailwhen the account was added (via the/exchangeendpoint). The DELETE and/refreshroutes only need to look up an existing stored email, sosanitize_text_fieldis the correct callback.Changes
inc/rest-api.phpDELETE /accounts/{email}:sanitize_email→sanitize_text_fieldPOST /accounts/{email}/refresh:sanitize_email→sanitize_text_fieldsrc/OAuthPool/PoolManager.phpremoveAccount(): now returnssavePool()'s result instead of alwaystrueafter a count change — surfaces DB-level save failures rather than silently ignoring themlistAccounts(): addsvalidity: nullto each account entry so theStatusBadgecomponent receives a consistent field (null= not checked; use/healthto validate tokens)Testing
user@example.com) can be removed ✓user@my_company.com) can be removed ✓validity: null)Summary by CodeRabbit