@@ -231,14 +231,21 @@ zend_bool s_memc_valid_key_binary(zend_string *key)
231
231
}
232
232
233
233
static
234
- zend_bool s_memc_valid_key_ascii (zend_string * key )
234
+ zend_bool s_memc_valid_key_ascii (zend_string * key , uint64_t verify_key )
235
235
{
236
236
const char * str = ZSTR_VAL (key );
237
237
size_t i , len = ZSTR_LEN (key );
238
238
239
- for (i = 0 ; i < len ; i ++ ) {
240
- if (!isgraph (str [i ]) || isspace (str [i ]))
241
- return 0 ;
239
+ if (verify_key ) {
240
+ for (i = 0 ; i < len ; i ++ ) {
241
+ if (!isgraph (str [i ]) || isspace (str [i ]))
242
+ return 0 ;
243
+ }
244
+ } else { /* if key verification is disabled, only check for spaces to avoid injection issues */
245
+ for (i = 0 ; i < len ; i ++ ) {
246
+ if (isspace (str [i ]))
247
+ return 0 ;
248
+ }
242
249
}
243
250
return 1 ;
244
251
}
@@ -248,7 +255,7 @@ zend_bool s_memc_valid_key_ascii(zend_string *key)
248
255
ZSTR_LEN(key) > MEMC_OBJECT_KEY_MAX_LENGTH || \
249
256
(memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) \
250
257
? !s_memc_valid_key_binary(key) \
251
- : ( memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_VERIFY_KEY) && !s_memc_valid_key_ascii(key )) \
258
+ : !s_memc_valid_key_ascii(key, memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_VERIFY_KEY)) \
252
259
))) { \
253
260
intern->rescode = MEMCACHED_BAD_KEY_PROVIDED; \
254
261
RETURN_FALSE; \
@@ -342,7 +349,7 @@ PHP_INI_MH(OnUpdateSessionPrefixString)
342
349
php_error_docref (NULL , E_WARNING , "memcached.sess_prefix too long (max: %d)" , MEMCACHED_MAX_KEY - 1 );
343
350
return FAILURE ;
344
351
}
345
- if (!s_memc_valid_key_ascii (new_value )) {
352
+ if (!s_memc_valid_key_ascii (new_value , 1 )) {
346
353
php_error_docref (NULL , E_WARNING , "memcached.sess_prefix cannot contain whitespace or control characters" );
347
354
return FAILURE ;
348
355
}
0 commit comments