Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6352,7 +6352,7 @@ Default value: `$apache::params::modsec_dir`

##### <a name="-apache--mod--security--modsec_secruleengine"></a>`modsec_secruleengine`

Data type: `String`
Data type: `Enum['On', 'Off', 'DetectionOnly']`

Configures the rules engine.

Expand Down Expand Up @@ -8108,6 +8108,7 @@ The following parameters are available in the `apache::vhost` defined type:
* [`userdir`](#-apache--vhost--userdir)
* [`proxy_protocol`](#-apache--vhost--proxy_protocol)
* [`proxy_protocol_exceptions`](#-apache--vhost--proxy_protocol_exceptions)
* [`modsec_rule_engine`](#-apache--vhost--modsec_rule_engine)

##### <a name="-apache--vhost--access_log"></a>`access_log`

Expand Down Expand Up @@ -8983,7 +8984,8 @@ Default value: `undef`

Data type: `Boolean`

Disables `mod_security` on a virtual host. Only valid if `apache::mod::security` is included.
Deprecated. Disables `mod_security` on a virtual host. Use `modsec_rule_engine => 'Off'` instead.
Only valid if `apache::mod::security` is included.

Default value: `false`

Expand Down Expand Up @@ -11052,6 +11054,15 @@ Disable processing of PROXY header for certain hosts or networks

Default value: `[]`

##### <a name="-apache--vhost--modsec_rule_engine"></a>`modsec_rule_engine`

Data type: `Optional[Enum['On', 'Off', 'DetectionOnly']]`

Configures the ModSecurity rules engine for this virtual host. When unset, the virtual host
inherits the global `apache::mod::security::modsec_secruleengine` setting.

Default value: `undef`

### <a name="apache--vhost--custom"></a>`apache::vhost::custom`

The `apache::vhost::custom` defined type is a thin wrapper around the `apache::custom_config` defined type, and simply overrides some of its default settings specific to the virtual host directory in Apache.
Expand Down
4 changes: 2 additions & 2 deletions manifests/mod/security.pp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@
Boolean $custom_rules = $apache::params::modsec_custom_rules,
Optional[Array[String]] $custom_rules_set = $apache::params::modsec_custom_rules_set,
Stdlib::Absolutepath $modsec_dir = $apache::params::modsec_dir,
String $modsec_secruleengine = $apache::params::modsec_secruleengine,
Integer[0, 9] $debug_log_level = 0,
Enum['On', 'Off', 'DetectionOnly'] $modsec_secruleengine = $apache::params::modsec_secruleengine,
Integer[0, 9] $debug_log_level = 0,
String $audit_log_relevant_status = '^(?:5|4(?!04))',
String $audit_log_parts = $apache::params::modsec_audit_log_parts,
String $audit_log_type = $apache::params::modsec_audit_log_type,
Expand Down
76 changes: 44 additions & 32 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@
# Configures the maximum request body size (in bytes) ModSecurity accepts for buffering.
#
# @param modsec_disable_vhost
# Disables `mod_security` on a virtual host. Only valid if `apache::mod::security` is included.
# Deprecated. Disables `mod_security` on a virtual host. Use `modsec_rule_engine => 'Off'` instead.
# Only valid if `apache::mod::security` is included.
#
# @param modsec_disable_ids
# Removes `mod_security` IDs from the virtual host.<br />
Expand Down Expand Up @@ -1710,6 +1711,11 @@
#
# @param proxy_protocol_exceptions
# Disable processing of PROXY header for certain hosts or networks
#
# @param modsec_rule_engine
# Configures the ModSecurity rules engine for this virtual host. When unset, the virtual host
# inherits the global `apache::mod::security::modsec_secruleengine` setting.
#
define apache::vhost (
Variant[Stdlib::Absolutepath, Boolean] $docroot,
Boolean $manage_docroot = true,
Expand Down Expand Up @@ -1973,6 +1979,7 @@
Optional[Variant[String[1], Array[String[1]]]] $userdir = undef,
Optional[Boolean] $proxy_protocol = undef,
Array[Stdlib::Host] $proxy_protocol_exceptions = [],
Optional[Enum['On', 'Off', 'DetectionOnly']] $modsec_rule_engine = undef,
) {
# The base class must be included first because it is used by parameter defaults
if ! defined(Class['apache']) {
Expand Down Expand Up @@ -2181,28 +2188,19 @@
}

## Create a global LocationMatch if locations aren't defined
if $modsec_disable_ids {
if $modsec_disable_ids =~ Array {
$_modsec_disable_ids = { '.*' => $modsec_disable_ids }
} else {
$_modsec_disable_ids = $modsec_disable_ids
}
$_modsec_disable_ids = $modsec_disable_ids ? {
Array => { '.*' => $modsec_disable_ids },
default => $modsec_disable_ids,
}

if $modsec_disable_msgs {
if $modsec_disable_msgs =~ Array {
$_modsec_disable_msgs = { '.*' => $modsec_disable_msgs }
} else {
$_modsec_disable_msgs = $modsec_disable_msgs
}
$_modsec_disable_msgs = $modsec_disable_msgs ? {
Array => { '.*' => $modsec_disable_msgs },
default => $modsec_disable_msgs,
}

if $modsec_disable_tags {
if $modsec_disable_tags =~ Array {
$_modsec_disable_tags = { '.*' => $modsec_disable_tags }
} else {
$_modsec_disable_tags = $modsec_disable_tags
}
$_modsec_disable_tags = $modsec_disable_tags ? {
Array => { '.*' => $modsec_disable_tags },
default => $modsec_disable_tags,
}

concat { "${priority_real}${filename}.conf":
Expand Down Expand Up @@ -2840,19 +2838,33 @@
}
}

if $modsec_disable_vhost or $modsec_disable_ids or !empty($modsec_disable_ips) or $modsec_disable_msgs or $modsec_disable_tags or $modsec_audit_log_destination or ($modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold) or $modsec_allowed_methods {
$security_params = {
'modsec_disable_vhost' => $modsec_disable_vhost,
'modsec_audit_log_destination' => $modsec_audit_log_destination,
'_modsec_disable_ids' => $modsec_disable_ids,
'modsec_disable_ips' => $modsec_disable_ips,
'_modsec_disable_msgs' => $modsec_disable_msgs,
'_modsec_disable_tags' => $modsec_disable_tags,
'modsec_body_limit' => $modsec_body_limit,
'modsec_inbound_anomaly_threshold' => $modsec_inbound_anomaly_threshold,
'modsec_outbound_anomaly_threshold' => $modsec_outbound_anomaly_threshold,
'modsec_allowed_methods' => $modsec_allowed_methods,
}
if $modsec_disable_vhost {
deprecation('apache::vhost::modsec_disable_vhost', 'This parameter is deprecated. Set `modsec_rule_engine` to `Off` instead.')
}

$_modsec_rule_engine = $modsec_rule_engine ? {
undef => $modsec_disable_vhost ? {
true => 'Off',
default => undef,
},
Comment thread
Copilot marked this conversation as resolved.
Resolved
default => $modsec_rule_engine,
}

$modsec_enabled = ($modsec_rule_engine != undef or $modsec_disable_vhost or $modsec_audit_log_destination != undef or $modsec_disable_ids != undef or !empty($modsec_disable_ips) or $modsec_disable_msgs != undef or $modsec_disable_tags != undef or $modsec_body_limit != undef or $modsec_inbound_anomaly_threshold != undef or $modsec_outbound_anomaly_threshold != undef or $modsec_allowed_methods != undef)

$security_params = {
'_modsec_rule_engine' => $_modsec_rule_engine,
'modsec_audit_log_destination' => $modsec_audit_log_destination,
'_modsec_disable_ids' => $_modsec_disable_ids,
'modsec_disable_ips' => $modsec_disable_ips,
'_modsec_disable_msgs' => $_modsec_disable_msgs,
'_modsec_disable_tags' => $_modsec_disable_tags,
'modsec_body_limit' => $modsec_body_limit,
'modsec_inbound_anomaly_threshold' => $modsec_inbound_anomaly_threshold,
'modsec_outbound_anomaly_threshold' => $modsec_outbound_anomaly_threshold,
'modsec_allowed_methods' => $modsec_allowed_methods,
}
if $modsec_enabled {
concat::fragment { "${name}-security":
target => "${priority_real}${filename}.conf",
order => 320,
Expand Down
76 changes: 76 additions & 0 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,82 @@
content: %r{^\s*SecAuditLog "/var/log/#{apache_name}/rspec\.example\.com_security\.log"$},
)
}

it {
expect(subject).to contain_concat__fragment('rspec.example.com-security')
.without_content(%r{^\s*SecRuleEngine\s})
}
end

context 'modsec_rule_engine On' do
let :params do
{
'docroot' => '/rspec/docroot',
'modsec_rule_engine' => 'On'
}
end

it { is_expected.to compile }

it {
expect(subject).to contain_concat__fragment('rspec.example.com-security').with(
content: %r{^\s*SecRuleEngine On$},
)
}
end

context 'modsec_rule_engine DetectionOnly' do
let :params do
{
'docroot' => '/rspec/docroot',
'modsec_rule_engine' => 'DetectionOnly'
}
end

it { is_expected.to compile }

it {
expect(subject).to contain_concat__fragment('rspec.example.com-security').with(
content: %r{^\s*SecRuleEngine DetectionOnly$},
)
}
end

context 'modsec_rule_engine Off' do
let :params do
{
'docroot' => '/rspec/docroot',
'modsec_rule_engine' => 'Off',
'modsec_audit_log' => true,
'modsec_allowed_methods' => 'GET HEAD POST OPTIONS'
}
end

it { is_expected.to compile }

it {
expect(subject).to contain_concat__fragment('rspec.example.com-security')
.with_content(%r{^\s*SecRuleEngine Off$})
.without_content(%r{^\s*SecAuditLog\s})
.without_content(%r{^\s*SecAction\s})
}
end

context 'legacy modsec_disable_vhost' do
let :params do
{
'docroot' => '/rspec/docroot',
'modsec_disable_vhost' => true
}
end

it { is_expected.to compile }

it {
expect(subject).to contain_concat__fragment('rspec.example.com-security').with(
content: %r{^\s*SecRuleEngine Off$},
)
}
end

context 'modsec_audit_log_file' do
Expand Down
51 changes: 32 additions & 19 deletions templates/vhost/_security.epp
Original file line number Diff line number Diff line change
@@ -1,55 +1,67 @@
<%- | Optional[Enum['On', 'Off', 'DetectionOnly']] $_modsec_rule_engine,
Optional[String] $modsec_audit_log_destination,
Optional[Hash] $_modsec_disable_ids,
Array[String] $modsec_disable_ips,
Optional[Hash] $_modsec_disable_msgs,
Optional[Hash] $_modsec_disable_tags,
Optional[String] $modsec_body_limit,
Optional[Integer] $modsec_inbound_anomaly_threshold,
Optional[Integer] $modsec_outbound_anomaly_threshold,
Optional[String] $modsec_allowed_methods,
| -%>
<IfModule mod_security2.c>
<% if $modsec_disable_vhost {-%>
SecRuleEngine Off
<% if $_modsec_rule_engine { -%>
SecRuleEngine <%= $_modsec_rule_engine %>
<% } -%>
<% if $modsec_audit_log_destination {-%>
<% if $_modsec_rule_engine != 'Off' { -%>
Comment on lines +14 to +16
<% if $modsec_audit_log_destination { -%>
SecAuditLog "<%= $modsec_audit_log_destination %>"
<% } -%>
<% if $_modsec_disable_ids =~ Hash {-%>
<% $_modsec_disable_ids.each |$location, $rules| {-%>
<% if $_modsec_disable_ids =~ Hash and !$_modsec_disable_ids.empty { -%>
<% $_modsec_disable_ids.each |$location, $rules| { -%>
<LocationMatch <%= $location %>>
<% Array($rules).each |$rule| {-%>
<% Array($rules).each |$rule| { -%>
SecRuleRemoveById <%= $rule %>
<% } -%>
</LocationMatch>
<% } -%>
<% } -%>
<% unless $modsec_disable_ips.empty {%>
<% unless $modsec_disable_ips.empty { -%>
SecRule REMOTE_ADDR "<%= join($modsec_disable_ips, ',') %>" "nolog,allow,id:1234123455"
SecAction "phase:2,pass,nolog,id:1234123456"
SecAction "phase:2,pass,nolog,id:1234123456"
<% } -%>
<% if $_modsec_disable_msgs =~ Hash {-%>
<% $_modsec_disable_msgs.each |$location, $rules| {-%>
<% if $_modsec_disable_msgs =~ Hash and !$_modsec_disable_msgs.empty { -%>
<% $_modsec_disable_msgs.each |$location, $rules| { -%>
<LocationMatch <%= $location %>>
<% Array($rules).each |$rule| {-%>
<% Array($rules).each |$rule| { -%>
SecRuleRemoveByMsg "<%= $rule %>"
<% } -%>
</LocationMatch>
<% } -%>
<% } -%>
<% if $_modsec_disable_tags =~ Hash {-%>
<% $_modsec_disable_tags.each |$location, $rules| {-%>
<% if $_modsec_disable_tags =~ Hash and !$_modsec_disable_tags.empty { -%>
<% $_modsec_disable_tags.each |$location, $rules| { -%>
<LocationMatch <%= $location %>>
<% Array($rules).each |$rule| {-%>
<% Array($rules).each |$rule| { -%>
SecRuleRemoveByTag "<%= $rule %>"
<% } -%>
</LocationMatch>
<% } -%>
<% } -%>
<% if $modsec_body_limit {-%>
<% if $modsec_body_limit { -%>
SecRequestBodyLimit <%= $modsec_body_limit %>
<% } -%>
<% if $modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold {-%>
<% if $modsec_inbound_anomaly_threshold and $modsec_outbound_anomaly_threshold { -%>
SecAction \
"id:900110,\
phase:1,\
nolog,\
pass,\
t:none,\
setvar:tx.inbound_anomaly_score_threshold=<%= $modsec_inbound_anomaly_threshold -%>, \
setvar:tx.outbound_anomaly_score_threshold=<%= $modsec_outbound_anomaly_threshold -%>"
setvar:tx.inbound_anomaly_score_threshold=<%= $modsec_inbound_anomaly_threshold %>, \
setvar:tx.outbound_anomaly_score_threshold=<%= $modsec_outbound_anomaly_threshold %>"
<% } -%>
<% if $modsec_allowed_methods {-%>
<% if $modsec_allowed_methods { -%>
SecAction \
"id:900200,\
phase:1,\
Expand All @@ -58,4 +70,5 @@
t:none,\
setvar:'tx.allowed_methods=<%= $modsec_allowed_methods -%>'"
<% } -%>
<% } -%>
</IfModule>
Loading