rspec/rules/S2317/python/rule.adoc

51 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-08-08 11:11:03 +02:00
This rule raises an issue when the exec statement is used.
== Why is this an issue?
2021-04-28 16:49:39 +02:00
Use of the ``++exec++`` statement could be dangerous, and should be avoided. Moreover, the ``++exec++`` statement was removed in Python 3.0. Instead, the built-in ``++exec()++`` function can be used.
2023-08-08 11:11:03 +02:00
Use of the ``++exec++`` statement is strongly discouraged for several reasons such as:
* *Security Risks:* Executing code from a string opens up the possibility of code injection attacks.
* *Readability and Maintainability:* Code executed with ``++exec++`` statement is often harder to read and understand since it is not explicitly written in the source code.
* *Performance Implications:* The use of ``++exec++`` statement can have performance implications since the code is compiled and executed at runtime.
* *Limited Static Analysis:* Since the code executed with ``++exec++`` statement is only known at runtime, static code analysis tools may not be able to catch certain errors or issues, leading to potential bugs.
=== Code examples
2023-08-08 11:11:03 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-08-08 11:11:03 +02:00
[source,python,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
exec 'print 1' # Noncompliant
----
2023-08-08 11:11:03 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-08-08 11:11:03 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
exec('print 1')
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use the "exec()" function instead.
'''
== Comments And Links
(visible only on this page)
=== on 25 Feb 2019, 17:14:43 Tibor Blenessy wrote:
Changing this to code smell, as this rule is mostly about migration towards Python 3. The security aspect of this rule is covered in hotspot RSPEC-1523
endif::env-github,rspecator-view[]