@@ -37,6 +37,11 @@ visualCaptcha = {
37
37
numberOfOptions = 5 ;
38
38
}
39
39
40
+ // Set the minimum numberOfOptions to four
41
+ if ( numberOfOptions < 4 ) {
42
+ numberOfOptions = 4 ;
43
+ }
44
+
40
45
// Shuffle all imageOptions
41
46
this . imageOptions = _ . shuffle ( this . imageOptions ) ;
42
47
@@ -166,19 +171,37 @@ visualCaptcha = {
166
171
response . set ( 'pragma' , 'no-cache' ) ;
167
172
response . set ( 'expires' , 0 ) ;
168
173
169
- stream = fs . createReadStream ( audioFilePath )
170
- . pipe ( response ) ;
174
+ stream = fs . createReadStream ( audioFilePath ) ;
175
+ var responseData = [ ] ;
171
176
172
177
if ( stream ) {
178
+ stream . on ( 'data' , function ( chunk ) {
179
+ responseData . push ( chunk ) ;
180
+ } ) ;
181
+
173
182
stream . on ( 'end' , function ( ) {
174
183
if ( ! response . headerSent ) {
175
- response . status ( 200 ) . send ( 'Ok' ) ;
184
+ var finalData = Buffer . concat ( responseData ) ;
185
+ response . write ( finalData ) ;
186
+
187
+ // Add some noise randomly, so audio files can't be saved and matched easily by filesize or checksum
188
+ var noiseData = crypto . randomBytes ( Math . round ( ( Math . random ( ) * 1999 ) ) + 501 ) . toString ( 'hex' ) ;
189
+ response . write ( noiseData ) ;
190
+
191
+ response . end ( ) ;
176
192
}
177
193
} ) ;
178
194
179
195
stream . on ( 'close' , function ( ) {
180
196
if ( ! response . headerSent ) {
181
- response . status ( 200 ) . send ( 'Ok' ) ;
197
+ var finalData = Buffer . concat ( responseData ) ;
198
+ response . write ( finalData ) ;
199
+
200
+ // Add some noise randomly, so audio files can't be saved and matched easily by filesize or checksum
201
+ var noiseData = crypto . randomBytes ( Math . round ( ( Math . random ( ) * 1999 ) ) + 501 ) . toString ( 'hex' ) ;
202
+ response . write ( noiseData ) ;
203
+
204
+ response . end ( ) ;
182
205
}
183
206
} ) ;
184
207
} else {
@@ -233,19 +256,37 @@ visualCaptcha = {
233
256
response . set ( 'pragma' , 'no-cache' ) ;
234
257
response . set ( 'expires' , 0 ) ;
235
258
236
- stream = fs . createReadStream ( imageFilePath )
237
- . pipe ( response ) ;
238
-
259
+ stream = fs . createReadStream ( imageFilePath ) ;
260
+ var responseData = [ ] ;
261
+
239
262
if ( stream ) {
263
+ stream . on ( 'data' , function ( chunk ) {
264
+ responseData . push ( chunk ) ;
265
+ } ) ;
266
+
240
267
stream . on ( 'end' , function ( ) {
241
268
if ( ! response . headerSent ) {
242
- response . status ( 200 ) . send ( 'Ok' ) ;
269
+ var finalData = Buffer . concat ( responseData ) ;
270
+ response . write ( finalData ) ;
271
+
272
+ // Add some noise randomly, so images can't be saved and matched easily by filesize or checksum
273
+ var noiseData = crypto . randomBytes ( Math . round ( ( Math . random ( ) * 1999 ) ) + 501 ) . toString ( 'hex' ) ;
274
+ response . write ( noiseData ) ;
275
+
276
+ response . end ( ) ;
243
277
}
244
278
} ) ;
245
279
246
280
stream . on ( 'close' , function ( ) {
247
281
if ( ! response . headerSent ) {
248
- response . status ( 200 ) . send ( 'Ok' ) ;
282
+ var finalData = Buffer . concat ( responseData ) ;
283
+ response . write ( finalData ) ;
284
+
285
+ // Add some noise randomly, so images can't be saved and matched easily by filesize or checksum
286
+ var noiseData = crypto . randomBytes ( Math . round ( ( Math . random ( ) * 1999 ) ) + 501 ) . toString ( 'hex' ) ;
287
+ response . write ( noiseData ) ;
288
+
289
+ response . end ( ) ;
249
290
}
250
291
} ) ;
251
292
} else {
0 commit comments