From b8732c92db07a3e68bb2314e759f409157e93133 Mon Sep 17 00:00:00 2001 From: slefevre Date: Tue, 19 Dec 2017 14:46:54 +0100 Subject: fix double decoding --- src/sp_cookie_encryption.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sp_cookie_encryption.c b/src/sp_cookie_encryption.c index 5189c20..5c3c372 100644 --- a/src/sp_cookie_encryption.c +++ b/src/sp_cookie_encryption.c @@ -56,7 +56,7 @@ int decrypt_cookie(zval *pDest, int num_args, va_list args, generate_key(key); - value_len = php_url_decode(Z_STRVAL_P(pDest), Z_STRLEN_P(pDest)); + value_len = Z_STRLEN_P(pDest); if (value_len == 0) { return ZEND_HASH_APPLY_KEEP; -- cgit v1.3 From 560b6fc407237aa240e7a76691f6ab46562b0fa0 Mon Sep 17 00:00:00 2001 From: slefevre Date: Tue, 19 Dec 2017 14:50:09 +0100 Subject: remove useless var --- src/sp_cookie_encryption.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/sp_cookie_encryption.c b/src/sp_cookie_encryption.c index 5c3c372..9e585c5 100644 --- a/src/sp_cookie_encryption.c +++ b/src/sp_cookie_encryption.c @@ -42,7 +42,6 @@ static inline void generate_key(unsigned char *key) { int decrypt_cookie(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key) { unsigned char key[crypto_secretbox_KEYBYTES] = {0}; - size_t value_len; zend_string *debase64; unsigned char *decrypted; sp_cookie *cookie = zend_hash_find_ptr(SNUFFLEUPAGUS_G(config).config_cookie->cookies, @@ -56,13 +55,11 @@ int decrypt_cookie(zval *pDest, int num_args, va_list args, generate_key(key); - value_len = Z_STRLEN_P(pDest); - - if (value_len == 0) { + if (Z_STRLEN_P(pDest) == 0) { return ZEND_HASH_APPLY_KEEP; } - debase64 = php_base64_decode((unsigned char *)(Z_STRVAL_P(pDest)), value_len); + debase64 = php_base64_decode((unsigned char *)(Z_STRVAL_P(pDest)), Z_STRLEN_P(pDest)); if (ZSTR_LEN(debase64) < crypto_secretbox_NONCEBYTES + crypto_secretbox_ZEROBYTES) { -- cgit v1.3 From 451d23a2c67694d3ac7ecb602c34da23a227f1f9 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 19 Dec 2017 14:54:47 +0100 Subject: Rework a bit the order of operation - There is no need to generate the key if the cookie has no value - There is no need to generate the key if the cookie length is invalid - Use yoda condition --- src/sp_cookie_encryption.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/sp_cookie_encryption.c b/src/sp_cookie_encryption.c index 9e585c5..2bb305f 100644 --- a/src/sp_cookie_encryption.c +++ b/src/sp_cookie_encryption.c @@ -53,13 +53,13 @@ int decrypt_cookie(zval *pDest, int num_args, va_list args, return ZEND_HASH_APPLY_KEEP; } - generate_key(key); - - if (Z_STRLEN_P(pDest) == 0) { + /* If the cookie has no value, it shouldn't be encrypted. */ + if (0 == Z_STRLEN_P(pDest)) { return ZEND_HASH_APPLY_KEEP; } - debase64 = php_base64_decode((unsigned char *)(Z_STRVAL_P(pDest)), Z_STRLEN_P(pDest)); + debase64 = php_base64_decode((unsigned char *)(Z_STRVAL_P(pDest)), + Z_STRLEN_P(pDest)); if (ZSTR_LEN(debase64) < crypto_secretbox_NONCEBYTES + crypto_secretbox_ZEROBYTES) { @@ -68,6 +68,8 @@ int decrypt_cookie(zval *pDest, int num_args, va_list args, return ZEND_HASH_APPLY_REMOVE; } + generate_key(key); + decrypted = pecalloc(ZSTR_LEN(debase64), 1, 0); ret = crypto_secretbox_open( -- cgit v1.3