Skip to content

Official Crawl4AI Cloud: a hosted web scraping, extraction, and search API for AI agents. Free API key, 1,000 credits a month, no card. Fully open source: this repo is the Apache-2.0 crawler that powers it, and you can self-host it.

fix(security): parameterise marketplace backend queries

ntohidi978dbb508287
diff --git a/docs/md_v2/marketplace/backend/database.py b/docs/md_v2/marketplace/backend/database.py
index 8ccfbaf4..0fb86098 100644
--- a/docs/md_v2/marketplace/backend/database.py
+++ b/docs/md_v2/marketplace/backend/database.py
@@ -77,14 +77,16 @@ class DatabaseManager:
self.conn.commit()
- def get_all(self, table: str, limit: int = 100, offset: int = 0, where: str = None) -> List[Dict]:
+ def get_all(self, table: str, limit: int = 100, offset: int = 0, where: str = None, params: List = None) -> List[Dict]:
cursor = self.conn.cursor()
query = f"SELECT * FROM {table}"
+ query_params = list(params) if params else []
if where:
query += f" WHERE {where}"
- query += f" LIMIT {limit} OFFSET {offset}"
+ query += " LIMIT ? OFFSET ?"
+ query_params += [limit, offset]
- cursor.execute(query)
+ cursor.execute(query, query_params)
rows = cursor.fetchall()
return [dict(row) for row in rows]
diff --git a/docs/md_v2/marketplace/backend/server.py b/docs/md_v2/marketplace/backend/server.py
index f4935eb6..d66965a2 100644
--- a/docs/md_v2/marketplace/backend/server.py
+++ b/docs/md_v2/marketplace/backend/server.py
@@ -96,17 +96,22 @@ async def get_apps(
):
"""Get apps with optional filters"""
where_clauses = []
+ params = []
if category:
- where_clauses.append(f"category = '{category}'")
+ where_clauses.append("category = ?")
+ params.append(category)
if type:
- where_clauses.append(f"type = '{type}'")
+ where_clauses.append("type = ?")
+ params.append(type)
if featured is not None:
- where_clauses.append(f"featured = {1 if featured else 0}")
+ where_clauses.append("featured = ?")
+ params.append(1 if featured else 0)
if sponsored is not None:
- where_clauses.append(f"sponsored = {1 if sponsored else 0}")
+ where_clauses.append("sponsored = ?")
+ params.append(1 if sponsored else 0)
where = " AND ".join(where_clauses) if where_clauses else None
- apps = db.get_all('apps', limit=limit, offset=offset, where=where)
+ apps = db.get_all('apps', limit=limit, offset=offset, where=where, params=params)
# Parse JSON fields
for app in apps:
@@ -118,7 +123,7 @@ async def get_apps(
@router.get("/apps/{slug}")
async def get_app(slug: str):
"""Get single app by slug"""
- apps = db.get_all('apps', where=f"slug = '{slug}'", limit=1)
+ apps = db.get_all('apps', where="slug = ?", params=[slug], limit=1)
if not apps:
raise HTTPException(status_code=404, detail="App not found")
@@ -135,8 +140,9 @@ async def get_articles(
offset: int = Query(default=0)
):
"""Get articles with optional category filter"""
- where = f"category = '{category}'" if category else None
- articles = db.get_all('articles', limit=limit, offset=offset, where=where)
+ where = "category = ?" if category else None
+ params = [category] if category else []
+ articles = db.get_all('articles', limit=limit, offset=offset, where=where, params=params)
# Parse JSON fields
for article in articles:
@@ -150,7 +156,7 @@ async def get_articles(
@router.get("/articles/{slug}")
async def get_article(slug: str):
"""Get single article by slug"""
- articles = db.get_all('articles', where=f"slug = '{slug}'", limit=1)
+ articles = db.get_all('articles', where="slug = ?", params=[slug], limit=1)
if not articles:
raise HTTPException(status_code=404, detail="Article not found")
@@ -174,8 +180,9 @@ async def get_categories():
@router.get("/sponsors")
async def get_sponsors(active: Optional[bool] = True):
"""Get sponsors, default active only"""
- where = f"active = {1 if active else 0}" if active is not None else None
- sponsors = db.get_all('sponsors', where=where, limit=20)
+ where = "active = ?" if active is not None else None
+ params = [1 if active else 0] if active is not None else []
+ sponsors = db.get_all('sponsors', where=where, params=params, limit=20)
# Filter by date if active
if active:
@@ -228,6 +235,14 @@ def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
return token
+def validate_columns(table: str, data: Dict[str, Any]):
+ """Reject any field that isn't a real column on this table"""
+ allowed = db.schema['tables'][table]['columns'].keys()
+ unknown = [k for k in data.keys() if k not in allowed]
+ if unknown:
+ raise HTTPException(status_code=400, detail=f"Unknown field(s): {', '.join(unknown)}")
+
+
@router.post("/admin/upload-image", dependencies=[Depends(verify_token)])
async def upload_image(file: UploadFile = File(...), folder: str = Form("sponsors")):
"""Upload image files for admin assets"""
@@ -296,6 +311,7 @@ async def get_admin_stats():
@router.post("/admin/apps", dependencies=[Depends(verify_token)])
async def create_app(app_data: Dict[str, Any]):
"""Create new app"""
+ validate_columns('apps', app_data)
try:
# Handle JSON fields
for field in ['screenshots', 'tags']:
@@ -310,11 +326,13 @@ async def create_app(app_data: Dict[str, Any]):
db.conn.commit()
return {"id": cursor.lastrowid, "message": "App created"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error creating app: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.put("/admin/apps/{app_id}", dependencies=[Depends(verify_token)])
async def update_app(app_id: int, app_data: Dict[str, Any]):
"""Update app"""
+ validate_columns('apps', app_data)
try:
# Handle JSON fields
for field in ['screenshots', 'tags']:
@@ -328,7 +346,8 @@ async def update_app(app_id: int, app_data: Dict[str, Any]):
db.conn.commit()
return {"message": "App updated"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error updating app: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.delete("/admin/apps/{app_id}", dependencies=[Depends(verify_token)])
async def delete_app(app_id: int):
@@ -342,6 +361,7 @@ async def delete_app(app_id: int):
@router.post("/admin/articles", dependencies=[Depends(verify_token)])
async def create_article(article_data: Dict[str, Any]):
"""Create new article"""
+ validate_columns('articles', article_data)
try:
for field in ['related_apps', 'tags']:
if field in article_data and isinstance(article_data[field], list):
@@ -355,11 +375,13 @@ async def create_article(article_data: Dict[str, Any]):
db.conn.commit()
return {"id": cursor.lastrowid, "message": "Article created"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error creating article: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.put("/admin/articles/{article_id}", dependencies=[Depends(verify_token)])
async def update_article(article_id: int, article_data: Dict[str, Any]):
"""Update article"""
+ validate_columns('articles', article_data)
try:
for field in ['related_apps', 'tags']:
if field in article_data and isinstance(article_data[field], list):
@@ -372,7 +394,8 @@ async def update_article(article_id: int, article_data: Dict[str, Any]):
db.conn.commit()
return {"message": "Article updated"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error updating article: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.delete("/admin/articles/{article_id}", dependencies=[Depends(verify_token)])
async def delete_article(article_id: int):
@@ -386,6 +409,7 @@ async def delete_article(article_id: int):
@router.post("/admin/categories", dependencies=[Depends(verify_token)])
async def create_category(category_data: Dict[str, Any]):
"""Create new category"""
+ validate_columns('categories', category_data)
try:
category_data = dict(category_data)
category_data['order_index'] = to_int(category_data.get('order_index'), 0)
@@ -398,11 +422,13 @@ async def create_category(category_data: Dict[str, Any]):
db.conn.commit()
return {"id": cursor.lastrowid, "message": "Category created"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error creating category: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.put("/admin/categories/{cat_id}", dependencies=[Depends(verify_token)])
async def update_category(cat_id: int, category_data: Dict[str, Any]):
"""Update category"""
+ validate_columns('categories', category_data)
try:
category_data = dict(category_data)
if 'order_index' in category_data:
@@ -415,7 +441,8 @@ async def update_category(cat_id: int, category_data: Dict[str, Any]):
db.conn.commit()
return {"message": "Category updated"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error updating category: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.delete("/admin/categories/{cat_id}", dependencies=[Depends(verify_token)])
@@ -427,12 +454,14 @@ async def delete_category(cat_id: int):
db.conn.commit()
return {"message": "Category deleted"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error deleting category: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
# Sponsors CRUD
@router.post("/admin/sponsors", dependencies=[Depends(verify_token)])
async def create_sponsor(sponsor_data: Dict[str, Any]):
"""Create new sponsor"""
+ validate_columns('sponsors', sponsor_data)
try:
cursor = db.conn.cursor()
columns = ', '.join(sponsor_data.keys())
@@ -442,11 +471,13 @@ async def create_sponsor(sponsor_data: Dict[str, Any]):
db.conn.commit()
return {"id": cursor.lastrowid, "message": "Sponsor created"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error creating sponsor: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.put("/admin/sponsors/{sponsor_id}", dependencies=[Depends(verify_token)])
async def update_sponsor(sponsor_id: int, sponsor_data: Dict[str, Any]):
"""Update sponsor"""
+ validate_columns('sponsors', sponsor_data)
try:
set_clause = ', '.join([f"{k} = ?" for k in sponsor_data.keys()])
cursor = db.conn.cursor()
@@ -455,7 +486,8 @@ async def update_sponsor(sponsor_id: int, sponsor_data: Dict[str, Any]):
db.conn.commit()
return {"message": "Sponsor updated"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error updating sponsor: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
@router.delete("/admin/sponsors/{sponsor_id}", dependencies=[Depends(verify_token)])
@@ -467,7 +499,8 @@ async def delete_sponsor(sponsor_id: int):
db.conn.commit()
return {"message": "Sponsor deleted"}
except Exception as e:
- raise HTTPException(status_code=400, detail=str(e))
+ print(f"Error deleting sponsor: {e}")
+ raise HTTPException(status_code=400, detail="Invalid request")
app.include_router(router)
diff --git a/docs/md_v2/marketplace/backend/test_sql_injection.py b/docs/md_v2/marketplace/backend/test_sql_injection.py
new file mode 100644
index 00000000..5f807fac
--- /dev/null
+++ b/docs/md_v2/marketplace/backend/test_sql_injection.py
@@ -0,0 +1,150 @@
+"""SQL injection regression test for the marketplace backend.
+
+Runs the real server.py on a copy of the backend folder, with a throwaway .env,
+same approach as the private repro script. No fixtures, standalone pytest file.
+
+Run with: <python with fastapi/httpx/pyyaml/dotenv/multipart> -m pytest test_sql_injection.py -v
+"""
+import os
+import shutil
+import sys
+import tempfile
+from pathlib import Path
+
+import pytest
+
+SRC = Path(__file__).resolve().parent
+
+
[email protected](scope="module")
+def client():
+ work = Path(tempfile.mkdtemp()) / "backend"
+ shutil.copytree(SRC, work, ignore=shutil.ignore_patterns("__pycache__", "uploads", "test_sql_injection.py"))
+ (work / ".env").write_text("MARKETPLACE_ADMIN_PASSWORD=x\nMARKETPLACE_JWT_SECRET=y\n")
+
+ old_cwd = os.getcwd()
+ os.chdir(work)
+ sys.path.insert(0, str(work))
+
+ # Fresh import each run since module state (db connection) is process-global
+ sys.modules.pop("server", None)
+ sys.modules.pop("database", None)
+ sys.modules.pop("config", None)
+ import server
+
+ from fastapi.testclient import TestClient
+ c = TestClient(server.app, raise_server_exceptions=False)
+ c.server_module = server
+ yield c
+
+ os.chdir(old_cwd)
+ sys.path.remove(str(work))
+
+
[email protected](scope="module")
+def admin_token(client):
+ r = client.post("/marketplace/api/admin/login", json={"password": "x"})
+ assert r.status_code == 200
+ return r.json()["token"]
+
+
+BASE = "/marketplace/api"
+
+
+def test_apps_category_union_injection_blocked(client):
+ cols = len(client.server_module.db.conn.execute("PRAGMA table_info(apps)").fetchall())
+ union = "' UNION SELECT " + ",".join(["NULL", "sql"] + ["NULL"] * (cols - 2)) + " FROM sqlite_master WHERE type='table'--"
+ r = client.get(f"{BASE}/apps", params={"category": union})
+ assert r.status_code == 200
+ assert r.json() == [] # no app has this literal category string, and no injected rows
+
+
+def test_apps_type_union_injection_blocked(client):
+ cols = len(client.server_module.db.conn.execute("PRAGMA table_info(apps)").fetchall())
+ union = "' UNION SELECT " + ",".join(["NULL", "sql"] + ["NULL"] * (cols - 2)) + " FROM sqlite_master WHERE type='table'--"
+ r = client.get(f"{BASE}/apps", params={"type": union})
+ assert r.status_code == 200
+ assert r.json() == []
+
+
+def test_app_slug_or_true_injection_blocked(client):
+ r = client.get(f"{BASE}/apps/x' OR '1'='1")
+ assert r.status_code == 404
+
+
+def test_articles_category_union_injection_blocked(client):
+ cols = len(client.server_module.db.conn.execute("PRAGMA table_info(articles)").fetchall())
+ union = "' UNION SELECT " + ",".join(["NULL", "sql"] + ["NULL"] * (cols - 2)) + " FROM sqlite_master WHERE type='table'--"
+ r = client.get(f"{BASE}/articles", params={"category": union})
+ assert r.status_code == 200
+ assert r.json() == []
+
+
+def test_article_slug_or_true_injection_blocked(client):
+ r = client.get(f"{BASE}/articles/x' OR '1'='1")
+ assert r.status_code == 404
+
+
+def test_normal_filters_still_work(client):
+ """A real category value must still return its apps."""
+ cur = client.server_module.db.conn
+ cur.execute(
+ "INSERT INTO apps (name, slug, category) VALUES ('Demo App', 'demo-app', 'demo-category')"
+ )
+ cur.commit()
+
+ r = client.get(f"{BASE}/apps", params={"category": "demo-category"})
+ assert r.status_code == 200
+ body = r.json()
+ assert len(body) == 1
+ assert body[0]["slug"] == "demo-app"
+
+
+def test_admin_create_app_rejects_unknown_column(client, admin_token):
+ r = client.post(
+ f"{BASE}/admin/apps",
+ json={"name": "x", "slug) VALUES ('x'); DROP TABLE apps; --": "x"},
+ headers={"Authorization": f"Bearer {admin_token}"},
+ )
+ # Rejected before any SQL runs, so the malicious key is never executed
+ assert r.status_code == 400
+ tables = client.server_module.db.conn.execute(
+ "SELECT name FROM sqlite_master WHERE type='table'"
+ ).fetchall()
+ assert any(row[0] == "apps" for row in tables)
+
+
+def test_admin_update_app_rejects_unknown_column(client, admin_token):
+ r = client.post(
+ f"{BASE}/admin/apps",
+ json={"name": "victim", "slug": "victim-app"},
+ headers={"Authorization": f"Bearer {admin_token}"},
+ )
+ assert r.status_code == 200
+ app_id = r.json()["id"]
+
+ r = client.put(
+ f"{BASE}/admin/apps/{app_id}",
+ json={"not_a_real_column": "evil"},
+ headers={"Authorization": f"Bearer {admin_token}"},
+ )
+ assert r.status_code == 400
+ assert r.json()["detail"] != "not_a_real_column"
+
+
+def test_admin_create_app_error_detail_is_generic(client, admin_token):
+ """Forcing a real DB error (duplicate unique slug) must not leak str(e)."""
+ client.post(
+ f"{BASE}/admin/apps",
+ json={"name": "dup", "slug": "dup-app"},
+ headers={"Authorization": f"Bearer {admin_token}"},
+ )
+ r = client.post(
+ f"{BASE}/admin/apps",
+ json={"name": "dup2", "slug": "dup-app"},
+ headers={"Authorization": f"Bearer {admin_token}"},
+ )
+ assert r.status_code == 400
+ detail = r.json()["detail"]
+ assert "UNIQUE constraint" not in detail
+ assert "sqlite3" not in detail.lower()