Skip to content

Commit

Permalink
fix(v4-policy): add missing bucket field (#1168)
Browse files Browse the repository at this point in the history
* fix(v4-policy): add missing bucket field

* remove bucket from fields

* fix tests
  • Loading branch information
jkwlui committed May 1, 2020
1 parent ea48df3 commit 555a950
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/file.ts
Expand Up @@ -2586,6 +2586,7 @@ class File extends ServiceObject<File> {

fields = {
...fields,
bucket: this.bucket.name,
key: this.name,
'x-goog-date': nowISO,
'x-goog-credential': credential,
Expand All @@ -2600,6 +2601,8 @@ class File extends ServiceObject<File> {
}
});

delete fields.bucket;

const expiration = dateFormat.format(
expires,
'YYYY-MM-DD[T]HH:mm:ss[Z]',
Expand Down
26 changes: 16 additions & 10 deletions test/file.ts
Expand Up @@ -2822,23 +2822,27 @@ describe('File', () => {
fakeTimer.restore();
});

const fieldsToConditions = (fields: object) =>
Object.entries(fields).map(([k, v]) => ({[k]: v}));

it('should create a signed policy', done => {
CONFIG.fields = {
'x-goog-meta-foo': 'bar',
};

const fields = {
...CONFIG.fields,
const requiredFields = {
key: file.name,
'x-goog-date': '20200101T000000Z',
'x-goog-credential': `${CLIENT_EMAIL}/20200101/auto/storage/goog4_request`,
'x-goog-algorithm': 'GOOG4-RSA-SHA256',
};

const policy = {
conditions: Object.entries(fields).map(([key, value]) => ({
[key]: value,
})),
conditions: [
...fieldsToConditions(CONFIG.fields),
{bucket: BUCKET.name},
...fieldsToConditions(requiredFields),
],
expiration: dateFormat.format(
new Date(CONFIG.expires),
'YYYY-MM-DD[T]HH:mm:ss[Z]',
Expand All @@ -2851,6 +2855,12 @@ describe('File', () => {
const EXPECTED_SIGNATURE = Buffer.from(SIGNATURE, 'base64').toString(
'hex'
);
const EXPECTED_FIELDS = {
...CONFIG.fields,
...requiredFields,
'x-goog-signature': EXPECTED_SIGNATURE,
policy: EXPECTED_POLICY,
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
file.generateSignedPostPolicyV4(
Expand All @@ -2859,11 +2869,7 @@ describe('File', () => {
assert.ifError(err);
assert(res.url, `${STORAGE_POST_POLICY_BASE_URL}/${BUCKET.name}`);

assert.deepStrictEqual(res.fields, {
...fields,
'x-goog-signature': EXPECTED_SIGNATURE,
policy: EXPECTED_POLICY,
});
assert.deepStrictEqual(res.fields, EXPECTED_FIELDS);

const signStub = BUCKET.storage.authClient.sign;
assert.deepStrictEqual(
Expand Down

0 comments on commit 555a950

Please sign in to comment.