Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Events/EventInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace phpweb\Events;

final class EventInput
{
/**
* The numeric event fields (start/end date parts and recurrence) that must be
* integers before being passed to checkdate() or mktime().
*/
private const NUMERIC_FIELDS = [
'sday', 'smonth', 'syear', 'eday',
'emonth', 'eyear', 'recur', 'recur_day',
];

/**
* Coerce the numeric event fields of a raw request array to integers.
*
* Untrusted input (non-numeric strings, arrays, missing values) would otherwise
* reach checkdate()/mktime() and throw a TypeError under PHP 8. Anything that is
* not a numeric value becomes 0, i.e. an invalid date that the normal form
* validation already rejects. Non-numeric fields are left untouched.
*
* @param array<string, mixed> $post
* @return array<string, mixed>
*/
public static function normalizeNumericFields(array $post): array
{
foreach (self::NUMERIC_FIELDS as $field) {
$value = $post[$field] ?? null;
$post[$field] = is_numeric($value) ? (int) $value : 0;
}

return $post;
}
}
15 changes: 6 additions & 9 deletions submit-event.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use phpweb\Events\EventInput;

$_SERVER['BASE_PAGE'] = 'submit-event.php';
include_once __DIR__ . '/include/prepend.inc';
include_once __DIR__ . '/include/posttohost.inc';
Expand All @@ -9,16 +11,11 @@
$errors = [];
$process = [] !== $_POST;

// Coerce the numeric date/recurrence fields to integers so untrusted input can be
// passed safely to checkdate()/mktime() below instead of throwing a TypeError.
$_POST = EventInput::normalizeNumericFields($_POST);

// Avoid E_NOTICE errors on incoming vars if not set
$vars = [
'sday', 'smonth', 'syear', 'eday',
'emonth', 'eyear', 'recur', 'recur_day',
];
foreach ($vars as $varname) {
if (empty($_POST[$varname])) {
$_POST[$varname] = 0;
}
}
$vars = [
'type', 'country', 'category', 'email', 'url', 'ldesc', 'sdesc',
];
Expand Down
72 changes: 72 additions & 0 deletions tests/Unit/Events/EventInputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace phpweb\Test\Unit\Events;

use phpweb\Events\EventInput;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(EventInput::class)]
final class EventInputTest extends Framework\TestCase
{
public function testCoercesNumericStringsToIntegers(): void
{
$result = EventInput::normalizeNumericFields(['smonth' => '12', 'sday' => '25', 'syear' => '2026']);

self::assertSame(12, $result['smonth']);
self::assertSame(25, $result['sday']);
self::assertSame(2026, $result['syear']);
}

public function testCoercesNonNumericStringsToZero(): void
{
$result = EventInput::normalizeNumericFields(['smonth' => 'January', 'sday' => 'abc', 'syear' => '12abc']);

self::assertSame(0, $result['smonth']);
self::assertSame(0, $result['sday']);
self::assertSame(0, $result['syear']);
}

public function testCoercesArrayValuesToZero(): void
{
$result = EventInput::normalizeNumericFields(['smonth' => ['x'], 'sday' => [], 'syear' => ['1', '2']]);

self::assertSame(0, $result['smonth']);
self::assertSame(0, $result['sday']);
self::assertSame(0, $result['syear']);
}

public function testDefaultsMissingFieldsToZero(): void
{
$result = EventInput::normalizeNumericFields([]);

foreach (['sday', 'smonth', 'syear', 'eday', 'emonth', 'eyear', 'recur', 'recur_day'] as $field) {
self::assertSame(0, $result[$field], $field);
}
}

public function testLeavesNonNumericFieldsUntouched(): void
{
$result = EventInput::normalizeNumericFields(['email' => 'a@b.com', 'type' => 'multi', 'smonth' => '5']);

self::assertSame('a@b.com', $result['email']);
self::assertSame('multi', $result['type']);
self::assertSame(5, $result['smonth']);
}

/**
* Regression test for the production fatal:
* Uncaught TypeError: checkdate(): Argument #1 ($month) must be of type int, string given
*
* After normalization the date parts are always integers, so passing them to
* checkdate() (and mktime()) can no longer throw a TypeError; garbage input
* simply becomes an invalid (0) date that the normal validation rejects.
*/
public function testNormalizedValuesAreSafeForCheckdate(): void
{
$result = EventInput::normalizeNumericFields(['smonth' => 'notamonth', 'sday' => '1', 'syear' => '2026']);

self::assertFalse(checkdate($result['smonth'], $result['sday'], $result['syear']));
}
}