Skip to content

Commit 1c15520

Browse files
Improve control character detection in input validation (#102)
1 parent e8b1868 commit 1c15520

3 files changed

Lines changed: 6 additions & 2 deletions

File tree

‎project/app.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def main(name: str = "World"):
2525
"""
2626
if len(name) > 100:
2727
raise UsageError("Invalid name: maximum length is 100 characters.")
28-
if any(c < " " for c in name):
28+
if any(not c.isprintable() for c in name):
2929
raise UsageError("Invalid name: control characters are not allowed.")
3030

3131
secho(f"Hello {name}! 👋", fg="green", bold=True)

‎scripts/rename.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def main(name: str, description: str, author: str, email: str, github: str):
7878
]:
7979
if len(value) > 100:
8080
raise UsageError(f"Invalid {label}: maximum length is 100 characters.")
81-
if any(c < " " for c in value):
81+
if any(not c.isprintable() for c in value):
8282
raise UsageError(f"Invalid {label}: control characters are not allowed.")
8383
if label != "description" and '"' in value:
8484
raise UsageError(f"Invalid {label}: double quotes are not allowed.")

‎tests/test_app.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ def test_name_control_characters():
3838
result = runner.invoke(main, ["--name", "Injected\x1b[31mRed\x1b[0m"])
3939
assert result.exit_code != 0
4040
assert "control characters are not allowed" in result.output
41+
42+
result = runner.invoke(main, ["--name", "test\x7f"])
43+
assert result.exit_code != 0
44+
assert "control characters are not allowed" in result.output

0 commit comments

Comments
 (0)