Skip to content

Commit 140c2b2

Browse files
Clément PAPPALARDOClément PAPPALARDO
authored andcommitted
fix(NetworkInterface): only validate descr filter-name rules on CHANGED values
1 parent 498a990 commit 140c2b2

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/NetworkInterface.inc‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ class NetworkInterface extends Model {
9494
required: true,
9595
unique: true,
9696
maximum_length: 128,
97-
validators: [
98-
new FilterNameValidator(),
99-
new UniqueFromForeignModelValidator(model: 'FirewallAlias', field: 'name'),
100-
],
97+
validators: [new UniqueFromForeignModelValidator(model: 'FirewallAlias', field: 'name')],
10198
verbose_name: 'Description',
10299
help_text: 'The descriptive name for this interface.',
103100
);
@@ -445,6 +442,16 @@ class NetworkInterface extends Model {
445442
* @returns string The validated $descr to set.
446443
* @throws ValidationError When the `$descr` value is invalid. */
447444
public function validate_descr(string $descr): string {
445+
# Only apply the filter-name validation when the descr CHANGES. pfSense
446+
# revalidates every field on PATCH, so validating an unchanged value
447+
# would lock entire interfaces whose pre-existing descr predates these
448+
# rules (e.g. 'LAN', which matches a reserved pf filter token) — the
449+
# GUI allows such interfaces to be edited without renaming.
450+
$initial = $this->initial_object;
451+
if ($initial !== null and $initial->descr->value === $descr) {
452+
return $descr;
453+
}
454+
448455
# Ensure OpenVPN interfaces are 22 characters or less
449456
if (str_starts_with($this->if->value, 'ovpn') and strlen($descr) > 22) {
450457
throw new ValidationError(
@@ -477,6 +484,11 @@ class NetworkInterface extends Model {
477484
);
478485
}
479486

487+
# Filter-name rules apply to NEW/CHANGED descr values only (moved
488+
# from the field-level validator so unchanged legacy values are
489+
# not re-validated on unrelated PATCHes).
490+
(new FilterNameValidator())->validate($descr, 'descr');
491+
480492
return $descr;
481493
}
482494

‎pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APIModelsNetworkInterfaceTestCase.inc‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,4 +876,44 @@ class APIModelsNetworkInterfaceTestCase extends TestCase {
876876
}
877877

878878
# TODO: Needs Tests to ensure other interface types are correctly configured on the backend
879+
880+
/**
881+
* Checks that a NEW descr value violating filter-name rules is rejected
882+
* (FilterNameValidator now runs in validate_descr for changed values).
883+
*/
884+
public function test_descr_new_value_rejects_reserved_name() {
885+
$this->assert_throws_response(
886+
response_id: 'FILTER_NAME_VALIDATOR_RESERVED_NAME_USED',
887+
code: 400,
888+
callable: function () {
889+
$test_model = new NetworkInterface();
890+
$test_model->if->value = 'igc0';
891+
$test_model->validate_descr('LAN');
892+
},
893+
);
894+
}
895+
896+
/**
897+
* Checks that validate_descr accepts reserved legacy values when the
898+
* descr is UNCHANGED from the initial object (regression: pfSense
899+
* revalidates every field on PATCH; validating an unchanged value
900+
* locked whole interfaces whose descr predates the rules).
901+
*/
902+
public function test_descr_unchanged_legacy_value_not_revalidated() {
903+
$this->assert_does_not_throw(
904+
callable: function () {
905+
$test_model = new NetworkInterface();
906+
$test_model->if->value = 'igc0';
907+
908+
# Simulate a loaded object whose initial descr is a legacy
909+
# reserved token (e.g. created before the validator existed)
910+
$test_model->initial_object = $test_model->copy();
911+
$test_model->initial_object->descr->value = 'LAN';
912+
$test_model->descr->value = 'LAN';
913+
914+
# Unchanged descr -> no filter-name validation
915+
$test_model->validate_descr('LAN');
916+
},
917+
);
918+
}
879919
}

0 commit comments

Comments
 (0)