aliyun / aliyun/openapi-sdk-php

ROA Request invalid signature : resolveBody never add Content-MD5 if isset $this->options['body']

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

Nobody has claimed this yet.

Dominant language
PHP
Stars
530
Forks
110
PR merge metrics
No merged PRs in 30d

Description

  • PHP Version:
  • 8.1
  • Platform:
  • Magento 2.4.6
  • Product:
  • CloudAPI
  • Product Version:
  • 2016-07-14
  • API:
  • ROA request

original code: got error invalid signature because Content-MD5 never add to headers in resolveParameter.

$req = AlibabaCloud::roa()
              ->product('CloudAPI')
              ->version('2016-07-14')
              ->host($this->config('host'))
              ->pathPattern($this->config('path'))
              ->scheme('https')
              ->options(['headers' =>['Content-Type'=>'application/json']])
              ->method('POST')
              ->body($body)
              ->debug(1);
  $req->resolveParameter();
  $res = $req->request();

fixed by below: add Content-MD5 myself

$req = AlibabaCloud::roa()
              ->product('CloudAPI')
              ->version('2016-07-14')
              ->host($this->config('host'))
              ->pathPattern($this->config('path'))
              ->scheme('https')
              ->options(['headers' => 
                   ['Content-Type'=>'application/json','Content-MD5' => base64_encode(hex2bin(md5($body)))]])
              ->method('POST')
              ->body($body)
              ->debug(1);
  $req->resolveParameter();
  $res = $req->request();

what I found:

/vendor/alibabacloud/client/src/Request/RoaRequest.php
private function resolveBody()
{
        // If the body has already been specified, it will not be resolved.
        if (isset($this->options['body'])) {
            return;
        }
        ......
}

should be :

private function resolveBody()
{
        // If the body has already been specified, it will not be resolved.
        if (isset($this->options['body'] )
            && Stringy::contains($this->options['headers']['Content-Type'], 'application/json')
        ) {
            $this->options['headers']['Content-MD5'] = base64_encode(hex2bin(md5($this->options['body'], true)));
            return;
        }
        ......
}

BTW:
you are using : ===> not try yet

base64_encode(md5($this->options['body'], true));

I am using: ===> it works

base64_encode(hex2bin(md5($body)))

Line 101:

if (Stringy::contains($this->options['headers']['Content-Type'], 'application/json', false)) {

should be:

if (Stringy::contains($this->options['headers']['Content-Type'], 'application/json')) { 

Contributor guide

Open the contributing guide

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.

Research direction

Start in vendor/alibabacloud/client/src/Request/RoaRequest.php, focusing on resolveBody() and the Content-Type check around line 101. Reproduce the POST ROA request with a body, then verify that Content-MD5 is added and the signature is accepted without manually supplying the header.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.