-
Notifications
You must be signed in to change notification settings - Fork 0
feat(backend): add config-only automatic IP banning for DDoS policies #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/backend/alembic/versions/ea9292cbacec_add_auto_ban_fields_to_policies.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """add auto ban fields to policies | ||
|
|
||
| Revision ID: ea9292cbacec | ||
| Revises: 473d83df7b55 | ||
| Create Date: 2026-07-21 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| from collections.abc import Sequence | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "ea9292cbacec" | ||
| down_revision: str | Sequence[str] | None = "473d83df7b55" | ||
| branch_labels: str | Sequence[str] | None = None | ||
| depends_on: str | Sequence[str] | None = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Upgrade schema.""" | ||
| with op.batch_alter_table("policies") as batch_op: | ||
| batch_op.add_column( | ||
| sa.Column( | ||
| "auto_ban_enabled", | ||
| sa.Boolean(), | ||
| nullable=False, | ||
| server_default=sa.false(), | ||
| ) | ||
| ) | ||
| batch_op.add_column( | ||
| sa.Column( | ||
| "ban_threshold", | ||
| sa.Integer(), | ||
| nullable=False, | ||
| server_default="10", | ||
| ) | ||
| ) | ||
| batch_op.add_column( | ||
| sa.Column( | ||
| "ban_duration_seconds", | ||
| sa.Integer(), | ||
| nullable=False, | ||
| server_default="600", | ||
| ) | ||
| ) | ||
| batch_op.create_check_constraint( | ||
| "ck_policies_ban_threshold", | ||
| "ban_threshold >= 1", | ||
| ) | ||
| batch_op.create_check_constraint( | ||
| "ck_policies_ban_duration_seconds", | ||
| "ban_duration_seconds BETWEEN 1 AND 86400", | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Downgrade schema.""" | ||
| with op.batch_alter_table("policies") as batch_op: | ||
| batch_op.drop_constraint( | ||
| "ck_policies_ban_duration_seconds", type_="check" | ||
| ) | ||
| batch_op.drop_constraint("ck_policies_ban_threshold", type_="check") | ||
| batch_op.drop_column("ban_duration_seconds") | ||
| batch_op.drop_column("ban_threshold") | ||
| batch_op.drop_column("auto_ban_enabled") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,17 @@ frontend fe_http | |
| {% for route in routes %} | ||
| {% if route.ddos and route.ddos.enabled %} | ||
| http-request track-sc0 src table {{ route.ddos.stick_table_name }} if {{ route.vhost_acl_name }} !is_health !is_acme | ||
| {% if route.ddos.auto_ban_enabled %} | ||
| # Automatic IP banning: repeated rate/conn violations trip a ban that | ||
| # lifts after ban_duration_seconds of inactivity (track-sc1 refreshes | ||
| # the entry's expiry on every request, so a persistent attacker stays | ||
| # banned). Ban tracking/increment must run before the rate/conn deny | ||
| # rules below, since `deny` stops rule evaluation for the request. | ||
| http-request track-sc1 src table {{ route.ddos.ban_stick_table_name }} if {{ route.vhost_acl_name }} !is_health !is_acme | ||
| http-request deny deny_status 429 if {{ route.vhost_acl_name }} !is_health !is_acme { sc_get_gpc0(1) gt {{ route.ddos.ban_threshold }} } | ||
| http-request sc-inc-gpc0(1) if {{ route.vhost_acl_name }} !is_health !is_acme { sc_http_req_rate(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.rate_limit_requests }} } | ||
| http-request sc-inc-gpc0(1) if {{ route.vhost_acl_name }} !is_health !is_acme { sc_conn_cur(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.max_connections_per_ip }} } | ||
| {% endif %} | ||
| http-request deny deny_status 429 if {{ route.vhost_acl_name }} !is_health !is_acme { sc_http_req_rate(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.rate_limit_requests }} } | ||
| http-request deny deny_status 429 if {{ route.vhost_acl_name }} !is_health !is_acme { sc_conn_cur(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.max_connections_per_ip }} } | ||
| {% endif %} | ||
|
|
@@ -123,6 +134,12 @@ frontend fe_https | |
| {% for route in routes %} | ||
| {% if route.ddos and route.ddos.enabled %} | ||
| http-request track-sc0 src table {{ route.ddos.stick_table_name }} if {{ route.vhost_acl_name }} !is_health !is_acme | ||
| {% if route.ddos.auto_ban_enabled %} | ||
| http-request track-sc1 src table {{ route.ddos.ban_stick_table_name }} if {{ route.vhost_acl_name }} !is_health !is_acme | ||
| http-request deny deny_status 429 if {{ route.vhost_acl_name }} !is_health !is_acme { sc_get_gpc0(1) gt {{ route.ddos.ban_threshold }} } | ||
| http-request sc-inc-gpc0(1) if {{ route.vhost_acl_name }} !is_health !is_acme { sc_http_req_rate(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.rate_limit_requests }} } | ||
| http-request sc-inc-gpc0(1) if {{ route.vhost_acl_name }} !is_health !is_acme { sc_conn_cur(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.max_connections_per_ip }} } | ||
| {% endif %} | ||
|
Comment on lines
136
to
+142
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed the same way — track-sc1/sc-inc-gpc0 now run before the rate/conn deny rules in fe_https too. (2335c78) |
||
| http-request deny deny_status 429 if {{ route.vhost_acl_name }} !is_health !is_acme { sc_http_req_rate(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.rate_limit_requests }} } | ||
| http-request deny deny_status 429 if {{ route.vhost_acl_name }} !is_health !is_acme { sc_conn_cur(0,{{ route.ddos.stick_table_name }}) gt {{ route.ddos.max_connections_per_ip }} } | ||
| {% endif %} | ||
|
|
@@ -160,6 +177,11 @@ frontend fe_https | |
| backend {{ route.ddos.stick_table_name }} | ||
| stick-table type ipv6 size 100k expire {{ route.ddos.rate_limit_window_seconds }}s store http_req_rate({{ route.ddos.rate_limit_window_seconds }}s),conn_cur | ||
|
|
||
| {% if route.ddos.auto_ban_enabled %} | ||
| backend {{ route.ddos.ban_stick_table_name }} | ||
| stick-table type ipv6 size 100k expire {{ route.ddos.ban_duration_seconds }}s store gpc0 | ||
|
|
||
| {% endif %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {% endif %} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed — real bug. Reordered so track-sc1, the already-banned check, and both sc-inc-gpc0 rules run before the rate/conn deny rules in both fe_http and fe_https, so the abuse counter increments and the ban table's expiry refreshes on every tracked request as intended. (2335c78)