Dropbox and Google Drive: the token refresh check fails on text errors and Dropbox refreshes on every 401

Open
#694 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
backend, cloud

Research direction

Start in CloudAppTrait::sendRequest() to see how the parsed body reaches accessTokenNeedsRefresh(int $code, ?object $message = null) in DropboxApp and GoogleDriveApp, and in ResponseParser::parseBody() to confirm a non-JSON body comes back as a string. Widen both signatures to mixed and handle only object bodies, then change the Dropbox condition to || so only the tags in ACCESS_TOKEN_STATUS refresh. Done means new tests with a mocked HTTP client cover a 401 with a text body, a 401 with an unrelated .tag, and a 401 with expired_access_token refreshing exactly once.

Written by the indexing model from the issue text.

Description

bug

Summary

The Dropbox and Google Drive storage adapters decide after a failed API request whether the access token must be refreshed. That check fails in two ways.

Problems

  1. A TypeError for an error response that is not JSON. CloudAppTrait::sendRequest() passes the response body to accessTokenNeedsRefresh(int $code, ?object $message = null) in DropboxApp and GoogleDriveApp. ResponseParser::parseBody() returns an object only for a JSON response and the raw text otherwise, and Dropbox answers some errors with text/plain. With strict_types, the text causes a TypeError (Argument #2 ($message) must be of type ?object, string given) instead of the Exception with the API's error text that sendRequest() throws for errors that are not about the token.

  2. Dropbox refreshes the token on every 401. DropboxApp::accessTokenNeedsRefresh() reads:

    if (!isset($error['.tag']) && !in_array($error['.tag'], self::ACCESS_TOKEN_STATUS)) {
        return false;
    }
    
    return true;
    

    With && the method returns true whenever the error has a .tag, whatever its value, so a 401 that is not about the token (for example a missing permission) refreshes the token and retries. sendRequest() retries by calling itself, so the same 401 starts the next refresh: the request repeats until PHP gives up. Without a .tag, the code reads $error['.tag'] anyway and raises a warning. The condition should use ||, so only invalid_access_token and expired_access_token refresh the token.

Both are in 3.1.x as well; they are fixed in 3.2.0.

Fix

  • accessTokenNeedsRefresh() in both apps accepts the response body as it comes (mixed) and reads the error only from an object; a text body is not a token error, so sendRequest() throws its Exception with that text.
  • DropboxApp refreshes only for the .tag values in ACCESS_TOKEN_STATUS.
  • Tests with a mocked HTTP client: a 401 with a text body throws the Exception; a 401 with another .tag throws without refreshing; a 401 with expired_access_token refreshes and retries once.
Dominant language
PHP
Stars
43
Forks
22
Avg merge
54m
Merged PRs (30d)
57

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantum-php/framework

All issues in quantum-php/framework

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.