Skip to content

Commit

Permalink
fix: fix user input header MD5 value being overwritten problem (#1100)
Browse files Browse the repository at this point in the history
* fix: 修复header MD5值被覆盖问题

* fix: fix user input header MD5 value being overwritten problem

Co-authored-by: Undefined <peizerao@gmail.com>
  • Loading branch information
taotao7 and PeterRao authored Jun 8, 2022
1 parent 1697cc3 commit 3f26b79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
12 changes: 7 additions & 5 deletions lib/common/utils/createRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function createRequest(params) {
date = +new Date() + this.options.amendTimeSkewed;
}
const headers = {
'x-oss-date': dateFormat(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'")
};
if (typeof window !== 'undefined') {
headers['x-oss-user-agent'] = this.userAgent;
Expand Down Expand Up @@ -52,10 +52,12 @@ function createRequest(params) {
}
if (params.content) {
if (!params.disabledMD5) {
headers['Content-MD5'] = crypto
.createHash('md5')
.update(Buffer.from(params.content, 'utf8'))
.digest('base64');
if (!params.headers || !params.headers['Content-MD5']) {
headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64');
}
else {
headers['Content-MD5'] = params.headers['Content-MD5'];
}
}
if (!headers['Content-Length']) {
headers['Content-Length'] = params.content.length;
Expand Down
27 changes: 17 additions & 10 deletions lib/common/utils/createRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const { setRegion } = require('./setRegion');
const { getReqUrl } = require('../client/getReqUrl');

interface Headers {
[propName: string]: any
'x-oss-date': string,
'x-oss-user-agent'?: string,
[propName: string]: any;
'x-oss-date': string;
'x-oss-user-agent'?: string;
}

interface ReqParams {
[propName: string]: any
[propName: string]: any;
}

function getHeader(headers: Headers, name: string) {
Expand All @@ -34,7 +34,7 @@ export function createRequest(this: any, params) {
date = +new Date() + this.options.amendTimeSkewed;
}
const headers: Headers = {
'x-oss-date': dateFormat(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'")
};

if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -69,10 +69,11 @@ export function createRequest(this: any, params) {

if (params.content) {
if (!params.disabledMD5) {
headers['Content-MD5'] = crypto
.createHash('md5')
.update(Buffer.from(params.content, 'utf8'))
.digest('base64');
if (!params.headers || !params.headers['Content-MD5']) {
headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64');
} else {
headers['Content-MD5'] = params.headers['Content-MD5'];
}
}
if (!headers['Content-Length']) {
headers['Content-Length'] = params.content.length;
Expand All @@ -87,7 +88,13 @@ export function createRequest(this: any, params) {
}

const authResource = this._getResource(params);
headers.authorization = this.authorization(params.method, authResource, params.subres, headers, this.options.headerEncoding);
headers.authorization = this.authorization(
params.method,
authResource,
params.subres,
headers,
this.options.headerEncoding
);

// const url = this._getReqUrl(params);
if (isIP(this.options.endpoint.hostname)) {
Expand Down

0 comments on commit 3f26b79

Please sign in to comment.