-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
211 lines (144 loc) · 4.95 KB
/
schema.py
File metadata and controls
211 lines (144 loc) · 4.95 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
203
204
205
206
207
208
209
210
211
import enum
from enum import StrEnum
from typing import Annotated
from typing import Union
from typing import Literal
from pydantic import AfterValidator
from pydantic import BaseModel
from pydantic import BeforeValidator
from pydantic import Field
from pydantic import HttpUrl
from sssig_rules import hscheck # type: ignore
def ensure_valid_range(value: int | list[int]) -> list[int]:
if isinstance(value, int):
return [value, value]
if len(value) != 2:
raise ValueError(f"{value} must contain two values")
if value[0] >= value[1]:
raise ValueError(f"the first number in {value} must be smaller")
return value
def is_valid_hs_pattern(raw_pattern: str) -> str:
"""
Make sure the pattern is a valid hyperscan pattern
"""
err = hscheck.validate_pattern(raw_pattern)
if err:
raise ValueError(err)
return raw_pattern
RuleId = Annotated[str, Field(pattern="^S3IG[A-Z2-7]{16}$")]
OptionalPositiveInt = Annotated[int | None, Field(ge=0)]
OptionalPositiveFloat = Annotated[float | None, Field(ge=0)]
VariableName = Annotated[str, Field(pattern="^[a-z](?:[a-z0-9_]*[a-z0-9])?$")]
Pattern = Annotated[str, AfterValidator(is_valid_hs_pattern)]
class Confidence(StrEnum):
LOW = enum.auto()
MEDIUM = enum.auto()
HIGH = enum.auto()
class Syntax(StrEnum):
HTML = enum.auto()
JSON = enum.auto()
XML = enum.auto()
class TargetKind(StrEnum):
# The default kind if unset
UNKNOWN = enum.auto()
# Add types as needed and ensure the name is the official name used in the platform
AWS_ACCESS_KEY_ID = enum.auto()
AWS_SECRET_ACCESS_KEY = enum.auto()
GITHUB_FINE_GRAINED_PERSONAL_ACCESS_TOKEN = enum.auto()
GITHUB_PERSONAL_ACCESS_TOKEN = enum.auto()
HOSTNAME = enum.auto()
PASSWORD = enum.auto()
USERNAME = enum.auto()
class Examples(BaseModel):
positive: list[str] | None = None
negative: list[str] | None = None
class Meta(BaseModel):
# Rate the quality of the item
confidence: Confidence | None = None
# Provide pos/neg examples for this item
examples: Examples | None = None
# Provide references for how it was created
references: list[HttpUrl] | None = None
# Set whether or not the result of this should be included in the report
report: bool = True
# Tags for additional context and categorization
tags: list[str] | None = None
class RuleMeta(Meta):
kind: TargetKind = TargetKind.UNKNOWN
name: str
description: str | None = None
class Target(BaseModel):
prefix_pattern: Pattern | None = None
pattern: Pattern
suffix_pattern: Pattern | None = None
class FilterKind(StrEnum):
REQUIRE = enum.auto()
EXCLUDE = enum.auto()
StatusRange = Annotated[list[int], BeforeValidator(ensure_valid_range)]
class HttpMatcher(BaseModel):
statuses: list[StatusRange] | None = None
headers: dict[str, list[str]] | None = None
body_strings: list[str] | None = None
body_patterns: list[Pattern] | None = None
body_syntax: Syntax | None = None
class BaseFilter(BaseModel):
"""
Filters options supported for all kinds
"""
# Target features
target_strings: list[str] | None = None
# Path features
path_patterns: list[Pattern] | None = None
path_strings: list[str] | None = None
# Context features (note: context may vary by target)
context_strings: list[str] | None = None
class ExcludeFilter(BaseFilter):
"""
Filters options supported only exclude
"""
kind: Literal[FilterKind.EXCLUDE]
# Target features
target_patterns: list[Pattern] | None = None
# Match features
match_patterns: list[Pattern] | None = None
match_strings: list[str] | None = None
# Context features (note: context may vary by target)
context_patterns: list[Pattern] | None = None
class RequireFilter(BaseFilter):
"""
Filters options supported only require
"""
kind: Literal[FilterKind.REQUIRE]
# Target features
target_min_entropy: OptionalPositiveFloat = None
Filter = Annotated[Union[ExcludeFilter, RequireFilter], Field(discriminator="kind")]
class AnalyzerKind(StrEnum):
HTTP = enum.auto()
class AnalyzerMeta(Meta):
kind: AnalyzerKind
report: bool = False
class AnalyzerHttpAction(BaseModel):
url: HttpUrl
method: str | None = None
headers: dict[str, str] | None = None
body: str | None = None
timeout: OptionalPositiveFloat = None
class Analyzer(BaseModel):
meta: AnalyzerMeta
action: AnalyzerHttpAction
# these are AND'd
condition: list[HttpMatcher]
class Dependancy(BaseModel):
rule_id: RuleId
varname: VariableName
within_lines: OptionalPositiveInt = None
within_columns: OptionalPositiveInt = None
class Rule(BaseModel):
id: RuleId
meta: RuleMeta
dependencies: list[Dependancy] | None = None
target: Target
filters: list[Filter] | None = None
analyzers: list[Analyzer] | None = None
class Schema(BaseModel):
rules: list[Rule]