Skip to content

Commit

Permalink
Spf1: Remove strict option, always ignore repeating whitespace, ignor…
Browse files Browse the repository at this point in the history
…e missing domain-end as it is optional
  • Loading branch information
alexrsagen committed Apr 26, 2024
1 parent 23330fe commit a7a148d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alexrsagen/obie",
"version": "1.6.7",
"version": "1.6.8",
"type": "framework",
"description": "Obie is a simple PHP framework. It aims to provide basic services needed for any web app.",
"keywords": ["framework", "php", "http", "template", "view", "router", "routing", "model", "models", "session", "sessions"],
Expand Down
48 changes: 23 additions & 25 deletions src/encoding/spf1.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected static function storeDirective(string &$buf, ?Directive &$directive) {
return true;
}

public static function decode(string $input, bool $strict = true): ?Record {
public static function decode(string $input): ?Record {
$record = new Record();

// check version
Expand Down Expand Up @@ -126,29 +126,27 @@ public static function decode(string $input, bool $strict = true): ?Record {
Log::warning(sprintf('Spf1: invalid macro-string at position %d', $position));
return null;
}
if ($position >= strlen($input)) {
Log::warning(sprintf('Spf1: invalid domain-spec: ended before domain-end at position %d', $position));
return null;
}
if ($input[$position] === '.') {
// extract a toplabel from input, append to buffer
for (; $position < strlen($input) && preg_match('/^[a-z0-9\-]$/i', $input[$position]) === 1; $position++) {
$buf .= $input[$position];
}
if ($position < strlen($input) && $input[$position] === '.') {
$buf .= $input[$position];
$position++;
}
} elseif ($input[$position] === '%') {
// append to buffer the result of extracting a macro-expand from input, given position
$buf_key = $buf;
$buf = MacroString::extranctExpand($input, $position);
if ($buf === null) {
Log::warning(sprintf('Spf1: invalid macro-expand at position %d', $position));
return null;
if ($position < strlen($input)) {
if ($input[$position] === '.') {
// extract a toplabel from input, append to buffer
for (; $position < strlen($input) && preg_match('/^[a-z0-9\-]$/i', $input[$position]) === 1; $position++) {
$buf .= $input[$position];
}
if ($position < strlen($input) && $input[$position] === '.') {
$buf .= $input[$position];
$position++;
}
} elseif ($input[$position] === '%') {
// append to buffer the result of extracting a macro-expand from input, given position
$buf_key = $buf;
$buf = MacroString::extractExpand($input, $position);
if ($buf === null) {
Log::warning(sprintf('Spf1: invalid macro-expand at position %d', $position));
return null;
}
$buf = $buf_key . $buf;
unset($buf_key);
}
$buf = $buf_key . $buf;
unset($buf_key);
}
// store mechanism value in directive
if (!static::storeDirective($buf, $directive)) return null;
Expand All @@ -160,8 +158,8 @@ public static function decode(string $input, bool $strict = true): ?Record {

// term delimiter
case ' ':
// non-strict: ignore multiple whitespace
if (!$strict && strlen($buf) === 0 && $directive === null) {
// ignore multiple whitespace
if (strlen($buf) === 0 && $directive === null) {
break;
}
if (!static::storeDirective($buf, $directive)) return null;
Expand Down
4 changes: 2 additions & 2 deletions src/encoding/spf1/macro_string.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public static function extractString(string $input, int &$position): ?string {
// 3.3. If the code point at position within input is not "%", break.
if ($input[$position] !== '%') break;
// 3.4. Extract macro-expand
if (static::extranctExpand($input, $position) === null) return null;
if (static::extractExpand($input, $position) === null) return null;
}
// 4. Return the code points from positionStart to position, inclusive, within input.
return substr($input, $position_start, $position - $position_start);
}

public static function extranctExpand(string $input, int &$position): ?string {
public static function extractExpand(string $input, int &$position): ?string {
// 1. Let positionStart be position.
$position_start = $position;
// 2. Assert: the code point at position within input is "%"
Expand Down
2 changes: 1 addition & 1 deletion tests-fuzz/encoding_spf1_macro_string_extract_expand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

$config->setTarget(function (string $input) {
$pos = 0;
Obie\Encoding\Spf1\MacroString::extranctExpand($input, $pos);
Obie\Encoding\Spf1\MacroString::extractExpand($input, $pos);
});

$config->setMaxLen(512);

0 comments on commit a7a148d

Please sign in to comment.