Skip to content

Commit f3f4692

Browse files
authored
fix(subscriptions): handle regex syntax error (#222)
1 parent cc24b4a commit f3f4692

File tree

3 files changed

+225
-185
lines changed

3 files changed

+225
-185
lines changed

app/Entirety/subscriptions/utils.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import re
22
import itertools
3-
43
from filip.clients.ngsi_v2.cb import ContextBrokerClient
54
from filip.models import FiwareHeader
6-
75
from django.conf import settings
86

97

8+
def safe_compile(pattern):
9+
"""Attempt to compile a regex pattern safely."""
10+
try:
11+
return re.compile(pattern)
12+
except re.error as e:
13+
# Log or handle the error as needed
14+
raise re.error(f"Regular expression {pattern} invalid: {e}")
15+
16+
1017
def load_attributes(project, data_set):
1118
attributes = []
1219
with ContextBrokerClient(
@@ -22,7 +29,7 @@ def load_attributes(project, data_set):
2229
entity_type = data["entity_type"]
2330
if entity_type:
2431
if type_selector == "type_pattern":
25-
pattern = re.compile(entity_type)
32+
pattern = safe_compile(entity_type)
2633
tmp_attrs = itertools.chain.from_iterable(
2734
[
2835
list(t["attrs"].keys())
@@ -45,9 +52,9 @@ def load_attributes(project, data_set):
4552
if entity_id:
4653
entities = cb_client.get_entity_list(
4754
entity_ids=entity_id if entity_selector == "id" else None,
48-
id_pattern=entity_id
49-
if entity_selector == "id_pattern"
50-
else None,
55+
id_pattern=(
56+
entity_id if entity_selector == "id_pattern" else None
57+
),
5158
)
5259
for entity in entities:
5360
attributes.extend(cb_client.get_entity_attributes(entity.id))

app/Entirety/subscriptions/views/create.py

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,42 +89,62 @@ def post(self, request, *args, **kwargs):
8989
if form.is_valid() and entities_set.is_valid():
9090
data_set = [entity_form.cleaned_data for entity_form in entities_set]
9191
# Otherwise choices are empty
92-
attributes.fields["attributes"].choices = utils.load_attributes(
93-
self.project, data_set
94-
)
95-
if attributes.is_valid():
96-
instance = form.save(commit=False)
92+
try:
93+
attributes.fields["attributes"].choices = utils.load_attributes(
94+
self.project, data_set
95+
)
96+
if attributes.is_valid():
97+
instance = form.save(commit=False)
9798

98-
entities = []
99+
entities = []
99100

100-
for entity_form in entities_set:
101-
if entity_form.cleaned_data:
102-
entity_selector = entity_form.cleaned_data["entity_selector"]
103-
type_selector = entity_form.cleaned_data["type_selector"]
104-
pattern = EntityPattern(
105-
id=(
106-
entity_form.cleaned_data["entity_id"]
107-
if entity_selector == "id"
108-
else None
109-
),
110-
idPattern=(
111-
re.compile(entity_form.cleaned_data["entity_id"])
112-
if entity_selector == "id_pattern"
113-
else None
114-
),
115-
type=(
116-
entity_form.cleaned_data["entity_type"]
117-
if entity_form.cleaned_data["entity_type"]
118-
and type_selector == "type"
119-
else None
120-
),
121-
typePattern=(
122-
re.compile(entity_form.cleaned_data["entity_type"])
123-
if type_selector == "type_pattern"
124-
else None
125-
),
126-
)
127-
entities.append(pattern)
101+
for entity_form in entities_set:
102+
if entity_form.cleaned_data:
103+
entity_selector = entity_form.cleaned_data[
104+
"entity_selector"
105+
]
106+
type_selector = entity_form.cleaned_data["type_selector"]
107+
pattern = EntityPattern(
108+
id=(
109+
entity_form.cleaned_data["entity_id"]
110+
if entity_selector == "id"
111+
else None
112+
),
113+
idPattern=(
114+
utils.safe_compile(
115+
entity_form.cleaned_data["entity_id"]
116+
)
117+
if entity_selector == "id_pattern"
118+
else None
119+
),
120+
type=(
121+
entity_form.cleaned_data["entity_type"]
122+
if entity_form.cleaned_data["entity_type"]
123+
and type_selector == "type"
124+
else None
125+
),
126+
typePattern=(
127+
utils.safe_compile(
128+
entity_form.cleaned_data["entity_type"]
129+
)
130+
if type_selector == "type_pattern"
131+
else None
132+
),
133+
)
134+
entities.append(pattern)
135+
except re.error as e:
136+
logger.error(
137+
str(
138+
self.request.user.first_name
139+
if self.request.user.first_name
140+
else self.request.user.username
141+
)
142+
+ " tried creating the subscription "
143+
+ " but failed "
144+
f" in project {self.project.name}"
145+
)
146+
messages.error(request, e)
147+
return self.form_invalid(form)
128148
with ContextBrokerClient(
129149
url=settings.CB_URL,
130150
fiware_header=FiwareHeader(

0 commit comments

Comments
 (0)