@@ -26,6 +26,15 @@ const HTML_SUCCESS = `<!DOCTYPE html>
2626</body>
2727</html>`
2828
29+ function escapeHtml ( value : string ) {
30+ return value
31+ . replaceAll ( "&" , "&" )
32+ . replaceAll ( "<" , "<" )
33+ . replaceAll ( ">" , ">" )
34+ . replaceAll ( '"' , """ )
35+ . replaceAll ( "'" , "'" )
36+ }
37+
2938const HTML_ERROR = ( error : string ) => `<!DOCTYPE html>
3039<html>
3140<head>
@@ -42,7 +51,7 @@ const HTML_ERROR = (error: string) => `<!DOCTYPE html>
4251 <div class="container">
4352 <h1>Authorization Failed</h1>
4453 <p>An error occurred during authorization.</p>
45- <div class="error">${ error } </div>
54+ <div class="error">${ escapeHtml ( error ) } </div>
4655 </div>
4756</body>
4857</html>`
@@ -87,7 +96,7 @@ function handleRequest(req: import("http").IncomingMessage, res: import("http").
8796 // Enforce state parameter presence
8897 if ( ! state ) {
8998 const errorMsg = "Missing required state parameter - potential CSRF attack"
90- res . writeHead ( 400 , { "Content-Type" : "text/html" } )
99+ res . writeHead ( 400 , { "Content-Type" : "text/html; charset=utf-8 " } )
91100 res . end ( HTML_ERROR ( errorMsg ) )
92101 return
93102 }
@@ -101,21 +110,21 @@ function handleRequest(req: import("http").IncomingMessage, res: import("http").
101110 cleanupStateIndex ( state )
102111 pending . reject ( new Error ( errorMsg ) )
103112 }
104- res . writeHead ( 200 , { "Content-Type" : "text/html" } )
113+ res . writeHead ( 200 , { "Content-Type" : "text/html; charset=utf-8 " } )
105114 res . end ( HTML_ERROR ( errorMsg ) )
106115 return
107116 }
108117
109118 if ( ! code ) {
110- res . writeHead ( 400 , { "Content-Type" : "text/html" } )
119+ res . writeHead ( 400 , { "Content-Type" : "text/html; charset=utf-8 " } )
111120 res . end ( HTML_ERROR ( "No authorization code provided" ) )
112121 return
113122 }
114123
115124 // Validate state parameter
116125 if ( ! pendingAuths . has ( state ) ) {
117126 const errorMsg = "Invalid or expired state parameter - potential CSRF attack"
118- res . writeHead ( 400 , { "Content-Type" : "text/html" } )
127+ res . writeHead ( 400 , { "Content-Type" : "text/html; charset=utf-8 " } )
119128 res . end ( HTML_ERROR ( errorMsg ) )
120129 return
121130 }
@@ -127,7 +136,7 @@ function handleRequest(req: import("http").IncomingMessage, res: import("http").
127136 cleanupStateIndex ( state )
128137 pending . resolve ( code )
129138
130- res . writeHead ( 200 , { "Content-Type" : "text/html" } )
139+ res . writeHead ( 200 , { "Content-Type" : "text/html; charset=utf-8 " } )
131140 res . end ( HTML_SUCCESS )
132141}
133142
0 commit comments