Skip to content

Commit 53b7c4c

Browse files
Merge pull request #660 from jaredhendrickson13/next_patch
v2.3.4 Fixes
2 parents 5a60e23 + 486c75a commit 53b7c4c

5 files changed

Lines changed: 55 additions & 13 deletions

File tree

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/Model.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ class Model {
22182218
unset($representation_object['id']);
22192219

22202220
# Define a new Model object using this representation and validate
2221-
$model_object = new $this(data: $representation_object, skip_init: true);
2221+
$model_object = new $this(data: $representation_object, skip_init: true, client: $this->client);
22222222
$model_object->id = $model_object->get_next_id();
22232223
$model_object->validate(modelset: $new_objects);
22242224

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ class FirewallAlias extends Model {
108108
}
109109

110110
# Ensure value is an IP, FQDN or alias when `type` is `host`
111-
$host_alias_q = $aliases->query(name: $address, type: 'host');
111+
$host_alias_q = $aliases->query(name: $address, type__except: 'port');
112112
if ($type === 'host' and !is_ipaddr($address) and !is_fqdn($address) and !$host_alias_q->exists()) {
113113
throw new ValidationError(
114114
message: "Host alias 'address' value '$address' is not a valid IP, FQDN, or alias.",
115115
response_id: 'INVALID_HOST_ALIAS_ADDRESS',
116116
);
117117
}
118118

119-
# Ensure value is a CIDR, FQDN or alias when `type` is `network`
120-
$network_alias_q = $aliases->query(name: $address, type: 'network');
119+
# Ensure value is a CIDR, FQDN or non-port alias when `type` is `network`
120+
$network_alias_q = $aliases->query(name: $address, type__except: 'port');
121121
if ($type === 'network' and !is_subnet($address) and !is_fqdn($address) and !$network_alias_q->exists()) {
122122
throw new ValidationError(
123123
message: "Network alias 'address' value '$address' is not a valid CIDR, FQDN, or alias.",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ class PortForward extends Model {
205205
protocol: $this->protocol->value,
206206
source: $this->source->value,
207207
source_port: $this->source_port->value,
208-
destination: $this->destination->value,
209-
destination_port: $this->destination_port->value,
208+
destination: $this->target->value,
209+
destination_port: $this->local_port->value,
210210
descr: "Associated rule for port forward rule {$this->associated_rule_id->value}",
211211
client: $this->client,
212212
);
@@ -246,8 +246,8 @@ class PortForward extends Model {
246246
protocol: $this->protocol->value,
247247
source: $this->source->value,
248248
source_port: $this->source_port->value,
249-
destination: $this->destination->value,
250-
destination_port: $this->destination_port->value,
249+
destination: $this->target->value,
250+
destination_port: $this->local_port->value,
251251
);
252252

253253
# Format any errors that occur during the update of the rule so its clear that its associated with the linked rule

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RESTAPI\Tests;
44

5+
use RESTAPI\Core\Auth;
56
use RESTAPI\Core\Command;
67
use RESTAPI\Core\TestCase;
78
use RESTAPI\Models\FirewallRule;
@@ -904,4 +905,45 @@ class APIModelsFirewallRuleTestCase extends TestCase {
904905
$limiter2->delete(apply: true);
905906
$limiter1->delete(apply: true);
906907
}
908+
909+
/**
910+
* Checks that the username of the client is correctly identified when replace_all is called.
911+
*/
912+
public function test_replace_all_retains_client_username(): void {
913+
# Mock the authenticated client
914+
$client = new Auth();
915+
$client->username = 'testuser';
916+
$client->ip_address = '127.1.2.3';
917+
918+
# Replace all firewall rules with a new set
919+
$rule_model = new FirewallRule(client: $client);
920+
$rules = $rule_model->replace_all(
921+
data: [
922+
[
923+
'type' => 'pass',
924+
'interface' => ['lan'],
925+
'ipprotocol' => 'inet',
926+
'source' => 'any',
927+
'destination' => 'any',
928+
'descr' => 'test description',
929+
],
930+
[
931+
'type' => 'pass',
932+
'interface' => ['lan'],
933+
'ipprotocol' => 'inet',
934+
'source' => 'any',
935+
'destination' => 'any',
936+
'descr' => 'test description 2',
937+
],
938+
],
939+
);
940+
941+
# Ensure the client username and IP are correctly set in the new rules
942+
foreach ($rules->model_objects as $rule) {
943+
$this->assert_equals($rule->client->username, $client->username);
944+
$this->assert_equals($rule->client->ip_address, $client->ip_address);
945+
$this->assert_equals($rule->created_by->value, "$client->username@$client->ip_address (API)");
946+
$this->assert_equals($rule->updated_by->value, "$client->username@$client->ip_address (API)");
947+
}
948+
}
907949
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class APIModelsPortForwardTestCase extends TestCase {
182182
/**
183183
* Checks that port forwards with associated firewall rules correctly create, update and delete the associated rule
184184
*/
185-
public function test_associated_firewall_rules() {
185+
public function test_associated_firewall_rules(): void {
186186
# Create a port forward to test with
187187
$port_forward = new PortForward(
188188
interface: 'wan',
@@ -208,8 +208,8 @@ class APIModelsPortForwardTestCase extends TestCase {
208208
$this->assert_equals($rule_q->first()->protocol->value, 'tcp');
209209
$this->assert_equals($rule_q->first()->source->value, '1.2.3.4');
210210
$this->assert_equals($rule_q->first()->source_port->value, '1234');
211-
$this->assert_equals($rule_q->first()->destination->value, '4.3.2.1');
212-
$this->assert_equals($rule_q->first()->destination_port->value, '4321');
211+
$this->assert_equals($rule_q->first()->destination->value, '127.0.0.1');
212+
$this->assert_equals($rule_q->first()->destination_port->value, '1234');
213213

214214
# Update the port forward and ensure the associated rule is updated
215215
$port_forward->from_representation(
@@ -228,8 +228,8 @@ class APIModelsPortForwardTestCase extends TestCase {
228228
$this->assert_equals($rule_q->first()->protocol->value, 'udp');
229229
$this->assert_equals($rule_q->first()->source->value, '4321::1');
230230
$this->assert_equals($rule_q->first()->source_port->value, '4321');
231-
$this->assert_equals($rule_q->first()->destination->value, '1234::1');
232-
$this->assert_equals($rule_q->first()->destination_port->value, '1234');
231+
$this->assert_equals($rule_q->first()->destination->value, 'fe80::1');
232+
$this->assert_equals($rule_q->first()->destination_port->value, '4321');
233233

234234
# Delete the port forward and ensure the associated rule is deleted
235235
$port_forward->delete();

0 commit comments

Comments
 (0)