@@ -4,6 +4,7 @@ const api = require('../../../lib/api/api');
44const DummyRequest = require ( '../DummyRequest' ) ;
55const { default : AuthInfo } = require ( 'arsenal/build/lib/auth/AuthInfo' ) ;
66const assert = require ( 'assert' ) ;
7+ const crypto = require ( 'crypto' ) ;
78
89describe ( 'api.callApiMethod' , ( ) => {
910 let sandbox ;
@@ -49,6 +50,7 @@ describe('api.callApiMethod', () => {
4950 sandbox . restore ( ) ;
5051 } ) ;
5152
53+
5254 it ( 'should attach apiMethod to request' , done => {
5355 const testMethod = 'bucketGet' ;
5456 api . callApiMethod ( testMethod , request , response , log , ( ) => {
@@ -112,4 +114,101 @@ describe('api.callApiMethod', () => {
112114 ( userInfo , _request , streamingV4Params , log , cb ) => cb ) ;
113115 api . callApiMethod ( 'multipartDelete' , request , response , log ) ;
114116 } ) ;
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 fail for ${ method } when empty 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 , `expected error for ${ method } with no checksum` ) ;
209+ done ( ) ;
210+ } ) ;
211+ } ) ;
212+ } ) ;
213+ } ) ;
115214} ) ;
0 commit comments