Skip to content

Commit 0dce597

Browse files
authored
Solve bandit security problems in examples. (#852)
1 parent 762903d commit 0dce597

19 files changed

+94
-100
lines changed

examples/common/async_asyncio_client.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,48 +67,48 @@ async def start_async_test(client):
6767
_logger.debug("Write to a Coil and read back")
6868
rq = await client.write_coil(0, True, unit=UNIT)
6969
rr = await client.read_coils(0, 1, unit=UNIT)
70-
assert rq.function_code < 0x80 # test that we are not an error
71-
assert rr.bits[0] # test the expected value
70+
assert rq.function_code < 0x80 #nosec test that we are not an error
71+
assert rr.bits[0] #nosec test the expected value
7272

7373
_logger.debug("Write to multiple coils and read back- test 1")
7474
rq = await client.write_coils(1, [True] * 8, unit=UNIT)
75-
assert rq.function_code < 0x80 # test that we are not an error
75+
assert rq.function_code < 0x80 #nosec test that we are not an error
7676
rr = await client.read_coils(1, 21, unit=UNIT)
77-
assert rr.function_code < 0x80 # test that we are not an error
77+
assert rr.function_code < 0x80 #nosec test that we are not an error
7878
resp = [True] * 21
7979

8080
# If the returned output quantity is not a multiple of eight,
8181
# the remaining bits in the final data byte will be padded with zeros
8282
# (toward the high order end of the byte).
8383

8484
resp.extend([False] * 3)
85-
assert rr.bits == resp # test the expected value
85+
assert rr.bits == resp #nosec test the expected value
8686

8787
_logger.debug("Write to multiple coils and read back - test 2")
8888
rq = await client.write_coils(1, [False] * 8, unit=UNIT)
8989
rr = await client.read_coils(1, 8, unit=UNIT)
90-
assert rq.function_code < 0x80 # test that we are not an error
91-
assert rr.bits == [False] * 8 # test the expected value
90+
assert rq.function_code < 0x80 #nosec test that we are not an error
91+
assert rr.bits == [False] * 8 #nosec test the expected value
9292

9393
_logger.debug("Read discrete inputs")
9494
rr = await client.read_discrete_inputs(0, 8, unit=UNIT)
95-
assert rq.function_code < 0x80 # test that we are not an error
95+
assert rq.function_code < 0x80 #nosec test that we are not an error
9696

9797
_logger.debug("Write to a holding register and read back")
9898
rq = await client.write_register(1, 10, unit=UNIT)
9999
rr = await client.read_holding_registers(1, 1, unit=UNIT)
100-
assert rq.function_code < 0x80 # test that we are not an error
101-
assert rr.registers[0] == 10 # test the expected value
100+
assert rq.function_code < 0x80 #nosec test that we are not an error
101+
assert rr.registers[0] == 10 #nosec test the expected value
102102

103103
_logger.debug("Write to multiple holding registers and read back")
104104
rq = await client.write_registers(1, [10] * 8, unit=UNIT)
105105
rr = await client.read_holding_registers(1, 8, unit=UNIT)
106-
assert rq.function_code < 0x80 # test that we are not an error
107-
assert rr.registers == [10] * 8 # test the expected value
106+
assert rq.function_code < 0x80 #nosec test that we are not an error
107+
assert rr.registers == [10] * 8 #nosec test the expected value
108108

109109
_logger.debug("Read input registers")
110110
rr = await client.read_input_registers(1, 8, unit=UNIT)
111-
assert rq.function_code < 0x80 # test that we are not an error
111+
assert rq.function_code < 0x80 #nosec test that we are not an error
112112

113113
arguments = {
114114
'read_address': 1,
@@ -119,9 +119,9 @@ async def start_async_test(client):
119119
_logger.debug("Read write registers simultaneously")
120120
rq = await client.readwrite_registers(unit=UNIT, **arguments)
121121
rr = await client.read_holding_registers(1, 8, unit=UNIT)
122-
assert rq.function_code < 0x80 # test that we are not an error
123-
assert rq.registers == [20] * 8 # test the expected value
124-
assert rr.registers == [20] * 8 # test the expected value
122+
assert rq.function_code < 0x80 #nosec test that we are not an error
123+
assert rq.registers == [20] * 8 #nosec test the expected value
124+
assert rr.registers == [20] * 8 #nosec test the expected value
125125
await asyncio.sleep(1)
126126

127127

@@ -130,7 +130,7 @@ def run_with_not_running_loop():
130130
_logger.debug("Running Async client with asyncio loop not yet started")
131131
_logger.debug("------------------------------------------------------")
132132
loop = asyncio.new_event_loop()
133-
assert not loop.is_running()
133+
assert not loop.is_running() #nosec
134134
asyncio.set_event_loop(loop)
135135
new_loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020, loop=loop) #NOSONAR pylint: disable=unpacking-non-sequence
136136
loop.run_until_complete(start_async_test(client.protocol))
@@ -162,7 +162,7 @@ def start_loop(loop):
162162
# Start the loop
163163
mythread.start()
164164
asyncio.sleep(1)
165-
assert loop.is_running()
165+
assert loop.is_running() #nosec
166166
asyncio.set_event_loop(loop)
167167
loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020, loop=loop) #NOSONAR pylint: disable=unpacking-non-sequence
168168
future = asyncio.run_coroutine_threadsafe(

examples/common/async_asyncio_serial_client.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,49 @@ async def start_async_test(client): # pylint: disable=redefined-outer-name
5959
rq = await client.write_coil(0, True, unit=UNIT)
6060
rr = await client.read_coils(0, 1, unit=UNIT)
6161

62-
assert rq.function_code < 0x80 # test that we are not an error
63-
assert rr.bits[0] # test the expected value
62+
assert rq.function_code < 0x80 #nosec test that we are not an error
63+
assert rr.bits[0] #nosec test the expected value
6464

6565
log.debug("Write to multiple coils and read back- test 1")
6666
rq = await client.write_coils(1, [True] * 8, unit=UNIT)
6767
rr = await client.read_coils(1, 21, unit=UNIT)
6868

69-
assert rq.function_code < 0x80 # test that we are not an error
70-
assert rr.function_code < 0x80 # test that we are not an error
69+
assert rq.function_code < 0x80 #nosec test that we are not an error
70+
assert rr.function_code < 0x80 #nosec test that we are not an error
7171

7272
# If the returned output quantity is not a multiple of eight,
7373
# the remaining bits in the final data byte will be padded with zeros
7474
# (toward the high order end of the byte).
7575

7676
resp = [True] * 21
7777
resp.extend([False] * 3)
78-
assert rr.bits == resp # test the expected value
78+
assert rr.bits == resp #nosec test the expected value
7979

8080
log.debug("Write to multiple coils and read back - test 2")
8181
rq = await client.write_coils(1, [False] * 8, unit=UNIT)
8282
rr = await client.read_coils(1, 8, unit=UNIT)
83-
assert rq.function_code < 0x80 # test that we are not an error
84-
assert rr.bits == [False] * 8 # test the expected value
83+
assert rq.function_code < 0x80 #nosec test that we are not an error
84+
assert rr.bits == [False] * 8 #nosec test the expected value
8585

8686
log.debug("Read discrete inputs")
8787
rr = await client.read_discrete_inputs(0, 8, unit=UNIT)
88-
assert rq.function_code < 0x80 # test that we are not an error
88+
assert rq.function_code < 0x80 #nosec test that we are not an error
8989

9090
log.debug("Write to a holding register and read back")
9191
rq = await client.write_register(1, 10, unit=UNIT)
9292
rr = await client.read_holding_registers(1, 1, unit=UNIT)
93-
assert rq.function_code < 0x80 # test that we are not an error
94-
assert rr.registers[0] == 10 # test the expected value
93+
assert rq.function_code < 0x80 #nosec test that we are not an error
94+
assert rr.registers[0] == 10 #nosec test the expected value
9595

9696
log.debug("Write to multiple holding registers and read back")
9797
rq = await client.write_registers(1, [10] * 8, unit=UNIT)
9898
rr = await client.read_holding_registers(1, 8, unit=UNIT)
99-
assert rq.function_code < 0x80 # test that we are not an error
100-
assert rr.registers == [10] * 8 # test the expected value
99+
assert rq.function_code < 0x80 #nosec test that we are not an error
100+
assert rr.registers == [10] * 8 #nosec test the expected value
101101

102102
log.debug("Read input registers")
103103
rr = await client.read_input_registers(1, 8, unit=UNIT)
104-
assert rq.function_code < 0x80 # test that we are not an error
104+
assert rq.function_code < 0x80 #nosec test that we are not an error
105105

106106
arguments = {
107107
'read_address': 1,
@@ -112,9 +112,9 @@ async def start_async_test(client): # pylint: disable=redefined-outer-name
112112
log.debug("Read write registers simultaneously")
113113
rq = await client.readwrite_registers(unit=UNIT, **arguments)
114114
rr = await client.read_holding_registers(1, 8, unit=UNIT)
115-
assert rq.function_code < 0x80 # test that we are not an error
116-
assert rq.registers == [20] * 8 # test the expected value
117-
assert rr.registers == [20] * 8 # test the expected value
115+
assert rq.function_code < 0x80 #nosec test that we are not an error
116+
assert rq.registers == [20] * 8 #nosec test the expected value
117+
assert rr.registers == [20] * 8 #nosec test the expected value
118118
except Exception as exc: # pylint: disable=broad-except
119119
log.exception(exc)
120120
client.transport.close()
@@ -128,7 +128,7 @@ async def start_async_test(client): # pylint: disable=redefined-outer-name
128128
# ----------------------------------------------------------------------- #
129129
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
130130
# link=/tmp/ttyp0,raw,echo=0,ospeed=9600
131-
loop, client = ModbusClient(schedulers.ASYNC_IO, port='/tmp/ttyp0', # pylint: disable=unpacking-non-sequence
131+
loop, client = ModbusClient(schedulers.ASYNC_IO, port='/tmp/ttyp0', #nosec pylint: disable=unpacking-non-sequence
132132
baudrate=9600, method="rtu")
133133
loop.run_until_complete(start_async_test(client.protocol))
134134
loop.close()

examples/common/async_tornado_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def dassert(future, callback): # pylint: disable=redefined-outer-name
3434

3535
def _assertor(value):
3636
# by pass assertion, an error here stops the write callbacks
37-
assert value
37+
assert value #nosec
3838

3939
def on_done(f):
4040
if (exc := f.exception()):

examples/common/async_tornado_client_serial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def dassert(future, callback): # pylint: disable=redefined-outer-name
4141

4242
def _assertor(value):
4343
# by pass assertion, an error here stops the write callbacks
44-
assert value
44+
assert value #nosec
4545

4646
def on_done(f):
4747
if (exc := f.exception()):
@@ -161,7 +161,7 @@ def callback(protocol, future): # pylint: disable=redefined-outer-name
161161
# Rtu
162162
protocol, future = AsyncModbusSerialClient(schedulers.IO_LOOP, # pylint: disable=unpacking-non-sequence
163163
method="rtu",
164-
port="/tmp/ptyp0",
164+
port="/tmp/ptyp0", #nosec
165165
baudrate=9600,
166166
timeout=2)
167167

examples/common/async_twisted_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def err(*args, **kwargs):
3939
def dassert(deferred, callback): # pylint: disable=redefined-outer-name
4040
""" Dassert. """
4141
def _assertor(value):
42-
assert value
42+
assert value #nosec
4343
deferred.addCallback(lambda r: _assertor(callback(r)))
4444
deferred.addErrback(err)
4545

examples/common/async_twisted_client_serial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# state a few constants
2020
# ---------------------------------------------------------------------------#
2121

22-
SERIAL_PORT = "/tmp/ptyp0"
22+
SERIAL_PORT = "/tmp/ptyp0" #nosec
2323
STATUS_REGS = (1, 2)
2424
STATUS_COILS = (1, 3)
2525
CLIENT_DELAY = 1

examples/common/asyncio_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async def run_server():
116116
# defer_start=False)
117117

118118
# deferred start:
119-
server = await StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020),
119+
server = await StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020), #nosec
120120
allow_reuse_address=True, defer_start=True)
121121

