-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
202 lines (161 loc) · 5.67 KB
/
models.py
File metadata and controls
202 lines (161 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""Pydantic models for request/response validation."""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field, field_validator
import bleach
def sanitize_text(value: str) -> str:
"""Sanitize text to prevent XSS."""
if value is None:
return ""
# Strip all HTML tags
return bleach.clean(value, tags=[], strip=True)
PRIORITY_VALUES = ["highest", "high", "medium", "low", "lowest"]
class ProjectCreate(BaseModel):
"""Schema for creating a project."""
name: str = Field(..., min_length=1, max_length=255)
description: str = Field(default="", max_length=2000)
path: str = Field(default="", max_length=500)
notes: str = Field(default="", max_length=10000)
priority: str = Field(default="medium")
@field_validator("name", "description", "notes", mode="before")
@classmethod
def sanitize(cls, v):
return sanitize_text(str(v)) if v else ""
@field_validator("priority", mode="before")
@classmethod
def validate_priority(cls, v):
if v is None:
return "medium"
v = str(v).lower()
return v if v in PRIORITY_VALUES else "medium"
class ProjectUpdate(BaseModel):
"""Schema for updating a project."""
name: Optional[str] = Field(None, min_length=1, max_length=255)
description: Optional[str] = Field(None, max_length=2000)
path: Optional[str] = Field(None, max_length=500)
notes: Optional[str] = Field(None, max_length=10000)
priority: Optional[str] = Field(None)
paused: Optional[bool] = Field(None)
@field_validator("name", "description", "notes", mode="before")
@classmethod
def sanitize(cls, v):
if v is None:
return None
return sanitize_text(str(v))
@field_validator("priority", mode="before")
@classmethod
def validate_priority(cls, v):
if v is None:
return None
v = str(v).lower()
return v if v in PRIORITY_VALUES else None
class ProjectResponse(BaseModel):
"""Schema for project response."""
id: int
name: str
description: str
path: str
notes: str
priority: str
paused: bool
position: int
created_at: Optional[datetime]
tasks: List["TaskResponse"] = []
services: List["ServiceResponse"] = []
class ProjectReorder(BaseModel):
"""Schema for reordering projects."""
project_ids: List[int] = Field(..., min_length=1)
class TaskCreate(BaseModel):
"""Schema for creating a task."""
title: str = Field(..., min_length=1, max_length=500)
priority: int = Field(default=0, ge=0)
@field_validator("title", mode="before")
@classmethod
def sanitize(cls, v):
return sanitize_text(str(v)) if v else ""
class TaskUpdate(BaseModel):
"""Schema for updating a task."""
title: Optional[str] = Field(None, min_length=1, max_length=500)
completed: Optional[bool] = None
highlighted: Optional[bool] = None
blue_highlighted: Optional[bool] = None
priority: Optional[int] = Field(None, ge=0)
@field_validator("title", mode="before")
@classmethod
def sanitize(cls, v):
return sanitize_text(str(v)) if v else None
class TaskResponse(BaseModel):
"""Schema for task response."""
id: int
project_id: int
title: str
completed: bool
highlighted: bool
blue_highlighted: bool
priority: int
created_at: Optional[datetime]
class TaskReorder(BaseModel):
"""Schema for reordering tasks."""
task_ids: List[int] = Field(..., min_length=1)
class ServiceCreate(BaseModel):
"""Schema for creating a service."""
name: str = Field(..., min_length=1, max_length=255)
check_type: str = Field(default="http", pattern="^(http|tcp|command)$")
target: str = Field(..., min_length=1, max_length=500)
@field_validator("name", mode="before")
@classmethod
def sanitize_name(cls, v):
return sanitize_text(str(v)) if v else ""
@field_validator("target", mode="before")
@classmethod
def validate_target(cls, v):
"""Basic validation for service targets."""
if not v:
return ""
v = str(v).strip()
# Allow localhost, IPs, and domain names
# Block obviously dangerous patterns
dangerous = ["&&", "||", ";", "|", "`", "$", "$(", "${"]
for d in dangerous:
if d in v:
raise ValueError(f"Invalid character sequence in target: {d}")
return v
class ServiceUpdate(BaseModel):
"""Schema for updating a service."""
name: Optional[str] = Field(None, min_length=1, max_length=255)
check_type: Optional[str] = Field(None, pattern="^(http|tcp|command)$")
target: Optional[str] = Field(None, min_length=1, max_length=500)
@field_validator("name", mode="before")
@classmethod
def sanitize_name(cls, v):
return sanitize_text(str(v)) if v else None
@field_validator("target", mode="before")
@classmethod
def validate_target(cls, v):
if v is None:
return None
v = str(v).strip()
dangerous = ["&&", "||", ";", "|", "`", "$", "$(", "${"]
for d in dangerous:
if d in v:
raise ValueError(f"Invalid character sequence in target: {d}")
return v
class ServiceResponse(BaseModel):
"""Schema for service response."""
id: int
project_id: int
name: str
check_type: str
target: str
status: str
status_code: Optional[int]
response_time_ms: Optional[int]
last_error: Optional[str]
last_checked: Optional[datetime]
consecutive_failures: int
class SSEEvent(BaseModel):
"""Schema for SSE events."""
event_type: str
data: dict
# Forward references
ProjectResponse.model_rebuild()