Skip to content

Commit 4b5decd

Browse files
authored
Merge branch 'master' into check_key_fix
2 parents 7f31d86 + d15c2e2 commit 4b5decd

9 files changed

+109
-40
lines changed

.github/workflows/build-and-test.yml

+4-19
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
php: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
10+
php: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
1111
experimental: [false]
1212
include:
13-
- php: '8.2'
14-
awesome: true
15-
experimental: true
16-
- php: '8.3'
13+
- php: '8.4'
1714
experimental: true
1815
steps:
1916
- name: Checkout
@@ -29,20 +26,8 @@ jobs:
2926
run: |
3027
sudo apt-get update
3128
sudo apt-get install cmake memcached libsasl2-dev sasl2-bin zlib1g-dev
32-
- if: ${{ ! matrix.awesome }}
33-
name: Install libmemcached-dev (from distro)
29+
- name: Install libmemcached-dev
3430
run: sudo apt-get install libmemcached-dev
35-
- if: ${{ matrix.awesome }}
36-
name: Install libmemcached-awesome (from source)
37-
run: |
38-
curl -sL -o libmemcached.tgz https://github.com/awesomized/libmemcached/archive/refs/tags/1.1.3.tar.gz
39-
mkdir libmemcached
40-
tar --strip-components=1 -xf libmemcached.tgz -C libmemcached
41-
mkdir build-libmemcached
42-
cd build-libmemcached
43-
cmake -D ENABLE_HASH_HSIEH=ON -D ENABLE_SASL=ON ../libmemcached
44-
make
45-
sudo make install
4631
- name: Start memcached daemons
4732
run: |
4833
export SASL_CONF_PATH="/tmp/sasl2"
@@ -83,7 +68,7 @@ jobs:
8368
8469
define ("MEMC_SASL_SERVER_HOST", "127.0.0.1");
8570
define ("MEMC_SASL_SERVER_PORT", 11212);
86-
71+
8772
define ('MEMC_SASL_USER', 'memcached');
8873
define ('MEMC_SASL_PASS', 'test');
8974
EOF

.github/workflows/build-windows.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
shell: cmd
88
strategy:
99
matrix:
10-
version: ['7.4', '8.0', '8.1']
10+
version: ['8.3']
1111
arch: [x64, x86]
1212
ts: [nts, zts]
1313
runs-on: windows-latest
@@ -16,7 +16,7 @@ jobs:
1616
uses: actions/checkout@v4
1717
- name: Setup PHP
1818
id: setup-php
19-
uses: cmb69/setup-php-sdk@v0.7
19+
uses: php/setup-php-sdk@v0.9
2020
with:
2121
version: ${{matrix.version}}
2222
arch: ${{matrix.arch}}

