ar07xd commited on
Commit
6a4e518
·
verified ·
1 Parent(s): 3681f82

Sync from GitHub via hub-sync

Browse files
Files changed (1) hide show
  1. config.py +37 -3
config.py CHANGED
@@ -1,4 +1,5 @@
1
  import json
 
2
  from typing import Any
3
  from pydantic import field_validator, model_validator
4
  from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -42,6 +43,41 @@ def _normalize_origin(origin: str) -> str:
42
  return cleaned
43
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  class Settings(BaseSettings):
46
  # Server
47
  APP_HOST: str = "0.0.0.0"
@@ -82,9 +118,7 @@ class Settings(BaseSettings):
82
  raw = v.strip()
83
  if not raw:
84
  return "sqlite:///./deepshield.db"
85
- if raw.startswith("postgres://"):
86
- return "postgresql://" + raw[len("postgres://") :]
87
- return raw
88
  return str(v)
89
 
90
  # File Upload
 
1
  import json
2
+ from urllib.parse import parse_qsl, urlencode
3
  from typing import Any
4
  from pydantic import field_validator, model_validator
5
  from pydantic_settings import BaseSettings, SettingsConfigDict
 
43
  return cleaned
44
 
45
 
46
+ def _fix_postgres_url(raw: str) -> str:
47
+ """Normalize common Postgres URL mistakes from deployment envs.
48
+
49
+ - Converts postgres:// to postgresql://
50
+ - Encodes stray '@' in credentials (usually from unescaped passwords)
51
+ - Ensures sslmode=require for Supabase URLs when missing
52
+ """
53
+ url = raw.strip()
54
+ if url.startswith("postgres://"):
55
+ url = "postgresql://" + url[len("postgres://") :]
56
+
57
+ if not url.startswith("postgresql://"):
58
+ return url
59
+
60
+ # Split scheme + authority/path safely without full URL parsing.
61
+ rest = url[len("postgresql://") :]
62
+ if "@" in rest:
63
+ userinfo, remainder = rest.rsplit("@", 1)
64
+ # Any '@' left in userinfo belongs to credentials and must be percent-encoded.
65
+ userinfo = userinfo.replace("@", "%40")
66
+ url = "postgresql://" + userinfo + "@" + remainder
67
+
68
+ if "supabase.co" in url:
69
+ if "?" in url:
70
+ base, query = url.split("?", 1)
71
+ params = dict(parse_qsl(query, keep_blank_values=True))
72
+ if "sslmode" not in params:
73
+ params["sslmode"] = "require"
74
+ url = base + "?" + urlencode(params)
75
+ else:
76
+ url = url + "?sslmode=require"
77
+
78
+ return url
79
+
80
+
81
  class Settings(BaseSettings):
82
  # Server
83
  APP_HOST: str = "0.0.0.0"
 
118
  raw = v.strip()
119
  if not raw:
120
  return "sqlite:///./deepshield.db"
121
+ return _fix_postgres_url(raw)
 
 
122
  return str(v)
123
 
124
  # File Upload