Skip to content

Commit 2650de1

Browse files
feat(HAProxyFrontendACL): add additional validation for supported expressions #948
1 parent 1028f71 commit 2650de1

2 files changed

Lines changed: 230 additions & 2 deletions

File tree

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace RESTAPI\Models;
44

5+
require_once 'RESTAPI/autoloader.inc';
6+
57
use RESTAPI\Core\Model;
68
use RESTAPI\Dispatchers\HAProxyApplyDispatcher;
79
use RESTAPI\Fields\BooleanField;
810
use RESTAPI\Fields\StringField;
11+
use RESTAPI\Responses\ValidationError;
912
use RESTAPI\Validators\RegexValidator;
1013

1114
/**
@@ -18,7 +21,10 @@ class HAProxyFrontendACL extends Model {
1821
public BooleanField $casesensitive;
1922
public BooleanField $not;
2023

21-
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
24+
/**
25+
* Constructs the HAProxy frontend ACL Model and its fields.
26+
*/
27+
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], mixed ...$options) {
2228
# Set model attributes
2329
$this->parent_model_class = 'HAProxyFrontend';
2430
$this->config_path = 'ha_acls/item';
@@ -31,8 +37,8 @@ class HAProxyFrontendACL extends Model {
3137
$this->name = new StringField(
3238
required: true,
3339
minimum_length: 2,
34-
validators: [new RegexValidator(pattern: '/[^a-zA-Z0-9\.\-_]/', invert: true)],
3540
verbose_name: 'Name',
41+
validators: [new RegexValidator(pattern: '/[^a-zA-Z0-9\.\-_]/', invert: true)],
3642
help_text: 'The unique name for this frontend ACL.',
3743
);
3844
$this->expression = new StringField(
@@ -89,6 +95,58 @@ class HAProxyFrontendACL extends Model {
8995
parent::__construct($id, $parent_id, $data, ...$options);
9096
}
9197

98+
/**
99+
* Ensures the expression is supported by the parent frontend's processing type.
100+
* @param string $expression The incoming ACL expression.
101+
* @return string The validated expression.
102+
* @throws ValidationError When the expression is incompatible with the parent frontend type.
103+
*/
104+
public function validate_expression(string $expression): string {
105+
$type = $this->parent_model->type->value;
106+
$supported_expressions = [
107+
'http' => [
108+
'host_starts_with',
109+
'host_ends_with',
110+
'host_matches',
111+
'host_regex',
112+
'host_contains',
113+
'path_starts_with',
114+
'path_ends_with',
115+
'path_matches',
116+
'path_regex',
117+
'path_contains',
118+
'path_dir',
119+
'url_parameter',
120+
'ssl_c_verify_code',
121+
'ssl_c_verify',
122+
'ssl_c_ca_commonname',
123+
'source_ip',
124+
'backendservercount',
125+
'custom',
126+
],
127+
'https' => [
128+
'source_ip',
129+
'backendservercount',
130+
'ssl_sni_matches',
131+
'ssl_sni_contains',
132+
'ssl_sni_starts_with',
133+
'ssl_sni_ends_with',
134+
'ssl_sni_regex',
135+
'custom',
136+
],
137+
'tcp' => ['source_ip', 'backendservercount', 'traffic_is_http', 'traffic_is_ssl', 'custom'],
138+
];
139+
140+
if (!in_array($expression, $supported_expressions[$type] ?? [], true)) {
141+
throw new ValidationError(
142+
message: "Expression `$expression` is not supported by parent frontend type `$type`.",
143+
response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH',
144+
);
145+
}
146+
147+
return $expression;
148+
}
149+
92150
/**
93151
* Ensures haproxy is marked as dirty before applying.
94152
*/
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\TestCase;
8+
use RESTAPI\Models\HAProxyFrontend;
9+
use RESTAPI\Models\HAProxyFrontendACL;
10+
11+
/**
12+
* Tests HAProxy frontend ACL expression compatibility with parent processing types.
13+
*/
14+
class APIModelsHAProxyFrontendACLTestCase extends TestCase {
15+
public array $required_packages = ['pfSense-pkg-haproxy'];
16+
17+
private const SUPPORTED_EXPRESSIONS = [
18+
'http' => [
19+
'host_starts_with',
20+
'host_ends_with',
21+
'host_matches',
22+
'host_regex',
23+
'host_contains',
24+
'path_starts_with',
25+
'path_ends_with',
26+
'path_matches',
27+
'path_regex',
28+
'path_contains',
29+
'path_dir',
30+
'url_parameter',
31+
'ssl_c_verify_code',
32+
'ssl_c_verify',
33+
'ssl_c_ca_commonname',
34+
'source_ip',
35+
'backendservercount',
36+
'custom',
37+
],
38+
'https' => [
39+
'source_ip',
40+
'backendservercount',
41+
'ssl_sni_matches',
42+
'ssl_sni_contains',
43+
'ssl_sni_starts_with',
44+
'ssl_sni_ends_with',
45+
'ssl_sni_regex',
46+
'custom',
47+
],
48+
'tcp' => ['source_ip', 'backendservercount', 'traffic_is_http', 'traffic_is_ssl', 'custom'],
49+
];
50+
51+
/**
52+
* Checks every expression against each persisted parent frontend type.
53+
*/
54+
public function test_expression_type_matrix(): void {
55+
$choices = (new HAProxyFrontendACL())->expression->choices;
56+
57+
foreach (self::SUPPORTED_EXPRESSIONS as $type => $supported) {
58+
$frontend = new HAProxyFrontend(name: "acl_validation_$type", type: $type);
59+
$frontend->create();
60+
61+
foreach ($choices as $expression) {
62+
$acl = new HAProxyFrontendACL(
63+
parent_id: $frontend->id,
64+
name: 'test_acl',
65+
expression: $expression,
66+
value: '',
67+
);
68+
69+
if (in_array($expression, $supported, true)) {
70+
$this->assert_is_true($acl->validate());
71+
$this->assert_equals($acl->expression->value, $expression);
72+
} else {
73+
$this->assert_throws_response(
74+
response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH',
75+
code: 400,
76+
callable: fn() => $acl->validate(),
77+
);
78+
}
79+
}
80+
}
81+
}
82+
83+
/**
84+
* Checks nested ACL validation uses the unsaved parent frontend's type.
85+
*/
86+
public function test_nested_expression_type_matrix(): void {
87+
$choices = (new HAProxyFrontendACL())->expression->choices;
88+
89+
foreach (self::SUPPORTED_EXPRESSIONS as $type => $supported) {
90+
foreach ($choices as $expression) {
91+
$frontend = new HAProxyFrontend(
92+
name: "nested_acl_validation_$type",
93+
type: $type,
94+
ha_acls: [['name' => 'test_acl', 'expression' => $expression, 'value' => '']],
95+
);
96+
97+
if (in_array($expression, $supported, true)) {
98+
$this->assert_is_true($frontend->validate());
99+
$this->assert_equals($frontend->ha_acls->value[0]['expression'], $expression);
100+
} else {
101+
$this->assert_throws_response(
102+
response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH',
103+
code: 400,
104+
callable: fn() => $frontend->validate(),
105+
);
106+
}
107+
}
108+
}
109+
}
110+
111+
/**
112+
* Ensures incompatible expressions cannot be created or written over an existing ACL.
113+
*/
114+
public function test_reject_incompatible_writes(): void {
115+
$frontend = new HAProxyFrontend(name: 'acl_write_validation', type: 'http');
116+
$frontend->create();
117+
$acl = new HAProxyFrontendACL(
118+
parent_id: $frontend->id,
119+
name: 'test_acl',
120+
expression: 'traffic_is_ssl',
121+
value: '',
122+
);
123+
124+
$this->assert_throws_response(
125+
response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH',
126+
code: 400,
127+
callable: fn() => $acl->create(),
128+
);
129+
$this->assert_is_false(HAProxyFrontendACL::query(parent_id: $frontend->id)->exists());
130+
131+
$acl = new HAProxyFrontendACL(
132+
parent_id: $frontend->id,
133+
name: 'test_acl',
134+
expression: 'host_matches',
135+
value: 'example.com',
136+
);
137+
$acl->create();
138+
$acl->expression->value = 'ssl_sni_matches';
139+
140+
$this->assert_throws_response(
141+
response_id: 'HAPROXY_FRONTEND_ACL_EXPRESSION_TYPE_MISMATCH',
142+
code: 400,
143+
callable: fn() => $acl->update(),
144+
);
145+
$this->assert_equals(
146+
HAProxyFrontendACL::query(parent_id: $frontend->id, id: $acl->id)->first()->expression->value,
147+
'host_matches',
148+
);
149+
}
150+
151+
/**
152+
* Ensures unknown expressions still fail the Field's choice validation.
153+
*/
154+
public function test_reject_unknown_expression(): void {
155+
$frontend = new HAProxyFrontend(name: 'acl_unknown_validation', type: 'http');
156+
$frontend->create();
157+
$acl = new HAProxyFrontendACL(
158+
parent_id: $frontend->id,
159+
name: 'test_acl',
160+
expression: 'unknown_expression',
161+
value: '',
162+
);
163+
164+
$this->assert_throws_response(
165+
response_id: 'FIELD_INVALID_CHOICE',
166+
code: 400,
167+
callable: fn() => $acl->validate(),
168+
);
169+
}
170+
}

0 commit comments

Comments
 (0)