Skip to content

Commit cfd7b09

Browse files
authored
Add a mailto link generator to easily report malicious packages. (#93)
* Add a mailto link generator to easily report malicious packages. * Applied black reformat.
1 parent 0dde455 commit cfd7b09

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

inspector/main.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def handle_bad_request(e):
4848
@app.route("/")
4949
def index():
5050
if project := request.args.get("project"):
51-
return redirect(f"/project/{ project }")
51+
return redirect(f"/project/{project}")
5252
return render_template("index.html")
5353

5454

@@ -78,7 +78,7 @@ def versions(project_name):
7878
def distributions(project_name, version):
7979
resp = requests.get(f"https://pypi.org/pypi/{project_name}/{version}/json")
8080
if resp.status_code != 200:
81-
return redirect(f"/project/{ project_name }/")
81+
return redirect(f"/project/{project_name}/")
8282

8383
dist_urls = [
8484
"." + urllib.parse.urlparse(url["url"]).path + "/"
@@ -200,22 +200,47 @@ def distribution(project_name, version, first, second, rest, distname):
200200
return "Distribution type not supported"
201201

202202

203+
def mailto_report_link(project_name, version, file_path, request_url):
204+
"""
205+
Generate a mailto report link for malicious code.
206+
"""
207+
message_body = (
208+
"PyPI Malicious Package Report\n"
209+
"--\n"
210+
f"Package Name: {project_name}\n"
211+
f"Version: {version}\n"
212+
f"File Path: {file_path}\n"
213+
f"Inspector URL: {request_url}\n\n"
214+
"Additional Information:\n\n"
215+
)
216+
217+
subject = f"Malicious Package Report: {project_name}"
218+
219+
return (
220+
f"mailto:security@pypi.org?"
221+
f"subject={urllib.parse.quote(subject)}"
222+
f"&body={urllib.parse.quote(message_body)}"
223+
)
224+
225+
203226
@app.route(
204227
"/project/<project_name>/<version>/packages/<first>/<second>/<rest>/<distname>/<path:filepath>" # noqa
205228
)
206229
def file(project_name, version, first, second, rest, distname, filepath):
207230
dist = _get_dist(first, second, rest, distname)
208-
209231
if dist:
210232
try:
211233
contents = dist.contents(filepath)
212234
except UnicodeDecodeError:
213235
return "Binary files are not supported"
214236
except FileNotFoundError:
215237
return abort(404)
238+
239+
report_link = mailto_report_link(project_name, version, filepath, request.url)
216240
return render_template(
217241
"code.html",
218242
code=contents,
243+
mailto_report_link=report_link,
219244
h2=f"{project_name}",
220245
h2_link=f"/project/{project_name}",
221246
h2_paren="View this project on PyPI",

inspector/templates/code.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{% endblock %}
66

77
{% block body %}
8+
<a href="{{ mailto_report_link }}" style="color:red"> <strong>Report Malicious Package</strong> </a>
89
<pre id="line" class="line-numbers linkable-line-numbers language-python">
910
<code class="language-python">{{- code }}</code>
1011
</pre>

0 commit comments

Comments
 (0)