Skip to content

Commit 905e85a

Browse files
committed
Fix: Check port in overrides/notes like in [gvmd](greenbone/gvmd@56ab13b)
1 parent cd4e0b4 commit 905e85a

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

gvm/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# pylint: disable=arguments-differ, redefined-builtin
2020

2121
import base64
22+
import re
2223
import warnings
2324
import logging
2425

@@ -103,10 +104,9 @@ def is_list_like(value: Any) -> bool:
103104

104105

105106
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
107+
pattern = re.compile(
108+
r"^(cpe:[^\s]+|(general|[1-9][0-9]{0,4})/[0-9A-Za-z]+)$"
109+
)
110+
if pattern.fullmatch(value):
111+
return True
112+
return False

tests/utils/test_check_port.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (C) 2022 Greenbone Networks GmbH
3+
#
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import unittest
20+
21+
from gvm.utils import check_port
22+
23+
24+
class TestCheckPort(unittest.TestCase):
25+
def test_port_cpe(self):
26+
self.assertTrue(check_port("cpe:meh:moo*"))
27+
28+
def test_port_cpe_fail(self):
29+
self.assertFalse(check_port("cpe:meh haha"))
30+
31+
def test_port_general(self):
32+
self.assertTrue(check_port("general/tcp"))
33+
34+
def test_ports(self):
35+
self.assertTrue(check_port("1/tcp"))
36+
self.assertTrue(check_port("12/tcp"))
37+
self.assertTrue(check_port("123/tcp"))
38+
self.assertTrue(check_port("1234/tcp"))
39+
self.assertTrue(check_port("12345/tcp"))
40+
self.assertTrue(check_port("1/udp"))
41+
self.assertTrue(check_port("12/udp"))
42+
43+
def test_ports_fail(self):
44+
self.assertFalse(check_port("1"))
45+
self.assertFalse(check_port("1//udp"))
46+
self.assertFalse(check_port("1/12/tcp"))
47+
self.assertFalse(check_port("tcp"))
48+
self.assertFalse(check_port("general"))
49+
self.assertFalse(check_port("lol"))

0 commit comments

Comments
 (0)