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

未关闭
#694 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
php
领域
backend, cloud

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
PHP
星标
43
派生
22
平均合并
54 分钟
30 天内合并 PR
57

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

quantum-php/framework 的其他 Issue

查看 quantum-php/framework 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。