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
52 changes: 52 additions & 0 deletions src/Events/EventInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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',
];

/**
* The free-text event fields, which default to an empty string so the form can
* be rendered and validated without E_WARNING on undefined array keys.
*/
private const STRING_FIELDS = [
'type', 'country', 'category', 'email', 'url', 'ldesc', 'sdesc',
];

/**
* Normalize a raw event submission so every known field is present and of the
* expected type.
*
* Untrusted numeric 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. Missing text fields become an empty
* string. Unknown fields are left untouched.
*
* @param array<string, mixed> $post
* @return array<string, mixed>
*/
public static function normalize(array $post): array
{
foreach (self::NUMERIC_FIELDS as $field) {
$value = $post[$field] ?? null;
$post[$field] = is_numeric($value) ? (int) $value : 0;
}

foreach (self::STRING_FIELDS as $field) {
$post[$field] ??= '';
}

return $post;
}
}
24 changes: 6 additions & 18 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,24 +11,10 @@
$errors = [];
$process = [] !== $_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',
];
foreach ($vars as $varname) {
if (!isset($_POST[$varname])) {
$_POST[$varname] = "";
}
}
// Default the missing fields and 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::normalize($_POST);

// We need to process some form data
if ($process) {
Expand Down
89 changes: 89 additions & 0 deletions tests/Unit/Events/EventInputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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::normalize(['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::normalize(['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::normalize(['smonth' => ['x'], 'sday' => [], 'syear' => ['1', '2']]);

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

public function testDefaultsMissingNumericFieldsToZero(): void
{
$result = EventInput::normalize([]);

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

public function testDefaultsMissingStringFieldsToEmptyString(): void
{
$result = EventInput::normalize([]);

foreach (['type', 'country', 'category', 'email', 'url', 'ldesc', 'sdesc'] as $field) {
self::assertSame('', $result[$field], $field);
}
}

public function testKeepsSuppliedStringFields(): void
{
$result = EventInput::normalize(['email' => 'a@b.com', 'type' => 'multi', 'sdesc' => '0']);

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

public function testLeavesUnknownFieldsUntouched(): void
{
$result = EventInput::normalize(['sname' => 'PHP UK', 'smonth' => '5']);

self::assertSame('PHP UK', $result['sname']);
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::normalize(['smonth' => 'notamonth', 'sday' => '1', 'syear' => '2026']);

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