Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tony-wuzx opened this issue Oct 17, 2023 · 0 comments

Comments

@tony-wuzx
Copy link

  • 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')) { 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant