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

fix(v4-policy): encode special characters #1169

Merged
merged 6 commits into from May 1, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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