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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ USER 10001

COPY --chown=app:app pyproject.toml uv.lock* README.md /app/

RUN uv sync --extra dev --frozen
RUN uv sync --extra dev

ENV PATH="/app/.venv/bin:$PATH"

Expand Down
3 changes: 1 addition & 2 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
args:
PYTHON_VERSION: ${PYTHON_VERSION:-3.10}
image: flaskapi-guard-example-py${PYTHON_VERSION:-3.10}
command: gunicorn examples.main:app --bind 0.0.0.0:8000 --reload
command: gunicorn examples.simple_app.main:app --bind 0.0.0.0:8000 --reload
ports:
- "8000:8000"
environment:
Expand Down Expand Up @@ -36,7 +36,6 @@ services:
- ./flaskapi_guard:/app/flaskapi_guard:z
- ./tests:/app/tests:z
- ./pyproject.toml:/app/pyproject.toml:z
- ./uv.lock:/app/uv.lock:z
- ./vulture_whitelist.py:/app/vulture_whitelist.py:z
depends_on:
redis:
Expand Down
1 change: 1 addition & 0 deletions examples/advanced_app/app/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def custom_response_modifier(resp: Response) -> Response:
auto_ban_threshold=5,
auto_ban_duration=300,
enable_penetration_detection=True,
behavior_scan_response_body=True,
enable_redis=True,
redis_url=os.environ.get("REDIS_URL", "redis://localhost:6379"),
redis_prefix=os.environ.get("REDIS_PREFIX", "flaskapi_guard:"),
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@

IPINFO_TOKEN = str(os.getenv("IPINFO_TOKEN"))
REDIS_URL = str(os.getenv("REDIS_URL"))


@pytest.fixture(scope="session", autouse=True)
def _engine_default_redis_url_follows_redis_url() -> None:
# A SecurityConfig built without redis_url falls back to localhost:6379, which
# is not where Redis lives inside the compose network.
SecurityConfig.model_fields["redis_url"].default = (
os.getenv("REDIS_URL") or "redis://localhost:6379"
)
SecurityConfig.model_rebuild(force=True)


REDIS_PREFIX = str(os.getenv("REDIS_PREFIX"))


Expand Down
4 changes: 2 additions & 2 deletions tests/test_decorators/test_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def test_honeypot_form_detection(security_config: SecurityConfig) -> None:
"application/x-www-form-urlencoded" if key == "content-type" else default
)
mock_request.body.return_value = b"bot_trap=filled"
mock_request.read_body_prefix.return_value = b"bot_trap=filled"
mock_request.read_body_prefix = lambda _max_bytes: b"bot_trap=filled"

result = validator(mock_request)
assert result.status_code == 403
Expand Down Expand Up @@ -278,7 +278,7 @@ def test_honeypot_json_detection(security_config: SecurityConfig) -> None:
"application/json" if key == "content-type" else default
)
mock_request.body.return_value = b'{"spam_check": "filled"}'
mock_request.read_body_prefix.return_value = b'{"spam_check": "filled"}'
mock_request.read_body_prefix = lambda _max_bytes: b'{"spam_check": "filled"}'

result = validator(mock_request)
assert result.status_code == 403
5 changes: 4 additions & 1 deletion tests/test_decorators/test_extension_integration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Any, cast
from unittest.mock import MagicMock, Mock, patch

Expand All @@ -11,6 +12,8 @@
from flaskapi_guard.adapters import FlaskGuardResponse
from flaskapi_guard.extension import FlaskAPIGuard

REDIS_URL = os.getenv("REDIS_URL") or "redis://localhost:6379"


def test_set_decorator_handler() -> None:
"""Test set_decorator_handler method."""
Expand Down Expand Up @@ -575,7 +578,7 @@ def test_endpoint() -> str:

def test_route_specific_rate_limit_with_redis() -> None:
app = Flask(__name__)
config = SecurityConfig(enable_redis=True, redis_url="redis://localhost:6379")
config = SecurityConfig(enable_redis=True, redis_url=REDIS_URL)
config.enable_penetration_detection = False

decorator = SecurityDecorator(config)
Expand Down
Loading