Skip to content

Commit 4b01cd1

Browse files
authored
ext/session/mod_mm: implement VALIDATE_SID handler (php#21178)
Rather than "manually" doing it in the READ handler. At the same time, get rid of various inconsistent legacy handler macro definitions, thus mandating all modules to implement the create and validate SID handlers. The only handler that remains optional is the update timestamp one.
1 parent a760cf7 commit 4b01cd1

3 files changed

Lines changed: 32 additions & 43 deletions

File tree

UPGRADING.INTERNALS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ PHP 8.6 INTERNALS UPGRADE NOTES
100100
`void **mod_data, zend_string *save_path, zend_string *session_name`
101101
rather than
102102
`void **mod_data, const char *save_path, const char *session_name`
103+
. PS_FUNCS() now includes the PS_VALIDATE_SID_FUNC()
104+
. PS_MOD() now requires that the PS_CREATE_SID_FUNC() and
105+
PS_VALIDATE_SID_FUNC() functions are defined.
106+
. PS_FUNCS_SID() and PS_MOD_SID() have been removed.
107+
Either use PS_FUNCS()/PS_MOD() or PS_FUNCS_UPDATE_TIMESTAMP()/
108+
PS_MOD_UPDATE_TIMESTAMP() if timestamp support exists.
103109

104110
- ext/standard:
105111
. _php_error_log() now has a formal return type of zend_result.

ext/session/mod_mm.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static zend_result ps_mm_key_exists(ps_mm *data, const zend_string *key)
215215
}
216216

217217
const ps_module ps_mod_mm = {
218-
PS_MOD_SID(mm)
218+
PS_MOD(mm)
219219
};
220220

221221
#define PS_MM_DATA ps_mm *data = PS_GET_MOD_DATA()
@@ -346,26 +346,7 @@ PS_READ_FUNC(mm)
346346

347347
mm_lock(data->mm, MM_LOCK_RD);
348348

349-
/* If there is an ID and strict mode, verify existence */
350-
if (PS(use_strict_mode)
351-
&& ps_mm_key_exists(data, key) == FAILURE) {
352-
/* key points to PS(id), but cannot change here. */
353-
if (key) {
354-
efree(PS(id));
355-
PS(id) = NULL;
356-
}
357-
PS(id) = PS(mod)->s_create_sid((void **)&data);
358-
if (!PS(id)) {
359-
return FAILURE;
360-
}
361-
if (PS(use_cookies)) {
362-
PS(send_cookie) = true;
363-
}
364-
php_session_reset_id();
365-
PS(session_status) = php_session_active;
366-
}
367-
368-
sd = ps_sd_lookup(data, PS(id), false);
349+
sd = ps_sd_lookup(data, key, false);
369350
if (sd) {
370351
*val = zend_string_init(sd->data, sd->datalen, false);
371352
ret = SUCCESS;
@@ -488,4 +469,23 @@ PS_CREATE_SID_FUNC(mm)
488469
return sid;
489470
}
490471

472+
/*
473+
* Check session ID existence for use_strict_mode support.
474+
* PARAMETERS: PS_VALIDATE_SID_ARGS in php_session.h
475+
* RETURN VALUE: SUCCESS or FAILURE.
476+
*
477+
* Return SUCCESS for valid key(already existing session).
478+
* Return FAILURE for invalid key(non-existing session).
479+
* *mod_data, *key are guaranteed to have non-NULL values.
480+
*/
481+
PS_VALIDATE_SID_FUNC(mm)
482+
{
483+
PS_MM_DATA;
484+
485+
mm_lock(data->mm, MM_LOCK_RD);
486+
zend_result ret = ps_mm_key_exists(data, key)
487+
mm_unlock(data->mm);
488+
return ret;
489+
}
490+
491491
#endif

ext/session/php_session.h

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,40 +62,23 @@ typedef struct ps_module_struct {
6262
#define PS_VALIDATE_SID_FUNC(x) zend_result ps_validate_sid_##x(PS_VALIDATE_SID_ARGS)
6363
#define PS_UPDATE_TIMESTAMP_FUNC(x) zend_result ps_update_timestamp_##x(PS_UPDATE_TIMESTAMP_ARGS)
6464

65-
/* Legacy save handler module definitions */
65+
/* Save handler module definitions without timestamp enabled */
6666
#define PS_FUNCS(x) \
6767
PS_OPEN_FUNC(x); \
6868
PS_CLOSE_FUNC(x); \
6969
PS_READ_FUNC(x); \
7070
PS_WRITE_FUNC(x); \
7171
PS_DESTROY_FUNC(x); \
7272
PS_GC_FUNC(x); \
73-
PS_CREATE_SID_FUNC(x)
73+
PS_CREATE_SID_FUNC(x) \
74+
PS_VALIDATE_SID_FUNC(x);
7475

7576
#define PS_MOD(x) \
76-
#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
77-
ps_delete_##x, ps_gc_##x, php_session_create_id, \
78-
php_session_validate_sid, php_session_update_timestamp
79-
80-
/* Legacy SID creation enabled save handler module definitions */
81-
#define PS_FUNCS_SID(x) \
82-
PS_OPEN_FUNC(x); \
83-
PS_CLOSE_FUNC(x); \
84-
PS_READ_FUNC(x); \
85-
PS_WRITE_FUNC(x); \
86-
PS_DESTROY_FUNC(x); \
87-
PS_GC_FUNC(x); \
88-
PS_CREATE_SID_FUNC(x); \
89-
PS_VALIDATE_SID_FUNC(x); \
90-
PS_UPDATE_TIMESTAMP_FUNC(x);
91-
92-
#define PS_MOD_SID(x) \
9377
#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
9478
ps_delete_##x, ps_gc_##x, ps_create_sid_##x, \
95-
php_session_validate_sid, php_session_update_timestamp
79+
ps_validate_sid_##x, NULL
9680

97-
/* Update timestamp enabled save handler module definitions
98-
New save handlers should use this API */
81+
/* Save handlers with timestamp enabled, it is recommended to use this API */
9982
#define PS_FUNCS_UPDATE_TIMESTAMP(x) \
10083
PS_OPEN_FUNC(x); \
10184
PS_CLOSE_FUNC(x); \

0 commit comments

Comments
 (0)