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

Ensure default expiration parameter is set for presigned URLs #1313

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
isBoolean,
isDefined,
isEmpty,
isFunction,
isNumber,
isObject,
isReadableStream,
Expand Down Expand Up @@ -2849,18 +2848,11 @@ export class TypedClient {
throw new errors.AnonymousRequestError(`Presigned ${method} url cannot be generated for anonymous requests`)
}

// Handle optional parameters and defaults
if (requestDate === undefined && isFunction(reqParams)) {
requestDate = new Date()
}
if (reqParams === undefined && isFunction(expires)) {
reqParams = {}
requestDate = new Date()
}
if (expires && typeof expires === 'function') {
if (!expires) {
expires = PRESIGN_EXPIRY_DAYS_MAX
}
if (!reqParams) {
reqParams = {}
requestDate = new Date()
}
if (!requestDate) {
requestDate = new Date()
Expand Down Expand Up @@ -2911,18 +2903,6 @@ export class TypedClient {
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
}
if (expires && isFunction(expires)) {
expires = PRESIGN_EXPIRY_DAYS_MAX
respHeaders = {}
requestDate = new Date()
}
if (!expires) {
expires = PRESIGN_EXPIRY_DAYS_MAX
}
if (isFunction(respHeaders)) {
respHeaders = {}
requestDate = new Date()
}

const validRespHeaders = [
'response-content-type',
Expand All @@ -2948,9 +2928,6 @@ export class TypedClient {
if (!isValidObjectName(objectName)) {
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
}
if (expires && isFunction(expires)) {
expires = PRESIGN_EXPIRY_DAYS_MAX
}

return this.presignedUrl('PUT', bucketName, objectName, expires)
}
Expand Down Expand Up @@ -2978,7 +2955,7 @@ export class TypedClient {
// 'expiration' is mandatory field for S3.
// Set default expiration date of 7 days.
const expires = new Date()
expires.setSeconds(24 * 60 * 60 * 7)
expires.setSeconds(PRESIGN_EXPIRY_DAYS_MAX)
postPolicy.setExpires(expires)
}

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,36 @@ describe('Client', function () {
})
})
describe('Presigned URL', () => {
describe('presignedUrl', () => {
const client = new Minio.Client({
endPoint: 'localhost',
port: 9000,
accessKey: 'accesskey',
secretKey: 'secretkey',
useSSL: false,
region: 'us-east-1',
})

it('should use the default expiry if omitted', async () => {
const url = await client.presignedUrl('get', 'bucket', 'object')
return expect(url).to.contain('X-Amz-Expires=604800')
})
it('should set the expiry time', async () => {
const url = await client.presignedUrl('get', 'bucket', 'object', 3600)
return expect(url).to.contain('X-Amz-Expires=3600')
})
it('should reject an invalid expiry time', async () => {
await expect(client.presignedUrl('get', 'bucket', 'object', 'invalid')).to.eventually.be.rejectedWith(
'expires should be of type "number"',
)
})
it('should handle a callback function', (done) => {
client.presignedUrl('get', 'bucket', 'object', (error, result) => {
expect(result).to.contain('X-Amz-Expires=604800')
done()
})
})
})
describe('presigned-get', () => {
it('should not generate presigned url with no access key', async () => {
try {
Expand Down
Loading