Skip to content
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

add regex validation #420

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
13 changes: 13 additions & 0 deletions libs/infinity_emb/infinity_emb/infinity_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import asyncio
import os
import re
import signal
import sys
import time
Expand Down Expand Up @@ -739,6 +740,16 @@ def _construct(name: str):
envvar=f"`{MANAGER.to_name(name)}`",
)

def validate_url(path: str):
"""
This regex matches:
- An empty string or A single '/'
- A string that starts with '/' and does not end with '/'
"""
if re.match(r"^$|^/$|^/.*[^/]$", path):
return path
raise typer.BadParameter("Path must start with '/' and must not end with '/'")
michaelfeil marked this conversation as resolved.
Show resolved Hide resolved

@tp.command("v2")
def v2(
# t
Expand Down Expand Up @@ -813,6 +824,7 @@ def v2(
),
url_prefix: str = typer.Option(
**_construct("url_prefix"),
callback=validate_url,
help="prefix for all routes of the FastAPI uvicorn server. Useful if you run behind a proxy / cascaded API.",
),
redirect_slash: str = typer.Option(
Expand Down Expand Up @@ -928,6 +940,7 @@ def v2(
uvicorn.run(app, host=host, port=port, log_level=log_level.name)

def cli():
CHECK_TYPER.mark_required()
if len(sys.argv) == 1 or sys.argv[1] not in ["v1", "v2", "help", "--help"]:
for _ in range(3):
logger.error(
Expand Down
Loading