Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ build-backend = "hatchling.build"
line-length = 120

[tool.ruff.lint]
select = ["E", "I"]
select = ["E", "I", "S"]

[tool.coverage.run]
branch = true
Expand Down
20 changes: 18 additions & 2 deletions scripts/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@
@option("--email", required=True, help="Author email")
@option("--github", required=True, help="GitHub username")
def main(name: str, description: str, author: str, email: str, github: str):
# Validate name to prevent directory traversal or other injection
# Validate inputs to prevent configuration injection
for label, value in [
("name", name),
("description", description),
("author", author),
("email", email),
("github", github),
]:
if "\n" in value or "\r" in value:
raise UsageError(f"Invalid {label}: newlines are not allowed.")
if label != "description" and '"' in value:
raise UsageError(f"Invalid {label}: double quotes are not allowed.")

if not re.match(r"^[a-zA-Z0-9_-]+$", name):
raise UsageError(
f"Invalid project name '{name}'. Only alphanumeric characters, dashes, and underscores are allowed."
)

# Sanitize description for TOML double-quoted strings
description = description.replace('"', '\\"')

source = name.replace("-", "_").lower()

echo(f"Initializing project '{name}' (source: '{source}')...")
Expand Down Expand Up @@ -51,7 +66,8 @@ def main(name: str, description: str, author: str, email: str, github: str):
continue

content = path.read_text()
new_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
# Use a lambda for replacement to avoid regex backreference injection
new_content = re.sub(pattern, lambda _: replacement, content, flags=re.MULTILINE)
path.write_text(new_content)
echo(f"Updated {filepath}")

Expand Down