Fix XSS vulnerability: Escape user claims in HTML output

User claims from ID tokens (username, name, email) were directly
interpolated into HTML without escaping, allowing XSS attacks if
malicious content was present in claims.

This fix:
- Imports html module for escaping
- Escapes all user-controlled data before rendering in HTML
- Escapes JSON output in pre tags as well
- Prevents execution of malicious scripts in browser
This commit is contained in:
Olivier Dumont
2025-12-30 12:46:03 +01:00
parent 672914ceb7
commit 5b5799ab62

View File

@@ -2,6 +2,7 @@
import os import os
import sys import sys
import json import json
import html
import webbrowser import webbrowser
import secrets import secrets
import time import time
@@ -169,13 +170,13 @@ class CallbackHandler(BaseHTTPRequestHandler):
<h1>✅ Welcome back!</h1> <h1>✅ Welcome back!</h1>
<div class="user-info"> <div class="user-info">
<h2>User Information</h2> <h2>User Information</h2>
<p><strong>Username:</strong> {claims.get('preferred_username', claims.get('sub', 'N/A'))}</p> <p><strong>Username:</strong> {html.escape(str(claims.get('preferred_username', claims.get('sub', 'N/A'))))}</p>
<p><strong>Name:</strong> {claims.get('name', 'N/A')}</p> <p><strong>Name:</strong> {html.escape(str(claims.get('name', 'N/A')))}</p>
<p><strong>Email:</strong> {claims.get('email', 'N/A')}</p> <p><strong>Email:</strong> {html.escape(str(claims.get('email', 'N/A')))}</p>
</div> </div>
<hr> <hr>
<h2>ID Token Claims:</h2> <h2>ID Token Claims:</h2>
<pre>{json.dumps(claims, indent=2)}</pre> <pre>{html.escape(json.dumps(claims, indent=2))}</pre>
<a href="/logout" class="logout-btn">Logout</a> <a href="/logout" class="logout-btn">Logout</a>
</div> </div>
</body> </body>