七牛云回調簽名驗證不一致的原因是什么?如何解決這個問題?

七牛云回調簽名驗證不一致的原因是什么?如何解決這個問題?

七牛云回調簽名驗證不一致問題分析與解決方案

在使用七牛云進行文件管理時,回調簽名驗證是確保數據安全的關鍵步驟。然而,有時會出現回調簽名驗證不一致的情況,導致驗證失敗。以下是問題的分析和解決方案。

問題分析

在給定的問題內容中,代碼嘗試驗證七牛云的回調簽名,但驗證結果始終與七牛云傳來的簽名不匹配。具體代碼如下:

public function verifyCallbackSignature(Request $request): bool {     $authstr = $request->header('Authorization');     if (empty($authstr) || strpos($authstr, "QBox ") !== 0) {         supportLog::debug("簽名驗證失敗-頭部格式錯誤", [             'sign_content' => 'qianmingshibai'         ]);         return false;     }      $auth = explode(":", substr($authstr, 5));     if (count($auth) !== 2 || $auth[0] !== env('QINIU_AK')) {         supportLog::debug("簽名驗證失敗-AK不匹配", [             'sign_content' => 'zhanghuAkshibai',             'auth_count' => count($auth),             'auth_ak' => $auth[0],         ]);         return false;     }      $data = $request->uri() . "n" . file_get_contents('php://input');     $re = $this->URLSafeBase64Encode(hash_hmac('sha1', $data, env('QINIU_SK'), true)) === $auth[1];      supportLog::debug("簽名驗證詳情", [         'data' => $data,         'computed_sign' => $this->URLSafeBase64Encode(hash_hmac('sha1', $data, env('QINIU_SK'), true)),         'received_sign' => $auth[1],     ]);      return $re; }

在實際測試中,發現即使嘗試使用body,但由于前端沒有添加body,導致后端獲取的body為空,因此生成的computed_sign始終與七牛云傳來的簽名不一致。

解決方案

為了解決這個問題,我們需要對代碼進行一些調整和完善。修改后的代碼如下:

public function verifyCallbackSignature(Request $request): bool {     $authstr = $request->header('Authorization');     if (empty($authstr) || strpos($authstr, "QBox ") !== 0) {         supportLog::debug("簽名驗證失敗-頭部格式錯誤", [             'sign_content' => 'qianmingshibai',             'authstr' => $authstr,         ]);         return false;     }      $auth = explode(":", substr($authstr, 5));     if (count($auth) !== 2 || $auth[0] !== env('QINIU_AK')) {         supportLog::debug("簽名驗證失敗-AK不匹配", [             'sign_content' => 'zhanghuAkshibai',             'auth_count' => count($auth),             'auth_ak' => $auth[0],             'expected_ak' => env('QINIU_AK'),         ]);         return false;     }      // 獲取 URI 和請求體     $uri = $request->uri();     if (!empty($_SERVER['QUERY_STRING'])) {         $uri .= '?' . $_SERVER['QUERY_STRING'];     }     $body = file_get_contents('php://input');     $data = $uri . "n" . $body;      // 計算簽名     $computed_sign = $this->URLSafeBase64Encode(hash_hmac('sha1', $data, env('QINIU_SK'), true));      // 記錄調試信息     supportLog::debug("簽名驗證詳情", [         'uri' => $uri,         'body' => $body,         'data' => $data,         'computed_sign' => $computed_sign,         'received_sign' => $auth[1],         'secret_key' => substr(env('QINIU_SK'), 0, 4) . '****',     ]);      return $computed_sign === $auth[1]; }  public function URLSafeBase64Encode($data): string {     return str_replace([' ', '/'], ['-', '_'], base64_encode($data)); }

解決方案解釋

  1. 完善URI獲取:在獲取URI時,考慮了可能存在的查詢字符串(QUERY_STRING),確保URI的完整性。
  2. 請求體的處理:即使前端沒有提供請求體,依然會讀取php://input,確保數據的完整性。
  3. 詳細的調試信息:增加了更多的調試信息,包括URI、請求體、計算出的簽名和接收到的簽名等,方便排查問題。
  4. 簽名計算:使用相同的算法和秘鑰進行簽名計算,確保計算出的簽名與七牛云傳來的簽名一致。

通過以上修改,可以有效解決七牛云回調簽名驗證不一致的問題,確保驗證過程順利進行。

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享