File tree Expand file tree Collapse file tree 2 files changed +37
-6
lines changed
packages/s3-request-presigner/src Expand file tree Collapse file tree 2 files changed +37
-6
lines changed Original file line number Diff line number Diff line change @@ -116,4 +116,34 @@ describe("s3 presigner", () => {
116
116
host : `${ minimalRequest . hostname } :${ port } ` ,
117
117
} ) ;
118
118
} ) ;
119
+
120
+ it ( "should inject host header with port if current host header missing port" , async ( ) => {
121
+ const signer = new S3RequestPresigner ( s3ResolvedConfig ) ;
122
+ const port = 12345 ;
123
+ const signed = await signer . presign ( {
124
+ ...minimalRequest ,
125
+ headers : {
126
+ host : minimalRequest . hostname ,
127
+ } ,
128
+ port,
129
+ } ) ;
130
+ expect ( signed . headers ) . toMatchObject ( {
131
+ host : `${ minimalRequest . hostname } :${ port } ` ,
132
+ } ) ;
133
+ } ) ;
134
+
135
+ it ( "should NOT overwrite host header if different host header is already set" , async ( ) => {
136
+ const signer = new S3RequestPresigner ( s3ResolvedConfig ) ;
137
+ const port = 12345 ;
138
+ const signed = await signer . presign ( {
139
+ ...minimalRequest ,
140
+ headers : {
141
+ host : "proxy." + minimalRequest . hostname ,
142
+ } ,
143
+ port,
144
+ } ) ;
145
+ expect ( signed . headers ) . toMatchObject ( {
146
+ host : "proxy." + minimalRequest . hostname ,
147
+ } ) ;
148
+ } ) ;
119
149
} ) ;
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ export class S3RequestPresigner implements RequestPresigner {
24
24
this . signer = new SignatureV4MultiRegion ( resolvedOptions ) ;
25
25
}
26
26
27
- public async presign (
27
+ public presign (
28
28
requestToSign : IHttpRequest ,
29
29
{ unsignableHeaders = new Set ( ) , unhoistableHeaders = new Set ( ) , ...options } : RequestPresigningArguments = { }
30
30
) : Promise < IHttpRequest > {
@@ -38,11 +38,12 @@ export class S3RequestPresigner implements RequestPresigner {
38
38
unhoistableHeaders . add ( header ) ;
39
39
} ) ;
40
40
requestToSign . headers [ SHA256_HEADER ] = UNSIGNED_PAYLOAD ;
41
- if ( ! requestToSign . headers [ "host" ] ) {
42
- requestToSign . headers . host = requestToSign . hostname ;
43
- if ( requestToSign . port ) {
44
- requestToSign . headers . host = `${ requestToSign . headers . host } :${ requestToSign . port } ` ;
45
- }
41
+
42
+ const currentHostHeader = requestToSign . headers . host ;
43
+ const port = requestToSign . port ;
44
+ const expectedHostHeader = `${ requestToSign . hostname } ${ requestToSign . port != null ? ":" + port : "" } ` ;
45
+ if ( ! currentHostHeader || ( currentHostHeader === requestToSign . hostname && requestToSign . port != null ) ) {
46
+ requestToSign . headers . host = expectedHostHeader ;
46
47
}
47
48
return this . signer . presign ( requestToSign , {
48
49
expiresIn : 900 ,
You can’t perform that action at this time.
0 commit comments