@@ -4,6 +4,7 @@ const api = require('../../../lib/api/api');
4
4
const DummyRequest = require ( '../DummyRequest' ) ;
5
5
const { default : AuthInfo } = require ( 'arsenal/build/lib/auth/AuthInfo' ) ;
6
6
const assert = require ( 'assert' ) ;
7
+ const crypto = require ( 'crypto' ) ;
7
8
8
9
describe ( 'api.callApiMethod' , ( ) => {
9
10
let sandbox ;
@@ -49,6 +50,7 @@ describe('api.callApiMethod', () => {
49
50
sandbox . restore ( ) ;
50
51
} ) ;
51
52
53
+
52
54
it ( 'should attach apiMethod to request' , done => {
53
55
const testMethod = 'bucketGet' ;
54
56
api . callApiMethod ( testMethod , request , response , log , ( ) => {
@@ -112,4 +114,101 @@ describe('api.callApiMethod', () => {
112
114
( userInfo , _request , streamingV4Params , log , cb ) => cb ) ;
113
115
api . callApiMethod ( 'multipartDelete' , request , response , log ) ;
114
116
} ) ;
117
+
118
+ describe ( 'MD5 checksum validation' , ( ) => {
119
+ const methodsWithChecksumValidation = [
120
+ 'bucketPutACL' ,
121
+ 'bucketPutCors' ,
122
+ 'bucketPutEncryption' ,
123
+ 'bucketPutLifecycle' ,
124
+ 'bucketPutNotification' ,
125
+ 'bucketPutObjectLock' ,
126
+ 'bucketPutPolicy' ,
127
+ 'bucketPutReplication' ,
128
+ 'bucketPutVersioning' ,
129
+ 'bucketPutWebsite' ,
130
+ 'multiObjectDelete' ,
131
+ 'objectPutACL' ,
132
+ 'objectPutLegalHold' ,
133
+ 'objectPutTagging' ,
134
+ 'objectPutRetention'
135
+ ] ;
136
+
137
+ methodsWithChecksumValidation . forEach ( method => {
138
+ it ( `should return BadDigest for ${ method } when bad MD5 checksum is provided` , done => {
139
+ const body = '<xml></xml>' ;
140
+ const headers = {
141
+ 'content-md5' : 'badchecksum123=' , // Invalid MD5
142
+ 'content-length' : body . length . toString ( )
143
+ } ;
144
+
145
+ const requestWithBody = new DummyRequest ( {
146
+ headers,
147
+ query : { } ,
148
+ socket : { remoteAddress : '127.0.0.1' , destroy : sandbox . stub ( ) }
149
+ } , body ) ;
150
+
151
+ sandbox . stub ( api , method ) . callsFake ( ( ) => {
152
+ done ( new Error ( `${ method } was called despite bad checksum` ) ) ;
153
+ } ) ;
154
+
155
+ api . callApiMethod ( method , requestWithBody , response , log , err => {
156
+ assert ( err , `Expected error for ${ method } with bad checksum` ) ;
157
+ assert ( err . is . BadDigest , `Expected BadDigest error for ${ method } , got: ${ err . code } ` ) ;
158
+ done ( ) ;
159
+ } ) ;
160
+ } ) ;
161
+ } ) ;
162
+
163
+ methodsWithChecksumValidation . forEach ( method => {
164
+ it ( `should succeed for ${ method } when correct MD5 checksum is provided` , done => {
165
+ const body = '<xml></xml>' ;
166
+ const correctMd5 = crypto . createHash ( 'md5' ) . update ( body ) . digest ( 'base64' ) ;
167
+ const headers = {
168
+ 'content-md5' : correctMd5 ,
169
+ 'content-length' : body . length . toString ( )
170
+ } ;
171
+
172
+ const requestWithBody = new DummyRequest ( {
173
+ headers,
174
+ query : { } ,
175
+ socket : { remoteAddress : '127.0.0.1' , destroy : sandbox . stub ( ) }
176
+ } , body ) ;
177
+
178
+ sandbox . stub ( api , method ) . callsFake ( ( userInfo , _request , log , cb ) => {
179
+ cb ( ) ;
180
+ } ) ;
181
+
182
+ api . callApiMethod ( method , requestWithBody , response , log , err => {
183
+ assert . ifError ( err , `Unexpected error for ${ method } with good checksum: ${ err } ` ) ;
184
+ done ( ) ;
185
+ } ) ;
186
+ } ) ;
187
+ } ) ;
188
+
189
+ methodsWithChecksumValidation . forEach ( method => {
190
+ it ( `should succeed for ${ method } when no MD5 checksum is provided` , done => {
191
+ const body = '<xml></xml>' ;
192
+ const headers = {
193
+ 'content-md5' : '' ,
194
+ 'content-length' : body . length . toString ( )
195
+ } ;
196
+
197
+ const requestWithBody = new DummyRequest ( {
198
+ headers,
199
+ query : { } ,
200
+ socket : { remoteAddress : '127.0.0.1' , destroy : sandbox . stub ( ) }
201
+ } , body ) ;
202
+
203
+ sandbox . stub ( api , method ) . callsFake ( ( userInfo , _request , log , cb ) => {
204
+ cb ( ) ;
205
+ } ) ;
206
+
207
+ api . callApiMethod ( method , requestWithBody , response , log , err => {
208
+ assert ( ! err , `Unexpected error for ${ method } with no checksum: ${ err } ` ) ;
209
+ done ( ) ;
210
+ } ) ;
211
+ } ) ;
212
+ } ) ;
213
+ } ) ;
115
214
} ) ;
0 commit comments