Sayantan Das
Sayantan Das

Reputation: 1641

Malformed Unicode code sequence in the field

I am trying browser uploads to my s3 bucket using POST form. I am using eddturtle/direct-upload to generate the policy and signature. This is my code:

$upload = new Signature(
            self::S3_KEY,
            self::S3_SECRET,
            self::S3_BUCKET,
            self::S3_REGION
        );

And generating the html using $upload->getFormInputsAsHtml() method. Which is generating an html like this

<form action="//s3.amazonaws.com/bucket" method="POST" enctype="multipart/form-data" class="upload-form">
    <input type="hidden" name="Content-Type" value="">
    <input type="hidden" name="acl" value="public-read">
    <input type="hidden" name="success_action_status" value="201">
    <input type="hidden" name="policy" value="eyJleHBpcmF0aW9uIjoiMjAxNy0wMi0xNVQxMzowNDozOFoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJ0YWdmaSJ9LHsiYWNsIjoicHJpdmF0ZSJ9LFsic3RhcnRzLXdpdGgiLCIka2V5IiwiIl0sWyJzdGFydHMtd2l0aCIsIiRDb250ZW50LVR5cGUiLCIiXSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwwLDUyNDI4ODAwMF0seyJzdWNjNzX2FjdGlvbl9zdGF0dXMiOiIyMDEifSx7IngtYW16LWNyZWRlbnRpYWwiOiJBS0lBSkhGS0hKMklLNjZTVFA0QVwvMjAxNzAyMTVcL3VzLWVhc3QtMVwvczNcL2F3czRfcmVxdWVzdCJ9LHsieC1hbXotYWxnb3JpdGhtIjoiQVdTNC1ITUFDLVNIQTI1NiJ9LHsieC1hbXotZGF0ZSI6IjIwMTcwMjE1VDA3MDQzOFoifV19">
    <input type="hidden" name="X-amz-credential" value="AKIAJHFKHJ2IK66STP4A/20170215/us-east-1/s3/aws4_request">
    <input type="hidden" name="X-amz-algorithm" value="AWS4-HMAC-SHA256">
    <input type="hidden" name="X-amz-date" value="20170215T070438Z">
    <input type="hidden" name="X-amz-signature" value="e9d30613e4fcab8fec3fe75c4ce2969bb6497d83af7cca7f9ea6a1c0738844c5">
    <input type="hidden" name="key" value="${filename}">
</form>

But after submitting the form I am getting this error from s3

<Error>
    <Code>InvalidArgument</Code>
    <Message>Malformed Unicode code sequence in the field.</Message>
    <ArgumentName>formField</ArgumentName>
    <RequestId>46E52494FC98ED76</RequestId>
    <HostId>
        Qk/g94sJSfVicIxPb3oFN0nO1EVCvxy8YzBQjRKyKpATZZmX1VjlDK+zWfvidhZl5vuemZyuKg0=
    </HostId>
</Error>

Sample request payload

------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="image"; filename="shop-small.png" Content-Type: image/png

------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="Content-Type"

------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="acl"

private ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="success_action_status"

201 ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="policy"

eyJleHBpcmF0aW9uIjoiMjAxNy0wMi0xNVQxOToxMzo1NVoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJ0YWdmaSJ9LHsiYWNsIjoicHJpdmF0ZSJ9LFsic3RhcnRzLXdpdGgiLCIka2V5IiwiIl0sWyJzdGFydHMtd2l0aCIsIiRDb250ZW50LVR5cGUiLCIiXSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwwLDUyNDI4ODAwMF0seyJzdWNjZXNzX2FjdGlvbl9zdGF0dXMiOiIyMDEifSx7IngtYW16LWNyZWRlbnRpYWwiOiJBS0lBSkhGS0hKMklLNjZTVFA0QVwvMjAxNzAyMTVcL3VzLWVhc3QtMVwvczNcL2F3czRfcmVxdWVzdCJ9LHsieC1hbXotYWxnb3JpdGhtIjoiQVdTNC1ITUFDLVNIQTI1NiJ9LHsieC1hbXotZGF0ZSI6IjIwMTcwMjE1VDEzMTM1NVoifV19 ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="X-amz-credential"

