-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmodels.py
77 lines (68 loc) · 2.35 KB
/
models.py
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
from typing import Any, Dict
from django.db import models
from framework.enums import InputKeyword
from framework.models import BaseInput
from rekono.settings import AUTH_USER_MODEL
from security.validators.input_validator import Regex, Validator
from targets.models import Target
# Create your models here.
class HttpHeader(BaseInput):
target = models.ForeignKey(
Target,
related_name="http_headers",
on_delete=models.CASCADE,
blank=True,
null=True,
)
user = models.ForeignKey(
AUTH_USER_MODEL,
related_name="http_headers",
on_delete=models.CASCADE,
blank=True,
null=True,
)
key = models.TextField(
max_length=100,
validators=[Validator(Regex.NAME.value, code="key", deny_injections=True)],
)
value = models.TextField(
max_length=500,
validators=[Validator(Regex.TEXT.value, code="value", deny_injections=True)],
)
filters = [BaseInput.Filter(type=str, field="key")]
class Meta:
constraints = [
models.UniqueConstraint(
fields=["target", "user", "key"],
name="unique_http_headers_1",
condition=models.Q(user__isnull=False, target__isnull=False),
),
models.UniqueConstraint(
fields=["user", "key"],
name="unique_http_headers_2",
condition=models.Q(target__isnull=True),
),
models.UniqueConstraint(
fields=["target", "key"],
name="unique_http_headers_3",
condition=models.Q(user__isnull=True),
),
models.UniqueConstraint(
fields=["key"],
name="unique_http_headers_4",
condition=models.Q(user__isnull=True, target__isnull=True),
),
]
def parse(self, accumulated: Dict[str, Any] = {}) -> Dict[str, Any]:
return {
InputKeyword.HEADERS.name.lower(): {
**accumulated.get(InputKeyword.HEADERS.name.lower(), {}),
self.key: self.value,
}
}
def __str__(self) -> str:
parent = self.target or self.user
return f"{parent.__str__()} - {self.key}" if parent else self.key
@classmethod
def get_project_field(cls) -> str:
return "target__project"