Skip to content

Commit 5db2194

Browse files
committed
Ensure to truncate any extra data
Fixes issue #15
1 parent f77de8d commit 5db2194

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Sessions for the XP Framework ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
* Fixed issue #15: unserialize(): Extra data starting at offset [...]
7+
(@thekid)
8+
69
## 3.2.0 / 2024-03-24
710

811
* Made compatible with XP 12 - @thekid

src/main/php/web/session/filesystem/Session.class.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ public function close() {
162162
$modification($name);
163163
}
164164
$this->modifications= [];
165-
$this->file->write(serialize($this->values));
165+
166+
// Write file, ensuring to truncate any extra data
167+
$length= $this->file->write(serialize($this->values));
168+
if ($length < $size) {
169+
$this->file->truncate($length);
170+
}
166171

167172
$this->file->unLock();
168173
$this->file->close();

src/test/php/web/session/unittest/InFileSystemTest.class.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,24 @@ public function session_identifiers_consist_of_32_lowercase_hex_digits() {
3939
$id= $sessions->create()->id();
4040
Assert::matches('/^[a-f0-9]{32}$/i', $id);
4141
}
42+
43+
#[Test]
44+
public function issue_15_extra_data_during_unserialize() {
45+
$sessions= $this->fixture();
46+
47+
// Create session and register value
48+
$a= $sessions->create();
49+
$a->register('name', 'initial');
50+
$a->close();
51+
52+
// Overwrite initial value with a shorter one, this should truncate
53+
$b= $sessions->open($a->id());
54+
$b->register('name', 'test');
55+
$b->close();
56+
57+
// Modify session again, should not trigger the "extra data" warning
58+
$c= $sessions->open($a->id());
59+
$c->register('name', 'irrelevant');
60+
$c->close();
61+
}
4262
}

0 commit comments

Comments
 (0)