AKIAJHFKHJ2IK66STP4A/20170215/us-east-1/s3/aws4_request ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="X-amz-algorithm"

AWS4-HMAC-SHA256 ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="X-amz-date"

20170215T131355Z ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="X-amz-signature"

1c28fb88fc0b1317df11b7f566fe39eaac9b8cba012b8fef80448e93767f2677 ------WebKitFormBoundaryVGEGWkAW5d70u2rp Content-Disposition: form-data; name="key"

testfile ------WebKitFormBoundaryVGEGWkAW5d70u2rp--

I don't know what the issue is and how to fix it. Every example I have gone through is using similar process. Thanks in advance.

Upvotes: 5

Views: 2559

Answers (4)

Slobodan Djordjevic
Slobodan Djordjevic

Reputation: 69

Had the same issue, there was an additional field that was causing this. So just removing it resolved it

key: 23/26da4cfd-dfd1-4d8d-a69b-4de51ad6b0b0.png
Content-Type: image/png
bucket: user-data
X-Amz-Algorithm: AWS4-HMAC-SHA256
X-Amz-Credential: AKIO/1/us-east-1/s3/aws4_request
X-Amz-Date: 
20230129T022521Z
Policy: eyJleHBpcmF0aW9uIjoiMjAyMy0wMS0y
X-Amz-Signature: 5ceb01fc3b135ca1ec6ef42de
file: (binary)
image: (binary) <---- This field was the issue

Upvotes: 0

Sayantan Das
Sayantan Das

Reputation: 1641

I finally got it fixed. Though I still don't know what was causing the issue. Instead of using $upload->getFormInputsAsHtml(); to get the form content, I used $s3Policy = $upload->getFormInputs(); to get the policy and individually assigning to pre-written html.

<form :action="aws_url" method="POST" enctype="multipart/form-data">

    <input type="hidden" name="Content-Type" :value="s3Policy.content_type" />
    <input type="hidden" name="acl" :value="s3Policy.acl" />
    <input type="hidden" name="success_action_status" :value="s3Policy.success_action_status" />
    <input type="hidden" name="policy" :value="s3Policy.policy" />
    <input type="hidden" name="X-amz-credential" :value="s3Policy.credential" />
    <input type="hidden" name="X-amz-algorithm" :value="s3Policy.algorithm" />
    <input type="hidden" name="X-amz-date" :value="s3Policy.date" />
    <input type="hidden" name="X-amz-signature" :value="s3Policy.signature" />
    <input type="hidden" name="key" :value="s3Policy.key" />
    <input type="file" name="file">
    <input type="submit" value="Upload">

</form>

May be this was caused by vuejs on my frontend but the html looked fine in dev console.

Upvotes: 1

Titi Wangsa bin Damhore
Titi Wangsa bin Damhore

Reputation: 7199

I decoded your base64 content It looks like you have extra characters on your "credential" field. Looks like AKIAJHFKHJ2IK66STP4A\/20170215\/us-east-1\/s3\/aws4_request

You will need to change it to AKIAJHFKHJ2IK66STP4A/20170215/us-east-1/s3/aws4_request

Upvotes: 0

Titi Wangsa bin Damhore
Titi Wangsa bin Damhore

Reputation: 7199

I copied your "policy" value and pasted it on https://www.base64decode.org

The default encoding is UTF-8.

It failed to decode.

When I changed it to ASCII, the result was partially ok.

{"expiration":"2017-02-15T13:04:38Z","conditions":[{"bucket":"tagfi"},{"acl":"private"},["starts-with","$key",""],["starts-with","$Content-Type",""],["content-length-range",0,524288000],{"succ757F7FGW2###''7&VFVF#$d$ce5ED#s#UW2V7B35w3E&WVW7B''v&F#$u3B24#Sb''FFR###s#UCsC3'

It looks like "success" may have a different "e".

Try posting you plain text policy on https://www.base64encode.org and use that encoded value.

Or reply it here so I can take a look.

Hope this helps

Upvotes: 0

Related Questions