Skip to content

New code examples for pyscg-0036 as part of issue #1059#1078

Open
s19110 wants to merge 1 commit intoossf:mainfrom
s19110:pyscg_issue_1059
Open

New code examples for pyscg-0036 as part of issue #1059#1078
s19110 wants to merge 1 commit intoossf:mainfrom
s19110:pyscg_issue_1059

Conversation

@s19110
Copy link
Copy Markdown
Contributor

@s19110 s19110 commented Mar 27, 2026

This change aims to update the code examples from pyscg-0036 to be better alligned with CWE-252.

The second set of code examples was changed to edit the string using the index instead of just printing it. This way, we can more tangibly show negative consequences of ignoring return values that indicate errors or missing information, making it more similar to example 2 from https://cwe.mitre.org/data/definitions/252.html

Signed-off-by: s19110 <hubertdan24@gmail.com>
Copy link
Copy Markdown
Contributor

@myteron myteron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compliant code has inline comment "Non-compliant"
gatekeeping would be nice in the compliant02.py
wording in explaining -1

@@ -3,18 +3,25 @@
""" Non-compliant Code Example """
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the compliant version

Suggested change
""" Non-compliant Code Example """
""" Compliant Code Example """

Comment on lines +8 to +19
index_start = full_string.find(sub_string)

if index_start >= 0:
index_end = index_start + len(sub_string)
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])
else:
print(f"There is no '{sub_string}' in '{full_string}'")
# Nothing to wrap, return unchanged string
return full_string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gatekeeping is preferred over if/else.

Suggested change
index_start = full_string.find(sub_string)
if index_start >= 0:
index_end = index_start + len(sub_string)
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])
else:
print(f"There is no '{sub_string}' in '{full_string}'")
# Nothing to wrap, return unchanged string
return full_string
index_start = full_string.find(sub_string)
if index_start < 0:
# Nothing to wrap, return unchanged string
return full_string
index_end = index_start + len(sub_string)
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])

@@ -97,25 +101,32 @@
""" Non-compliant Code Example """
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
""" Non-compliant Code Example """
""" Compliant Code Example """

Comment on lines +108 to +117
if index_start >= 0:
index_end = index_start + len(sub_string)
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])
else:
print(f"There is no '{sub_string}' in '{full_string}'")
# Nothing to wrap, return unchanged string
return full_string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gatekeep

Suggested change
if index_start >= 0:
index_end = index_start + len(sub_string)
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])
else:
print(f"There is no '{sub_string}' in '{full_string}'")
# Nothing to wrap, return unchanged string
return full_string
if index_start < 0:
# Nothing to wrap, return unchanged string
return full_string
index_end = index_start + len(sub_string)
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])


## Compliant Solution - Invalid value handling

Since `str.find()` indicates the fact that the sub-string couldn't be found with a negative index, a simple `if` check is enough to tackle the issue from the previous code example.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our wording is a bit vague how about:

Suggested change
The `str.find()` method returning `-1` when it can't find the string requires to check for `-1` before using the return value as an index.

Copy link
Copy Markdown
Contributor

@BartKaras1128 BartKaras1128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few suggestions. Kinda nitpicking most of them to be honest, but have a look and see if you agree!

Comment on lines +12 to +16
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be one space too many here

Suggested change
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])
return (full_string[:index_start]
+ "\""
+ full_string[index_start:index_end]
+ "\""
+ full_string[index_end:])

#####################
# exploiting above code example
#####################
my_string = "Secure Python coding"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here, but since we're revisiting this, should we conform to "UPPER_CASE" naming style.
Constant name "my_string" doesn't conform to UPPER_CASE naming stylePylintC0103:invalid-name

Suggested change
my_string = "Secure Python coding"
MY_STRING = "Secure Python coding"

find_in_string(my_string, "Python")
find_in_string(my_string, "I'm evil")
print(wrap_in_quotes(my_string, "Secure"))
print(wrap_in_quotes(my_string, "I'm evil")) No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed a newline, may as well have one in, again most linters just suggest this.

Suggested change
print(wrap_in_quotes(my_string, "I'm evil"))
print(wrap_in_quotes(my_string, "I'm evil"))

find_in_string(my_string, "Python")
find_in_string(my_string, "I'm evil")
print(wrap_in_quotes(my_string, "Secure"))
print(wrap_in_quotes(my_string, "I'm evil")) No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just suggestion to have a newline at the end, most linters like this, lights up the IDE less!

Suggested change
print(wrap_in_quotes(my_string, "I'm evil"))
print(wrap_in_quotes(my_string, "I'm evil"))

#####################
# exploiting above code example
#####################
my_string = "Secure Python coding"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here, but since we're revisiting this, should we conform to "UPPER_CASE" naming style.
Constant name "my_string" doesn't conform to UPPER_CASE naming stylePylintC0103:invalid-name

Suggested change
my_string = "Secure Python coding"
MY_STRING = "Secure Python coding"

#####################
# exploiting above code example
#####################
my_string = "Secure Python coding"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion here as in the code example.

Suggested change
my_string = "Secure Python coding"
MY_STRING = "Secure Python coding"

#####################
# exploiting above code example
#####################
my_string = "Secure Python coding"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion here as in the code example:

Suggested change
my_string = "Secure Python coding"
MY_STRING = "Secure Python coding"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants