adding some security
This commit is contained in:
@@ -1,8 +1,26 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, HTTPException, status
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
allowed_host_env = os.getenv("LLM_REVIEW_ALLOWED_HOST")
|
||||
allowed_hosts = []
|
||||
allowed_hosts_not_empty = True
|
||||
|
||||
if allowed_host_env is None:
|
||||
allowed_hosts_not_empty = False
|
||||
else:
|
||||
allowed_hosts = allowed_host_env.split(',')
|
||||
if len(allowed_hosts) == 0:
|
||||
allowed_hosts_not_empty = False
|
||||
|
||||
for (i, host) in enumerate(allowed_hosts):
|
||||
allowed_hosts[i] = host.rstrip('/')
|
||||
|
||||
print(f'allowed host: {allowed_hosts}')
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
class ReviewRequest(BaseModel):
|
||||
@@ -13,15 +31,30 @@ class ReviewRequest(BaseModel):
|
||||
repo: str
|
||||
pr_number: int
|
||||
|
||||
@app.get("/health")
|
||||
def test():
|
||||
try:
|
||||
r = requests.get("https://openrouter.ai/api/v1/models")
|
||||
r.raise_for_status()
|
||||
return {"status": "ok"}
|
||||
except requests.exceptions.RequestException as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=f"OpenRouter health check failed: {e}",
|
||||
)
|
||||
|
||||
@app.post("/")
|
||||
async def review(
|
||||
def review(
|
||||
request_body: ReviewRequest,
|
||||
):
|
||||
print('starting bg task with:')
|
||||
print(f'request: {request_body}')
|
||||
|
||||
gitea_host = request_body.gitea_host.rstrip("/")
|
||||
|
||||
if allowed_hosts_not_empty and gitea_host not in allowed_hosts:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Request source is not allowed host",
|
||||
)
|
||||
|
||||
headers = {"Authorization": f"token {request_body.gitea_token}"}
|
||||
|
||||
diff_res = requests.get(f"{gitea_host}/api/v1/repos/{request_body.repo}/pulls/{request_body.pr_number}.diff", headers=headers)
|
||||
@@ -35,7 +68,8 @@ async def review(
|
||||
prompt = (
|
||||
"You are an automated senior code reviewer. Review the following git diff.\n"
|
||||
"Focus on: critical bugs, security vulnerabilities, edge cases, and code smells.\n"
|
||||
"Keep it short and clear, don't give me long solutions."
|
||||
"Keep it short and clear, don't give me long solutions.\n"
|
||||
"Dont point out good things only problems, errors, etc.\n"
|
||||
"Format your answer cleanly in Markdown with actionable feedback.\n\n"
|
||||
f"```diff\n{diff_text}\n```"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user