Skip to content

Commit 802f9ba

Browse files
committed
Added ValueError and reverted other changes
1 parent c104f04 commit 802f9ba

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

ext/session/mod_files.c

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ static void ps_files_open(ps_files *data, /* const */ zend_string *key)
166166
ps_files_close(data);
167167

168168
if (php_session_valid_key(ZSTR_VAL(key)) == FAILURE) {
169+
php_error_docref(NULL, E_WARNING, "Session ID is too long or contains illegal characters. Only the A-Z, a-z, 0-9, \"-\", and \",\" characters are allowed");
169170
return;
170171
}
171172

ext/session/session.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ PHPAPI zend_result php_session_valid_key(const char *key) /* {{{ */
368368
|| (c >= '0' && c <= '9')
369369
|| c == ','
370370
|| c == '-')) {
371-
php_error_docref(NULL, E_WARNING, "Prefix or name cannot contain special characters. Only the A-Z, a-z, 0-9, \"-\", and \",\" characters are allowed");
372371
return FAILURE;
373372
}
374373
}
@@ -378,7 +377,6 @@ PHPAPI zend_result php_session_valid_key(const char *key) /* {{{ */
378377
/* Somewhat arbitrary length limit here, but should be way more than
379378
anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */
380379
if (len == 0 || len > PS_MAX_SID_LENGTH) {
381-
php_error_docref(NULL, E_WARNING, "Prefix or name cannot be larger than 256 characters");
382380
return FAILURE;
383381
}
384382

@@ -2385,8 +2383,13 @@ PHP_FUNCTION(session_create_id)
23852383
}
23862384

23872385
if (prefix && ZSTR_LEN(prefix)) {
2386+
if (ZSTR_LEN(prefix) > PS_MAX_SID_LENGTH) {
2387+
zend_argument_value_error(1, "cannot be longer than %d characters", PS_MAX_SID_LENGTH);
2388+
RETURN_THROWS();
2389+
}
23882390
if (php_session_valid_key(ZSTR_VAL(prefix)) == FAILURE) {
23892391
/* E_ERROR raised for security reason. */
2392+
php_error_docref(NULL, E_WARNING, "Prefix cannot contain special characters. Only the A-Z, a-z, 0-9, \"-\", and \",\" characters are allowed");
23902393
RETURN_FALSE;
23912394
} else {
23922395
smart_str_append(&id, prefix);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-----------------------------20896060251896012921717172737
2+
Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"
3+
4+
rfc1867_sid_invalid.php
5+
-----------------------------20896060251896012921717172737
6+
Content-Disposition: form-data; name="file1"; filename="file1.txt"
7+
8+
1
9+
-----------------------------20896060251896012921717172737
10+
Content-Disposition: form-data; name="file2"; filename="file2.txt"
11+
12+
2
13+
-----------------------------20896060251896012921717172737--

ext/session/tests/session_create_id_invalid_prefix.phpt

+5-8
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,20 @@ session
1212

1313
var_dump(session_create_id('_'));
1414
var_dump(session_create_id('%'));
15-
var_dump(session_create_id('ABTgdPs68S3M4HMaqKwj33TzqLMv5PHpWQxJbfpeogEhrJRY7o9f33pKLCmhf0tXCtoBkIu0yxXYCSHfJhPd2miPUW4MIpd91dnEiOwWDfaBnfdJZOwgvgmYLSfDGaebqmnCAoyuzlcq2j59nNRhccgJIkr9ytY3RwFTTXszpcjpx6mlJuG9GksKAhPsnnaEwSEb0eFyqvn80gYI2roKSjaFSmJxg0xgXuCF4csMo8DxiSvovho5QTKx5u7h8VyQL'));
1615
try {
16+
var_dump(session_create_id('ABTgdPs68S3M4HMaqKwj33TzqLMv5PHpWQxJbfpeogEhrJRY7o9f33pKLCmhf0tXCtoBkIu0yxXYCSHfJhPd2miPUW4MIpd91dnEiOwWDfaBnfdJZOwgvgmYLSfDGaebqmnCAoyuzlcq2j59nNRhccgJIkr9ytY3RwFTTXszpcjpx6mlJuG9GksKAhPsnnaEwSEb0eFyqvn80gYI2roKSjaFSmJxg0xgXuCF4csMo8DxiSvovho5QTKx5u7h8VyQL'));
1717
var_dump(session_create_id("AB\0CD"));
1818
} catch (Throwable $e) {
19-
echo $e->getMessage() . "\n";
19+
echo $e::class . ': ' . $e->getMessage() . "\n";
2020
}
2121

2222
?>
2323
Done
2424
--EXPECTF--
25-
Warning: session_create_id(): Prefix or name cannot contain special characters. Only the A-Z, a-z, 0-9, "-", and "," characters are allowed in %s on line %d
25+
Warning: session_create_id(): Prefix cannot contain special characters. Only the A-Z, a-z, 0-9, "-", and "," characters are allowed in %s on line %d
2626
bool(false)
2727

28-
Warning: session_create_id(): Prefix or name cannot contain special characters. Only the A-Z, a-z, 0-9, "-", and "," characters are allowed in %s on line %d
28+
Warning: session_create_id(): Prefix cannot contain special characters. Only the A-Z, a-z, 0-9, "-", and "," characters are allowed in %s on line %d
2929
bool(false)
30-
31-
Warning: session_create_id(): Prefix or name cannot be larger than 256 characters in %s on line %d
32-
bool(false)
33-
session_create_id(): Argument #1 ($prefix) must not contain any null bytes
30+
ValueError: session_create_id(): Argument #1 ($prefix) cannot be longer than 256 characters
3431
Done

0 commit comments

Comments
 (0)