122122
asyncio.get_event_loop().call_later(20, lambda: server.serve_forever)

examples/common/changing_framers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
# ----------------------------------------------------------------------- #
4343
rq = client.write_coil(1, True)
4444
rr = client.read_coils(1, 1)
45-
assert not rq.isError() # test that we are not an error
46-
assert rr.bits[0] # test the expected value
45+
assert not rq.isError() #nosec test that we are not an error
46+
assert rr.bits[0] #nosec test the expected value
4747

4848
# ----------------------------------------------------------------------- #
4949
# close the client

examples/common/dbstore_update_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def updating_writer(parm1):
5656

5757
# import pdb; pdb.set_trace()
5858

59-
rand_value = random.randint(0, 9999) #NOSONAR
60-
rand_addr = random.randint(0, 65000) #NOSONAR
59+
rand_value = random.randint(0, 9999) #NOSONAR #nosec
60+
rand_addr = random.randint(0, 65000) #NOSONAR #nosec
6161
txt = f"Writing to datastore: {rand_addr}, {rand_value}"
6262
log.debug(txt)
6363
# import pdb; pdb.set_trace()

examples/common/modbus_payload.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ def run_binary_payload_ex():
163163
byteorder=byte_endian,
164164
wordorder=word_endian)
165165

166-
assert decoder._byteorder == builder._byteorder, \
167-
"Make sure byteorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder" # pylint: disable=protected-access
166+
assert decoder._byteorder == (builder._byteorder, #nosec pylint: disable=protected-access
167+
"Make sure byteorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder")
168168

169-
assert decoder._wordorder == builder._wordorder, \
170-
"Make sure wordorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder" # pylint: disable=protected-access
169+
assert decoder._wordorder == (builder._wordorder, #nosec pylint: disable=protected-access
170+
"Make sure wordorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder")
171171

172172
decoded = OrderedDict([
173173
('string', decoder.decode_string(len(my_string))),

0 commit comments

Comments
 (0)