README.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Build Status
22
------------
3-
[![Build Status](https://travis-ci.org/php-memcached-dev/php-memcached.png)](https://travis-ci.org/php-memcached-dev/php-memcached)
3+
![Build Status](https://github.com/php-memcached-dev/php-memcached/actions/workflows/build-and-test.yml/badge.svg?branch=master)
44

55
Description
66
-----------
@@ -23,7 +23,7 @@ Dependencies
2323
------------
2424

2525
php-memcached 3.x:
26-
* Supports PHP 7.0 - 8.2 or higher.
26+
* Supports PHP 7.0 - 8.3 or higher.
2727
* Requires libmemcached 1.x or higher.
2828
* Optionally supports igbinary 2.0 or higher.
2929
* Optionally supports msgpack 2.0 or higher.

composer.json

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"name": "php-memcached/php-memcached",
3+
"type": "php-ext",
4+
"license": "PHP-3.01",
5+
"description": "memcached extension based on libmemcached library ",
6+
"require": {
7+
"php": ">= 7.0.0"
8+
},
9+
"suggest": {
10+
"ext-igbinary": "igbinary is a faster and more compact binary serializer for PHP data structures.",
11+
"ext-msgpack": "msgpack is a faster and more compact data structure representation that is interoperable with msgpack implementations for other languages."
12+
},
13+
"php-ext": {
14+
"extension-name": "memcached",
15+
"configure-options": [
16+
{
17+
"name": "enable-memcached",
18+
"description": "Enable memcached support"
19+
},
20+
{
21+
"name": "with-libmemcached-dir",
22+
"description": "Set the path to libmemcached install prefix.",
23+
"needs-value": true
24+
},
25+
{
26+
"name": "enable-memcached-session",
27+
"description": "Enable memcached session handler support"
28+
},
29+
{
30+
"name": "enable-memcached-igbinary",
31+
"description": "Enable memcached igbinary serializer support"
32+
},
33+
{
34+
"name": "enable-memcached-json",
35+
"description": "Enable memcached json serializer support"
36+
},
37+
{
38+
"name": "enable-memcached-msgpack",
39+
"description": "Enable memcached msgpack serializer support"
40+
},
41+
{
42+
"name": "enable-memcached-sasl",
43+
"description": "Enable memcached sasl support"
44+
},
45+
{
46+
"name": "enable-memcached-protocol",
47+
"description": "Enable memcached protocol support"
48+
},
49+
{
50+
"name": "with-system-fastlz",
51+
"description": "Use system FastLZ library"
52+
},
53+
{
54+
"name": "with-zstd",
55+
"description": "Use system zstd library"
56+
},
57+
{
58+
"name": "with-zlib-dir",
59+
"description": "Set the path to ZLIB install prefix.",
60+
"needs-value": true
61+
},
62+
{
63+
"name": "enable-debug",
64+
"description": "Compile with debugging symbols"
65+
}
66+
]
67+
}
68+
}

php_memcached.c

+20-10
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,21 @@ uint32_t s_memc_object_key_max_length(php_memc_object_t *intern) {
243243
}
244244
}
245245

246-
static
247-
zend_bool s_memc_valid_key_ascii(zend_string *key)
246+
zend_bool s_memc_valid_key_ascii(zend_string *key, uint64_t verify_key)
248247
{
249248
const char *str = ZSTR_VAL(key);
250249
size_t i, len = ZSTR_LEN(key);
251250

252-
for (i = 0; i < len; i++) {
253-
if (!isgraph(str[i]) || isspace(str[i]))
254-
return 0;
251+
if (verify_key) {
252+
for (i = 0; i < len; i++) {
253+
if (!isgraph(str[i]) || isspace(str[i]))
254+
return 0;
255+
}
256+
} else { /* if key verification is disabled, only check for spaces to avoid injection issues */
257+
for (i = 0; i < len; i++) {
258+
if (isspace(str[i]))
259+
return 0;
260+
}
255261
}
256262
return 1;
257263
}
@@ -261,7 +267,7 @@ zend_bool s_memc_valid_key_ascii(zend_string *key)
261267
ZSTR_LEN(key) > s_memc_object_key_max_length(intern) || \
262268
(memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) \
263269
? !s_memc_valid_key_binary(key) \
264-
: !s_memc_valid_key_ascii(key) \
270+
: !s_memc_valid_key_ascii(key, memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_VERIFY_KEY)) \
265271
))) { \
266272
intern->rescode = MEMCACHED_BAD_KEY_PROVIDED; \
267273
RETURN_FALSE; \
@@ -355,7 +361,7 @@ PHP_INI_MH(OnUpdateSessionPrefixString)
355361
php_error_docref(NULL, E_WARNING, "memcached.sess_prefix too long (max: %d)", MEMCACHED_MAX_KEY - 1);
356362
return FAILURE;
357363
}
358-
if (!s_memc_valid_key_ascii(new_value)) {
364+
if (!s_memc_valid_key_ascii(new_value, 1)) {
359365
php_error_docref(NULL, E_WARNING, "memcached.sess_prefix cannot contain whitespace or control characters");
360366
return FAILURE;
361367
}
@@ -946,17 +952,18 @@ zend_bool s_compress_value (php_memc_compression_type compression_type, zend_lon
946952

947953
case COMPRESSION_TYPE_ZLIB:
948954
{
949-
compressed_size = buffer_size;
955+
unsigned long cs = compressed_size = buffer_size;
950956

951957
if (compression_level < 0) {
952958
compression_level = 0;
953959
} else if (compression_level > 9) {
954960
compression_level = 9;
955961
}
956962

957-
int status = compress2((Bytef *) buffer, &compressed_size, (Bytef *) ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level);
963+
int status = compress2((Bytef *) buffer, &cs, (Bytef *) ZSTR_VAL(payload), ZSTR_LEN(payload), compression_level);
958964

959965
if (status == Z_OK) {
966+
compressed_size = cs;
960967
compress_status = 1;
961968
compression_type_flag = MEMC_VAL_COMPRESSION_ZLIB;
962969
}
@@ -3764,7 +3771,10 @@ zend_string *s_decompress_value (const char *payload, size_t payload_len, uint32
37643771
decompress_status = ((length = fastlz_decompress(payload, payload_len, &buffer->val, buffer->len)) > 0);
37653772
}
37663773
else if (is_zlib) {
3767-
decompress_status = (uncompress((Bytef *) buffer->val, &buffer->len, (Bytef *)payload, payload_len) == Z_OK);
3774+
unsigned long ds = buffer->len;
3775+
3776+
decompress_status = (uncompress((Bytef *) buffer->val, &ds, (Bytef *)payload, payload_len) == Z_OK);
3777+
buffer->len = ds;
37683778
}
37693779

37703780
ZSTR_VAL(buffer)[stored_length] = '\0';

tests/cas_invalid_key.phpt

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ Memcached::cas() with strange key
55
--FILE--
66
<?php
77
include dirname(__FILE__) . '/config.inc';
8-
$m = memc_get_instance ();
8+
$m = memc_get_instance (array (
9+
Memcached::OPT_BINARY_PROTOCOL => false,
10+
Memcached::OPT_VERIFY_KEY => true
11+
));
912

1013
error_reporting(0);
1114
var_dump($m->cas(0, '', true, 10));

tests/delete_bykey.phpt

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ Memcached::deleteByKey()
55
--FILE--
66
<?php
77
include dirname(__FILE__) . '/config.inc';
8-
$m = memc_get_instance ();
8+
$m = memc_get_instance (array (
9+
Memcached::OPT_BINARY_PROTOCOL => false,
10+
Memcached::OPT_VERIFY_KEY => true
11+
));
912

1013
$m->setByKey('keffe', 'eisaleeoo', "foo");
1114
var_dump($m->getByKey('keffe', 'eisaleeoo'));

tests/get.phpt

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ Memcached::get()
55
--FILE--
66
<?php
77
include dirname(__FILE__) . '/config.inc';
8-
$m = memc_get_instance ();
8+
$m = memc_get_instance (array (
9+
Memcached::OPT_BINARY_PROTOCOL => false,
10+
Memcached::OPT_VERIFY_KEY => true
11+
));
912

1013
$m->delete('foo');
1114

tests/keys_ascii.phpt

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ Test valid and invalid keys - ascii
88
include dirname (__FILE__) . '/config.inc';
99
$ascii = memc_get_instance (array (
1010
Memcached::OPT_BINARY_PROTOCOL => false,
11-
Memcached::OPT_VERIFY_KEY => false
11+
Memcached::OPT_VERIFY_KEY => true
1212
));
13-
// libmemcached can verify keys, but these are tests are for our own
14-
// function s_memc_valid_key_ascii, so explicitly disable the checks
15-
// that libmemcached can perform.
1613

1714
echo 'ASCII: SPACES' . PHP_EOL;
1815
var_dump ($ascii->set ('ascii key with spaces', 'this is a test'));

0 commit comments

Comments
 (0)