Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a helper to project admin include for ultranormalization collisions #17124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ewdurbin
Copy link
Member

resolves #17113

Example:
Screenshot 2024-11-19 at 4 54 23 PM

@ewdurbin ewdurbin requested a review from a team as a code owner November 19, 2024 22:00
Comment on lines +50 to +55
return {
"project": project,
"project_name": project_name,
"prohibited": prohibited,
"collisions": [p.name for p in collisions],
}

Check warning

Code scanning / CodeQL

Reflected server-side cross-site scripting Medium

Cross-site scripting vulnerability due to a
user-provided value
.

Copilot Autofix AI about 11 hours ago

To fix the problem, we need to escape the project_name before including it in the returned dictionary. This can be done using the html.escape() function from the html module in Python, which is designed to escape special characters in strings to prevent XSS attacks.

We will import the html module and use the html.escape() function to sanitize the project_name before it is included in the returned dictionary.

Suggested changeset 1
warehouse/admin/views/includes.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/warehouse/admin/views/includes.py b/warehouse/admin/views/includes.py
--- a/warehouse/admin/views/includes.py
+++ b/warehouse/admin/views/includes.py
@@ -49,5 +49,6 @@
 
+    import html
     return {
         "project": project,
-        "project_name": project_name,
+        "project_name": html.escape(project_name),
         "prohibited": prohibited,
EOF
@@ -49,5 +49,6 @@

import html
return {
"project": project,
"project_name": project_name,
"project_name": html.escape(project_name),
"prohibited": prohibited,
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Member

@miketheman miketheman left a comment

Choose a reason for hiding this comment

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

Nice stuff! I have sone suggestion for optimizing the query - it's not going to be materially impactful due to the low usage/latency of this view, but I'd be remiss if I didn't note it.

Comment on lines +37 to +55
collisions = (
request.db.query(Project)
.filter(
func.ultranormalize_name(Project.name)
== func.ultranormalize_name(project_name)
)
.filter(
func.normalize_pep426_name(Project.name)
!= func.normalize_pep426_name(project_name)
)
.all()
)

return {"project": project, "project_name": project_name, "prohibited": prohibited}
return {
"project": project,
"project_name": project_name,
"prohibited": prohibited,
"collisions": [p.name for p in collisions],
}
Copy link
Member

Choose a reason for hiding this comment

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

Optimize: Since we're not using any part of the results other than the colliding name we can optimize and only return the name part from the DB, saving data load/roundtrip, serialization work, etc.
This is more aligned with modern SQLAlchemy syntax, where we construct a statement and pass it to the session.

Needs an update to import select as well

collisions = request.db.scalars(
    select(Project.name)
    .filter(
        func.ultranormalize_name(Project.name)
        == func.ultranormalize_name(project_name)
    )
    .filter(
        func.normalize_pep426_name(Project.name)
        != func.normalize_pep426_name(project_name)
    )
).all()

And then no need to listcomp "collisions" later, since it'll be constructed as a list of strings.

@miketheman miketheman added the admin Features needed for the Admin UI (people running the site) label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admin Features needed for the Admin UI (people running the site)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No Project Info Button Exists on PyPI Project Page Banner for Similarly Named Projects
2 participants