fix: phone leak, field-by-value detection, strict Flamingo email fallback

Three pilot-surfaced issues in one pass:

1. PhoneParser regex used '\s' in the candidate character class, so a
   digit on a following flattened line bled into the phone. '+7977
   6277470\n1' (phone + trailing checkbox value) parsed as 12 digits,
   producing '+797762774701'. Replaced '\s' with a literal space so
   newlines/tabs stop the candidate.

2. Custom-themed forms don't use 'your-name' / 'your-email' keys
   (washanyanya.ru has text-211 / text-212 / name_user). Added
   FieldDetector::find_name + find_email as fallbacks. Classifier
   and MessageFormatter now call them if no standard key matches,
   so 'Александр' in text-211 is recognised as a cyrillic name and
   scored / displayed accordingly.

3. FlamingoHelper copied '_from_email' into 'your-email' without
   validating it. Flamingo derives that meta from whatever field it
   considers "first"; on phone-first forms it ends up holding the
   phone number. Now we only copy _from_email if it contains '@',
   and dropped the _from_name shortcut entirely — FieldDetector
   handles the name via the actual field values.

Tests: +1 FieldDetectorTest (13 cases); +2 PhoneParserTest cases for
newline/tab bleeds; +1 ClassifierTest case; +1 MessageFormatterTest
case for custom field keys. Full suite 53/53 green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-04-18 00:22:39 +05:00
parent dbdfc9afa9
commit 8efb82ca43
11 changed files with 283 additions and 13 deletions
+17
View File
@@ -154,4 +154,21 @@ final class ClassifierTest extends TestCase
}
self::assertTrue($matched);
}
public function test_detects_cyrillic_name_under_custom_field_key(): void
{
// Real form on washanyanya.ru uses keys like 'text-211' / 'text-212'
// instead of 'your-name' / 'your-tel'. Classifier must still award
// +1 for a cyrillic name via the value-based heuristic.
$fields = [
'text-211' => 'Александр',
'text-212' => '+79776277470',
];
$r = Classifier::classify($fields, ['reason' => 'Akismet']);
$has_name = false;
foreach ($r['reasons'] as $reason) {
if (strpos($reason['text'], 'кириллическое имя') !== false) $has_name = true;
}
self::assertTrue($has_name, 'expected +1 кириллическое имя via value heuristic');
}
}
+116
View File
@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace Cf7stg\Tests;
use Cf7stg\FieldDetector;
use PHPUnit\Framework\TestCase;
final class FieldDetectorTest extends TestCase
{
public function test_finds_cyrillic_name_in_custom_key(): void
{
$r = FieldDetector::find_name([
'text-211' => 'Александр',
'text-212' => '+79776277470',
]);
self::assertSame('Александр', $r);
}
public function test_finds_multiword_cyrillic_name(): void
{
$r = FieldDetector::find_name(['x' => 'Анна Петровна']);
self::assertSame('Анна Петровна', $r);
}
public function test_finds_latin_name(): void
{
$r = FieldDetector::find_name(['x' => 'John Smith']);
self::assertSame('John Smith', $r);
}
public function test_skips_values_with_digits(): void
{
$r = FieldDetector::find_name([
'a' => '+79776277470',
'b' => 'Ольга',
]);
self::assertSame('Ольга', $r);
}
public function test_skips_urls(): void
{
$r = FieldDetector::find_name([
'a' => 'https://evil.com',
'b' => 'Павел',
]);
self::assertSame('Павел', $r);
}
public function test_skips_emails(): void
{
$r = FieldDetector::find_name([
'a' => 'anna@mail.ru',
'b' => 'Анна',
]);
self::assertSame('Анна', $r);
}
public function test_skips_too_short(): void
{
$r = FieldDetector::find_name([
'a' => 'X',
'b' => 'Дарья',
]);
self::assertSame('Дарья', $r);
}
public function test_skips_too_long(): void
{
$r = FieldDetector::find_name([
'a' => str_repeat('а', 200),
'b' => 'Лена',
]);
self::assertSame('Лена', $r);
}
public function test_no_name_returns_empty(): void
{
$r = FieldDetector::find_name([
'a' => '+79031234567',
'b' => '1',
'c' => 'http://x.y',
]);
self::assertSame('', $r);
}
public function test_finds_email(): void
{
$r = FieldDetector::find_email(['x-1' => 'user@example.com']);
self::assertSame('user@example.com', $r);
}
public function test_no_email_returns_empty_when_only_phone(): void
{
$r = FieldDetector::find_email([
'x' => 'Иван',
'y' => '+79031234567',
]);
self::assertSame('', $r);
}
public function test_email_is_trimmed(): void
{
$r = FieldDetector::find_email(['x' => ' anna@mail.ru ']);
self::assertSame('anna@mail.ru', $r);
}
public function test_first_email_wins_when_multiple(): void
{
$r = FieldDetector::find_email([
'a' => 'first@a.ru',
'b' => 'second@b.ru',
]);
self::assertSame('first@a.ru', $r);
}
}
+27
View File
@@ -151,4 +151,31 @@ final class MessageFormatterTest extends TestCase
self::assertStringContainsString('🟡 неясно', $r['text']);
}
public function test_custom_form_field_keys_are_detected(): void
{
// washanyanya.ru-style form — fields are named text-NNN, name_user,
// etc. We must still pull out name + phone via value heuristics.
$fields = [
'text-211' => 'Александр',
'text-212' => '+79776277470',
'checkbox-382' => '1',
];
$classification = Classifier::classify($fields, ['reason' => 'Akismet']);
$r = MessageFormatter::build([
'fields' => $fields, 'classification' => $classification,
'site_title' => 's.ru', 'form_title' => 'f',
'submitted_at' => '18.04.2026', 'reason' => 'Akismet',
'ip' => '', 'user_agent' => '', 'is_retro' => false,
]);
// Name resolves to 'Александр' via FieldDetector
self::assertStringContainsString('Александр', $r['text']);
// Phone correctly parsed (11 digits, no trailing checkbox '1')
self::assertStringContainsString('+7 (977) 627-74-70', $r['text']);
// Email stays empty (no @-like value)
self::assertStringContainsString("📧 (не указан)", $r['text']);
// The checkbox value should still appear in 'Прочие поля'
self::assertStringContainsString('checkbox-382: 1', $r['text']);
}
}
+18
View File
@@ -72,4 +72,22 @@ final class PhoneParserTest extends TestCase
self::assertSame('+74951234567', $r['e164']);
self::assertTrue($r['is_russian']);
}
public function test_digits_on_next_line_do_not_leak_into_number(): void
{
// Pilot bug: CF7 spam flatten joined values with "\n"; '\s' in the
// candidate regex matched newlines, so '+79776277470\n1' (phone then
// a checkbox value on the next line) turned into a 12-digit number
// ending in 1.
$r = PhoneParser::parse("+79776277470\n1");
self::assertSame('+79776277470', $r['e164']);
self::assertTrue($r['is_russian']);
}
public function test_tab_between_fields_does_not_leak(): void
{
// Same family as above but with tabs.
$r = PhoneParser::parse("89031234567\t1");
self::assertSame('+79031234567', $r['e164']);
}
}