Skip to content

Commit 8c9057b

Browse files
authored
Fix: ports argument for overrides and notes (#755)
* Fix: Behaviour of create/modify_override/note to accept only valid port strings * Adjust the tests
1 parent 73e0400 commit 8c9057b

File tree

17 files changed

+209
-180
lines changed

17 files changed

+209
-180
lines changed

gvm/protocols/gmpv208/entities/notes.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
from typing import Any, List, Optional
2121

22-
from gvm.errors import RequiredArgument, InvalidArgumentType
22+
from gvm.errors import InvalidArgument, RequiredArgument, InvalidArgumentType
2323
from gvm.protocols.gmpv208.entities.severity import Severity, SeverityLevel
24-
from gvm.utils import add_filter, to_comma_list, to_bool
24+
from gvm.utils import add_filter, check_port, to_comma_list, to_bool
2525
from gvm.xml import XmlCommand
2626

2727

@@ -33,7 +33,7 @@ def create_note(
3333
*,
3434
days_active: Optional[int] = None,
3535
hosts: Optional[List[str]] = None,
36-
port: Optional[int] = None,
36+
port: Optional[str] = None,
3737
result_id: Optional[str] = None,
3838
severity: Optional[Severity] = None,
3939
task_id: Optional[str] = None,
@@ -47,7 +47,8 @@ def create_note(
4747
days_active: Days note will be active. -1 on
4848
always, 0 off
4949
hosts: A list of hosts addresses
50-
port: Port to which the note applies
50+
port: Port to which the override applies, needs to be a string
51+
in the form {number}/{protocol}
5152
result_id: UUID of a result to which note applies
5253
severity: Severity to which note applies
5354
task_id: UUID of task to which note applies
@@ -78,7 +79,12 @@ def create_note(
7879
cmd.add_element("hosts", to_comma_list(hosts))
7980

8081
if port:
81-
cmd.add_element("port", str(port))
82+
if check_port(port):
83+
cmd.add_element("port", str(port))
84+
else:
85+
raise InvalidArgument(
86+
function=self.create_note.__name__, argument='port'
87+
)
8288

8389
if result_id:
8490
cmd.add_element("result", attrs={"id": result_id})
@@ -198,7 +204,7 @@ def modify_note(
198204
*,
199205
days_active: Optional[int] = None,
200206
hosts: Optional[List[str]] = None,
201-
port: Optional[int] = None,
207+
port: Optional[str] = None,
202208
result_id: Optional[str] = None,
203209
severity: Optional[Severity] = None,
204210
task_id: Optional[str] = None,
@@ -211,7 +217,8 @@ def modify_note(
211217
text: The text of the note.
212218
days_active: Days note will be active. -1 on always, 0 off.
213219
hosts: A list of hosts addresses
214-
port: Port to which note applies.
220+
port: Port to which the override applies, needs to be a string
221+
in the form {number}/{protocol}
215222
result_id: Result to which note applies.
216223
severity: Severity to which note applies.
217224
task_id: Task to which note applies.
@@ -242,7 +249,12 @@ def modify_note(
242249
cmd.add_element("hosts", to_comma_list(hosts))
243250

244251
if port:
245-
cmd.add_element("port", str(port))
252+
if check_port(port):
253+
cmd.add_element("port", str(port))
254+
else:
255+
raise InvalidArgument(
256+
function=self.modify_note.__name__, argument='port'
257+
)
246258

247259
if result_id:
248260
cmd.add_element("result", attrs={"id": result_id})

gvm/protocols/gmpv208/entities/overrides.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
from typing import Any, List, Optional
2020

21-
from gvm.errors import RequiredArgument, InvalidArgumentType
21+
from gvm.errors import InvalidArgument, RequiredArgument, InvalidArgumentType
2222
from gvm.protocols.gmpv208.entities.severity import Severity, SeverityLevel
23-
from gvm.utils import add_filter, to_comma_list, to_bool
23+
from gvm.utils import add_filter, check_port, to_comma_list, to_bool
2424
from gvm.xml import XmlCommand
2525

2626

@@ -32,7 +32,7 @@ def create_override(
3232
*,
3333
days_active: Optional[int] = None,
3434
hosts: Optional[List[str]] = None,
35-
port: Optional[int] = None,
35+
port: Optional[str] = None,
3636
result_id: Optional[str] = None,
3737
severity: Optional[Severity] = None,
3838
new_severity: Optional[Severity] = None,
@@ -47,7 +47,8 @@ def create_override(
4747
nvt_id: OID of the nvt to which override applies
4848
days_active: Days override will be active. -1 on always, 0 off
4949
hosts: A list of host addresses
50-
port: Port to which the override applies
50+
port: Port to which the override applies, needs to be a string
51+
in the form {number}/{protocol}
5152
result_id: UUID of a result to which override applies
5253
severity: Severity to which override applies
5354
new_severity: New severity for result
@@ -81,7 +82,12 @@ def create_override(
8182
cmd.add_element("hosts", to_comma_list(hosts))
8283

8384
if port:
84-
cmd.add_element("port", str(port))
85+
if check_port(port):
86+
cmd.add_element("port", str(port))
87+
else:
88+
raise InvalidArgument(
89+
function=self.create_override.__name__, argument='port'
90+
)
8591

8692
if result_id:
8793
cmd.add_element("result", attrs={"id": result_id})
@@ -215,7 +221,7 @@ def modify_override(
215221
*,
216222
days_active: Optional[int] = None,
217223
hosts: Optional[List[str]] = None,
218-
port: Optional[int] = None,
224+
port: Optional[str] = None,
219225
result_id: Optional[str] = None,
220226
severity: Optional[Severity] = None,
221227
new_severity: Optional[Severity] = None,
@@ -231,7 +237,8 @@ def modify_override(
231237
days_active: Days override will be active. -1 on always,
232238
0 off.
233239
hosts: A list of host addresses
234-
port: Port to which override applies.
240+
port: Port to which the override applies, needs to be a string
241+
in the form {number}/{protocol}
235242
result_id: Result to which override applies.
236243
severity: Severity to which override applies.
237244
new_severity: New severity score for result.
@@ -264,7 +271,12 @@ def modify_override(
264271
cmd.add_element("hosts", to_comma_list(hosts))
265272

266273
if port:
267-
cmd.add_element("port", str(port))
274+
if check_port(port):
275+
cmd.add_element("port", str(port))
276+
else:
277+
raise InvalidArgument(
278+
function=self.modify_override.__name__, argument='port'
279+
)
268280

269281
if result_id:
270282
cmd.add_element("result", attrs={"id": result_id})

gvm/protocols/gmpv214/entities/notes.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
from typing import Any, List, Optional
2121

22-
from gvm.errors import RequiredArgument
22+
from gvm.errors import InvalidArgument, RequiredArgument
2323
from gvm.protocols.gmpv208.entities.notes import NotesMixin as Gmp208NotesMixin
2424
from gvm.protocols.gmpv208.entities.severity import Severity
25-
from gvm.utils import deprecation, to_comma_list
25+
from gvm.utils import check_port, deprecation, to_comma_list
2626
from gvm.xml import XmlCommand
2727

2828

@@ -48,7 +48,8 @@ def create_note(
4848
days_active: Days note will be active. -1 on
4949
always, 0 off
5050
hosts: A list of hosts addresses
51-
port: Port to which the note applies
51+
port: Port to which the override applies, needs to be a string
52+
in the form {number}/{protocol}
5253
result_id: UUID of a result to which note applies
5354
severity: Severity to which note applies
5455
task_id: UUID of task to which note applies
@@ -78,7 +79,12 @@ def create_note(
7879
cmd.add_element("hosts", to_comma_list(hosts))
7980

8081
if port:
81-
cmd.add_element("port", str(port))
82+
if check_port(port):
83+
cmd.add_element("port", str(port))
84+
else:
85+
raise InvalidArgument(
86+
function=self.create_note.__name__, argument='port'
87+
)
8288

8389
if result_id:
8490
cmd.add_element("result", attrs={"id": result_id})
@@ -118,7 +124,8 @@ def modify_note(
118124
text: The text of the note.
119125
days_active: Days note will be active. -1 on always, 0 off.
120126
hosts: A list of hosts addresses
121-
port: Port to which note applies.
127+
port: Port to which the override applies, needs to be a string
128+
in the form {number}/{protocol}
122129
result_id: Result to which note applies.
123130
severity: Severity to which note applies.
124131
task_id: Task to which note applies.
@@ -148,7 +155,12 @@ def modify_note(
148155
cmd.add_element("hosts", to_comma_list(hosts))
149156

150157
if port:
151-
cmd.add_element("port", str(port))
158+
if check_port(port):
159+
cmd.add_element("port", str(port))
160+
else:
161+
raise InvalidArgument(
162+
function=self.modify_note.__name__, argument='port'
163+
)
152164

153165
if result_id:
154166
cmd.add_element("result", attrs={"id": result_id})

gvm/protocols/gmpv214/entities/overrides.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
from typing import Any, List, Optional
2121

22-
from gvm.errors import RequiredArgument
22+
from gvm.errors import InvalidArgument, RequiredArgument
2323
from gvm.protocols.gmpv208.entities.overrides import (
2424
OverridesMixin as Gmp208OverridesMixin,
2525
)
2626
from gvm.protocols.gmpv208.entities.severity import Severity
27-
from gvm.utils import deprecation, to_comma_list
27+
from gvm.utils import check_port, deprecation, to_comma_list
2828
from gvm.xml import XmlCommand
2929

3030

@@ -36,7 +36,7 @@ def create_override(
3636
*,
3737
days_active: Optional[int] = None,
3838
hosts: Optional[List[str]] = None,
39-
port: Optional[int] = None,
39+
port: Optional[str] = None,
4040
result_id: Optional[str] = None,
4141
severity: Optional[Severity] = None,
4242
new_severity: Optional[Severity] = None,
@@ -51,7 +51,8 @@ def create_override(
5151
nvt_id: OID of the nvt to which override applies
5252
days_active: Days override will be active. -1 on always, 0 off
5353
hosts: A list of host addresses
54-
port: Port to which the override applies
54+
port: Port to which the override applies, needs to be a string
55+
in the form {number}/{protocol}
5556
result_id: UUID of a result to which override applies
5657
severity: Severity to which override applies
5758
new_severity: New severity for result
@@ -83,7 +84,12 @@ def create_override(
8384
cmd.add_element("hosts", to_comma_list(hosts))
8485

8586
if port:
86-
cmd.add_element("port", str(port))
87+
if check_port(port):
88+
cmd.add_element("port", str(port))
89+
else:
90+
raise InvalidArgument(
91+
function=self.create_override.__name__, argument='port'
92+
)
8793

8894
if result_id:
8995
cmd.add_element("result", attrs={"id": result_id})
@@ -120,7 +126,7 @@ def modify_override(
120126
*,
121127
days_active: Optional[int] = None,
122128
hosts: Optional[List[str]] = None,
123-
port: Optional[int] = None,
129+
port: Optional[str] = None,
124130
result_id: Optional[str] = None,
125131
severity: Optional[Severity] = None,
126132
new_severity: Optional[Severity] = None,
@@ -136,7 +142,8 @@ def modify_override(
136142
days_active: Days override will be active. -1 on always,
137143
0 off.
138144
hosts: A list of host addresses
139-
port: Port to which override applies.
145+
port: Port to which the override applies, needs to be a string
146+
in the form {number}/{protocol}
140147
result_id: Result to which override applies.
141148
severity: Severity to which override applies.
142149
new_severity: New severity score for result.
@@ -167,7 +174,12 @@ def modify_override(
167174
cmd.add_element("hosts", to_comma_list(hosts))
168175

169176
if port:
170-
cmd.add_element("port", str(port))
177+
if check_port(port):
178+
cmd.add_element("port", str(port))
179+
else:
180+
raise InvalidArgument(
181+
function=self.modify_override.__name__, argument='port'
182+
)
171183

172184
if result_id:
173185
cmd.add_element("result", attrs={"id": result_id})

gvm/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,13 @@ def add_filter(cmd, filter_string, filter_id):
100100

101101
def is_list_like(value: Any) -> bool:
102102
return isinstance(value, (list, tuple))
103+
104+
105+
def check_port(value: str) -> bool:
106+
try:
107+
port, type = value.split('/', maxsplit=1)
108+
if port.isdigit() and type:
109+
return True
110+
return False
111+
except ValueError:
112+
return False

tests/protocols/gmpv208/entities/notes/test_create_note.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from decimal import Decimal
2020

21-
from gvm.errors import RequiredArgument, InvalidArgumentType
21+
from gvm.errors import InvalidArgument, RequiredArgument, InvalidArgumentType
2222

2323
from gvm.protocols.gmpv208 import SeverityLevel
2424

@@ -69,23 +69,13 @@ def test_create_note_with_hosts(self):
6969
)
7070

7171
def test_create_note_with_port(self):
72-
self.gmp.create_note('foo', nvt_oid='oid1', port='666')
72+
self.gmp.create_note('foo', nvt_oid='oid1', port='666/tcp')
7373

7474
self.connection.send.has_been_called_with(
7575
'<create_note>'
7676
'<text>foo</text>'
7777
'<nvt oid="oid1"/>'
78-
'<port>666</port>'
79-
'</create_note>'
80-
)
81-
82-
self.gmp.create_note('foo', nvt_oid='oid1', port=666)
83-
84-
self.connection.send.has_been_called_with(
85-
'<create_note>'
86-
'<text>foo</text>'
87-
'<nvt oid="oid1"/>'
88-
'<port>666</port>'
78+
'<port>666/tcp</port>'
8979
'</create_note>'
9080
)
9181

@@ -190,3 +180,10 @@ def test_create_note_with_days_active(self):
190180
'<active>3600</active>'
191181
'</create_note>'
192182
)
183+
184+
def test_create_note_with_invalid_port(self):
185+
with self.assertRaises(InvalidArgument):
186+
self.gmp.create_note(text='foo', nvt_oid='oid1', port='123')
187+
188+
with self.assertRaises(InvalidArgument):
189+
self.gmp.create_note(text='foo', nvt_oid='oid1', port='tcp/123')

0 commit comments

Comments
 (0)