Skip to content

Commit

Permalink
fix(v4-policy): encode special characters (#1169)
Browse files Browse the repository at this point in the history
* fix(v4-policy): encode special characters

* test special character encoding

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
jkwlui and bcoe committed May 1, 2020
1 parent 15ba795 commit 6e48539
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
11 changes: 5 additions & 6 deletions conformance-test/v4SignedUrl.ts
Expand Up @@ -220,16 +220,15 @@ describe('v4 conformance test', () => {

assert.strictEqual(policy.url, testCase.policyOutput.url);
const outputFields = testCase.policyOutput.fields;
const decodedPolicy = Buffer.from(
policy.fields.policy,
'base64'
).toString();
const decodedPolicy = JSON.parse(
Buffer.from(policy.fields.policy, 'base64').toString()
);
assert.deepStrictEqual(
decodedPolicy,
testCase.policyOutput.expectedDecodedPolicy
JSON.parse(testCase.policyOutput.expectedDecodedPolicy)
);

assert.deepStrictEqual(outputFields, testCase.policyOutput.fields);
assert.deepStrictEqual(policy.fields, outputFields);

fakeTimer.restore();
});
Expand Down
4 changes: 2 additions & 2 deletions src/file.ts
Expand Up @@ -63,7 +63,7 @@ import {
} from '@google-cloud/common/build/src/util';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const duplexify: DuplexifyConstructor = require('duplexify');
import {normalize, objectKeyToLowercase} from './util';
import {normalize, objectKeyToLowercase, unicodeJSONStringify} from './util';
import {GaxiosError, Headers, request as gaxiosRequest} from 'gaxios';

export type GetExpirationDateResponse = [Date];
Expand Down Expand Up @@ -2611,7 +2611,7 @@ class File extends ServiceObject<File> {
expiration,
};

const policyString = JSON.stringify(policy);
const policyString = unicodeJSONStringify(policy);
const policyBase64 = Buffer.from(policyString).toString('base64');

try {
Expand Down
13 changes: 13 additions & 0 deletions src/util.ts
Expand Up @@ -90,3 +90,16 @@ export function objectKeyToLowercase<T>(object: {[key: string]: T}) {
}
return newObj;
}

/**
* JSON encode str, with unicode \u+ representation.
* @param {object} obj The object to encode.
* @return {string} Serialized string.
*/
export function unicodeJSONStringify(obj: object) {
return JSON.stringify(obj).replace(
/[\u0080-\uFFFF]/g,
(char: string) =>
'\\u' + ('0000' + char.charCodeAt(0).toString(16)).slice(-4)
);
}
24 changes: 24 additions & 0 deletions test/file.ts
Expand Up @@ -2941,6 +2941,30 @@ describe('File', () => {
);
});

it('should encode special characters in policy', done => {
CONFIG = {
fields: {
'x-goog-meta-foo': 'bår',
},
...CONFIG,
};

file.generateSignedPostPolicyV4(
CONFIG,
(err: Error, res: SignedPostPolicyV4Output) => {
assert.ifError(err);

assert.strictEqual(res.fields['x-goog-meta-foo'], 'bår');
const decodedPolicy = Buffer.from(
res.fields.policy,
'base64'
).toString('utf-8');
assert(decodedPolicy.includes('"x-goog-meta-foo":"b\\u00e5r"'));
done();
}
);
});

it('should not include fields with x-ignore- prefix in conditions', done => {
CONFIG = {
fields: {
Expand Down

0 comments on commit 6e48539

Please sign in to comment.