Skip to content

Commit d333b43

Browse files
fix: use queries to check for nested aliases #619
1 parent 735c95e commit d333b43

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/FirewallAlias.inc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,39 @@ class FirewallAlias extends Model {
9393
* @returns string The validated value to set.
9494
* @throws ValidationError When the `address` value is invalid.
9595
*/
96-
public function validate_address(string $addresses): string {
96+
public function validate_address(string $address): string {
97+
# Variables
98+
$aliases = $this->read_all();
99+
$type = $this->type->value;
100+
97101
# Ensure value is a port, port range or port alias when `type` is `port`
98-
if ($this->type->value === 'port' and !is_port_or_range_or_alias($addresses)) {
102+
$port_alias_q = $aliases->query(name: $address, type: 'port');
103+
if ($type === 'port' and !is_port_or_range($address) and !$port_alias_q->exists()) {
99104
throw new ValidationError(
100-
message: "Port alias 'address' value '$addresses' is not a valid port, range, or alias.",
105+
message: "Port alias 'address' value '$address' is not a valid port, range, or alias.",
101106
response_id: 'INVALID_PORT_ALIAS_ADDRESS',
102107
);
103108
}
104109

105110
# Ensure value is an IP, FQDN or alias when `type` is `host`
106-
if ($this->type->value === 'host' and !is_ipaddroralias($addresses) and !is_fqdn($addresses)) {
111+
$host_alias_q = $aliases->query(name: $address, type: 'host');
112+
if ($type === 'host' and !is_ipaddr($address) and !is_fqdn($address) and !$host_alias_q->exists()) {
107113
throw new ValidationError(
108-
message: "Host alias 'address' value '$addresses' is not a valid IP, FQDN, or alias.",
114+
message: "Host alias 'address' value '$address' is not a valid IP, FQDN, or alias.",
109115
response_id: 'INVALID_HOST_ALIAS_ADDRESS',
110116
);
111117
}
112118

113119
# Ensure value is a CIDR, FQDN or alias when `type` is `network`
114-
if ($this->type->value === 'network') {
115-
if (!is_subnet($addresses) and alias_get_type($addresses) != 'network' and !is_fqdn($addresses)) {
116-
throw new ValidationError(
117-
message: "Host alias 'address' value '$addresses' is not a valid CIDR, FQDN, or alias.",
118-
response_id: 'INVALID_NETWORK_ALIAS_ADDRESS',
119-
);
120-
}
120+
$network_alias_q = $aliases->query(name: $address, type: 'network');
121+
if ($type === 'network' and !is_subnet($address) and !is_fqdn($address) and !$network_alias_q->exists()) {
122+
throw new ValidationError(
123+
message: "Host alias 'address' value '$address' is not a valid CIDR, FQDN, or alias.",
124+
response_id: 'INVALID_NETWORK_ALIAS_ADDRESS',
125+
);
121126
}
122127

123-
return $addresses;
128+
return $address;
124129
}
125130

126131
/**

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APIModelsFirewallAliasTestCase.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,22 @@ class APIModelsFirewallAliasTestCase extends TestCase {
144144
},
145145
);
146146
}
147+
148+
/**
149+
* Checks that we can reference a nested alias during replace_all() calls. This is regression test for #619.
150+
*/
151+
public function test_nested_alias_reference_in_replace_all(): void {
152+
# Ensure we can reference a nested alias during replace_all() calls without an error being thrown
153+
$this->assert_does_not_throw(
154+
callable: function () {
155+
$alias = new FirewallAlias();
156+
$alias->replace_all(
157+
data: [
158+
['name' => 'test_alias1', 'type' => 'host', 'address' => []],
159+
['name' => 'test_alias2', 'type' => 'host', 'address' => ['test_alias1']],
160+
],
161+
);
162+
},
163+
);
164+
}
147165
}

0 commit comments

Comments
 (0)