patx/gitman

Render profile bios as sanitized README-style Markdown

Commit 8d1b782 · patx · Nov 4 at 3:02AM

Changeset
8d1b782b4982ac264e5811742aa5ae2dfeaf7923
Parents
c7c7a36bcec8deea5169c12d3d397d5bf0db55e6

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/app.py b/app.py
index 31c0243..86f48b0 100644
--- a/app.py
+++ b/app.py
@@ -1948,7 +1948,7 @@ def profile_form_values(form, fallback=None):
     return {
         "username": fallback.get("username", ""),
         "display_name": (form.get("display_name", fallback.get("display_name", "")) or "").strip()[:80],
-        "bio": (form.get("bio", fallback.get("bio", "")) or "").strip()[:1000],
+        "bio": (form.get("bio", fallback.get("bio", "")) or "").strip()[:10000],
         "website": (form.get("website", fallback.get("website", "")) or "").strip(),
         "created_at": fallback.get("created_at", ""),
         "theme_preference": (form.get("theme_preference", fallback.get("theme_preference", "auto")) or "auto").strip(),
diff --git a/static/styles.css b/static/styles.css
index c6ab8a5..43acada 100644
--- a/static/styles.css
+++ b/static/styles.css
@@ -389,6 +389,9 @@ th { color: var(--ink); font-weight: 700; }
 
 .markdown-body { overflow-x: auto; }
 .markdown-body img { max-width: 100%; }
+.profile-bio { margin-bottom: .75rem; }
+.profile-bio > :last-child { margin-bottom: 0; }
+.field-help { margin: .5rem 0 0; color: var(--muted); font-size: .875rem; }
 .markdown-body h1 { font-size: 1.5rem; margin: 1.5rem 0 .75rem; }
 .markdown-body h2 { font-size: 1.25rem; margin: 1.5rem 0 .75rem; }
 .markdown-body h3 { font-size: 1.05rem; margin: 1.25rem 0 .6rem; }
diff --git a/templates/base.tpl b/templates/base.tpl
index a0c6401..ab2f649 100644
--- a/templates/base.tpl
+++ b/templates/base.tpl
@@ -20,7 +20,7 @@
   % if not get('landing', False) and theme != 'light':
     <link rel="stylesheet" href="/static/vendor/highlight.js/github-dark.min.css"{{!' media="(prefers-color-scheme: dark)"' if theme == 'auto' else ''}}>
   % end
-  <link rel="stylesheet" href="/static/styles.css?v=22">
+  <link rel="stylesheet" href="/static/styles.css?v=23">
 </head>
 <body class="{{'landing-page' if get('landing', False) else ''}}">
   % if not get("landing", False):
diff --git a/templates/edit_profile.tpl b/templates/edit_profile.tpl
index bb460aa..d604e52 100644
--- a/templates/edit_profile.tpl
+++ b/templates/edit_profile.tpl
@@ -11,10 +11,13 @@
           Display name
           <input name="display_name" value="{{profile['display_name']}}" maxlength="80">
         </label>
-        <label>
-          Bio
-          <textarea name="bio" rows="6" maxlength="1000">{{profile['bio']}}</textarea>
-        </label>
+        <div>
+          <label for="profile-bio">
+            Bio
+            <textarea id="profile-bio" name="bio" rows="10" maxlength="10000" aria-describedby="profile-bio-help">{{profile['bio']}}</textarea>
+          </label>
+          <p id="profile-bio-help" class="field-help">Supports Markdown, just like a repository README.</p>
+        </div>
         <label>
           Website
           <input name="website" value="{{profile['website']}}" placeholder="https://example.com" maxlength="255">
diff --git a/templates/profile.tpl b/templates/profile.tpl
index 26d551b..012c1f5 100644
--- a/templates/profile.tpl
+++ b/templates/profile.tpl
@@ -13,7 +13,7 @@
     Joined {{profile_user["created_at"]}}
   </small></p>
   % if profile_user["bio"]:
-    <p>{{!render_markdown_links(profile_user["bio"])}}</p>
+    <div class="markdown-body profile-bio">{{!render_markdown(profile_user["bio"])}}</div>
   % end
   % if profile_user["website"]:
     <p><a href="{{profile_user['website']}}" rel="nofollow">{{profile_user["website"]}}</a></p>
diff --git a/tests/test_app.py b/tests/test_app.py
index 3305c36..a48d103 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -2428,15 +2428,25 @@ def test_bottle_profile_star_fork_and_repo_settings_flows(isolated_app):
     response = login_client(owner_client, "alice")
     assert response.status_code == 303
 
+    bio = ('## About me\n\n- **Git maintainer**\n- [My work](https://example.com/work)\n\n'
+        '```python\nprint("hello")\n```\n\n'
+        '<script>alert("bio-xss")</script><img src="x" onerror="alert(1)">\n\n' + 'Profile text. ' * 100)
     response = owner_client.post(
         "/settings/profile",
-        {"display_name": "Alice A.", "bio": "Git maintainer", "website": "example.com"},
+        {"display_name": "Alice A.", "bio": bio, "website": "example.com"},
     )
     assert response.status_code == 200
     assert "Profile updated." in response.text
     profile = owner_client.get("/alice")
     assert "Alice A." in profile.text
     assert "https://example.com" in profile.text
+    assert isolated_app.get_user_by_username('alice')['bio'] == bio.strip()
+    assert '<h2>About me</h2>' in profile.text
+    assert '<li><strong>Git maintainer</strong></li>' in profile.text
+    assert '<a href="https://example.com/work">My work</a>' in profile.text
+    assert '<pre><code>print(' in profile.text
+    assert 'bio-xss' not in profile.text
+    assert 'onerror=' not in profile.text
     assert 'style="color:black;"' not in profile.text
     settings_response = owner_client.get("/alice/demo/settings")
     assert settings_response.status_code == 200