Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8ed5b690f |
@@ -12,6 +12,7 @@ from fastapi import FastAPI, Request
|
|||||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from starlette.middleware.gzip import GZipMiddleware
|
||||||
from devplacepy.config import (
|
from devplacepy.config import (
|
||||||
STATIC_DIR,
|
STATIC_DIR,
|
||||||
STATIC_VERSION,
|
STATIC_VERSION,
|
||||||
@@ -609,6 +610,10 @@ async def response_timing(request: Request, call_next):
|
|||||||
response.headers["X-Response-Time"] = f"{(time.perf_counter() - start) * 1000:.1f}ms"
|
response.headers["X-Response-Time"] = f"{(time.perf_counter() - start) * 1000:.1f}ms"
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
app.add_middleware(GZipMiddleware, minimum_size=512, compresslevel=5)
|
||||||
|
|
||||||
|
|
||||||
_home_cache = TTLCache(ttl=int(os.environ.get("DEVPLACE_HOME_CACHE_TTL", "60")), max_size=4)
|
_home_cache = TTLCache(ttl=int(os.environ.get("DEVPLACE_HOME_CACHE_TTL", "60")), max_size=4)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -470,9 +470,19 @@ class SeoRunForm(BaseModel):
|
|||||||
text = value.strip()
|
text = value.strip()
|
||||||
if not text:
|
if not text:
|
||||||
raise ValueError("A URL is required")
|
raise ValueError("A URL is required")
|
||||||
|
if "://" in text:
|
||||||
|
scheme = text.split("://", 1)[0]
|
||||||
|
if scheme not in ("http", "https"):
|
||||||
|
raise ValueError(f"Only http and https URLs are allowed; got '{scheme}://'")
|
||||||
|
else:
|
||||||
|
text = f"https://{text}"
|
||||||
|
if not SEO_URL_PATTERN.match(text):
|
||||||
|
raise ValueError("URL must be a valid http or https source location")
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
SEO_URL_PATTERN = re.compile(r"^https?://[a-zA-Z0-9][\w./:@~^?&#%=;-]*$")
|
||||||
|
|
||||||
ISSLOP_URL_PATTERN = re.compile(r"^(https?://|git://|ssh://|git@)[\w./:@~^-]+$", re.IGNORECASE)
|
ISSLOP_URL_PATTERN = re.compile(r"^(https?://|git://|ssh://|git@)[\w./:@~^-]+$", re.IGNORECASE)
|
||||||
ISSLOP_SINGLE_SLASH_PATTERN = re.compile(r"^(https?|git|ssh):/(?!/)", re.IGNORECASE)
|
ISSLOP_SINGLE_SLASH_PATTERN = re.compile(r"^(https?|git|ssh):/(?!/)", re.IGNORECASE)
|
||||||
ISSLOP_SCHEME_PATTERN = re.compile(r"^[a-z][a-z0-9+.-]*://", re.IGNORECASE)
|
ISSLOP_SCHEME_PATTERN = re.compile(r"^[a-z][a-z0-9+.-]*://", re.IGNORECASE)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ How a request flows through middleware to a router, how the database layer is bu
|
|||||||
|
|
||||||
## Middleware
|
## Middleware
|
||||||
|
|
||||||
Six HTTP middlewares run as a stack around every request, listed outermost first. `response_timing` is the outermost and `refresh_db_snapshot` the innermost. Compression is handled by nginx in production; the Python application does not compress responses itself.
|
Seven HTTP middlewares run as a stack around every request, listed outermost first. `response_timing` is the outermost, `refresh_db_snapshot` the innermost, and a `GZipMiddleware` (responses over 512 bytes) wraps the whole stack on top:
|
||||||
|
|
||||||
| Middleware (outermost first) | Responsibility |
|
| Middleware (outermost first) | Responsibility |
|
||||||
|------------|----------------|
|
|------------|----------------|
|
||||||
|
|||||||
+1
-2
@@ -4,8 +4,7 @@ version = "1.0.0"
|
|||||||
description = "DevPlace - The Developer Social Network"
|
description = "DevPlace - The Developer Social Network"
|
||||||
requires-python = ">=3.12"
|
requires-python = ">=3.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fastapi>=0.110.0",
|
"fastapi",
|
||||||
"starlette>=0.37.0",
|
|
||||||
"uvicorn[standard]",
|
"uvicorn[standard]",
|
||||||
"jinja2",
|
"jinja2",
|
||||||
"python-multipart",
|
"python-multipart",
|
||||||
|
|||||||
@@ -89,6 +89,53 @@ def test_run_clamps_max_pages_above_cap(app_server):
|
|||||||
_clear_seo_jobs()
|
_clear_seo_jobs()
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_rejects_malformed_scheme(app_server):
|
||||||
|
r = requests.post(
|
||||||
|
f"{BASE_URL}/tools/seo/run",
|
||||||
|
headers=_json_headers(),
|
||||||
|
data={"url": "ahttps://devplace.net/sitem", "mode": "url"},
|
||||||
|
allow_redirects=False,
|
||||||
|
)
|
||||||
|
assert r.status_code in (400, 422), r.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_rejects_relative_path(app_server):
|
||||||
|
r = requests.post(
|
||||||
|
f"{BASE_URL}/tools/seo/run",
|
||||||
|
headers=_json_headers(),
|
||||||
|
data={"url": "/feed", "mode": "url"},
|
||||||
|
allow_redirects=False,
|
||||||
|
)
|
||||||
|
assert r.status_code in (400, 422), r.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_normalizes_missing_scheme(app_server):
|
||||||
|
try:
|
||||||
|
r = requests.post(
|
||||||
|
f"{BASE_URL}/tools/seo/run",
|
||||||
|
headers=_json_headers(),
|
||||||
|
data={"url": "example.com", "mode": "url", "max_pages": "5"},
|
||||||
|
)
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
uid = r.json()["uid"]
|
||||||
|
refresh_snapshot()
|
||||||
|
job = queue.get_job(uid)
|
||||||
|
assert job is not None
|
||||||
|
assert job["payload"]["url"] == "https://example.com"
|
||||||
|
finally:
|
||||||
|
_clear_seo_jobs()
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_rejects_non_http_scheme(app_server):
|
||||||
|
r = requests.post(
|
||||||
|
f"{BASE_URL}/tools/seo/run",
|
||||||
|
headers=_json_headers(),
|
||||||
|
data={"url": "ftp://example.com", "mode": "url"},
|
||||||
|
allow_redirects=False,
|
||||||
|
)
|
||||||
|
assert r.status_code in (400, 422), r.text
|
||||||
|
|
||||||
|
|
||||||
def test_run_enforces_one_active_job_per_owner(app_server):
|
def test_run_enforces_one_active_job_per_owner(app_server):
|
||||||
try:
|
try:
|
||||||
first = requests.post(
|
first = requests.post(
|
||||||
|
|||||||
Reference in New Issue
Block a user