Skip to content

Commit 3f26b79

Browse files
taotao7PeterRao
andauthored
fix: fix user input header MD5 value being overwritten problem (#1100)
* fix: 修复header MD5值被覆盖问题 * fix: fix user input header MD5 value being overwritten problem Co-authored-by: Undefined <peizerao@gmail.com>
1 parent 1697cc3 commit 3f26b79

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

lib/common/utils/createRequest.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function createRequest(params) {
2424
date = +new Date() + this.options.amendTimeSkewed;
2525
}
2626
const headers = {
27-
'x-oss-date': dateFormat(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
27+
'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'")
2828
};
2929
if (typeof window !== 'undefined') {
3030
headers['x-oss-user-agent'] = this.userAgent;
@@ -52,10 +52,12 @@ function createRequest(params) {
5252
}
5353
if (params.content) {
5454
if (!params.disabledMD5) {
55-
headers['Content-MD5'] = crypto
56-
.createHash('md5')
57-
.update(Buffer.from(params.content, 'utf8'))
58-
.digest('base64');
55+
if (!params.headers || !params.headers['Content-MD5']) {
56+
headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64');
57+
}
58+
else {
59+
headers['Content-MD5'] = params.headers['Content-MD5'];
60+
}
5961
}
6062
if (!headers['Content-Length']) {
6163
headers['Content-Length'] = params.content.length;

lib/common/utils/createRequest.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const { setRegion } = require('./setRegion');
1010
const { getReqUrl } = require('../client/getReqUrl');
1111

1212
interface Headers {
13-
[propName: string]: any
14-
'x-oss-date': string,
15-
'x-oss-user-agent'?: string,
13+
[propName: string]: any;
14+
'x-oss-date': string;
15+
'x-oss-user-agent'?: string;
1616
}
1717

1818
interface ReqParams {
19-
[propName: string]: any
19+
[propName: string]: any;
2020
}
2121

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

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

7070
if (params.content) {
7171
if (!params.disabledMD5) {
72-
headers['Content-MD5'] = crypto
73-
.createHash('md5')
74-
.update(Buffer.from(params.content, 'utf8'))
75-
.digest('base64');
72+
if (!params.headers || !params.headers['Content-MD5']) {
73+
headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64');
74+
} else {
75+
headers['Content-MD5'] = params.headers['Content-MD5'];
76+
}
7677
}
7778
if (!headers['Content-Length']) {
7879
headers['Content-Length'] = params.content.length;
@@ -87,7 +88,13 @@ export function createRequest(this: any, params) {
8788
}
8889

8990
const authResource = this._getResource(params);
90-
headers.authorization = this.authorization(params.method, authResource, params.subres, headers, this.options.headerEncoding);
91+
headers.authorization = this.authorization(
92+
params.method,
93+
authResource,
94+
params.subres,
95+
headers,
96+
this.options.headerEncoding
97+
);
9198

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

0 commit comments

Comments
 (0)