|
4 | 4 | using Microsoft.IdentityModel.Tokens;
|
5 | 5 | using Microsoft.AspNetCore.Http;
|
6 | 6 | using System.Text;
|
| 7 | +using System.Web; |
7 | 8 |
|
8 | 9 | namespace VulnerableWebApplication.MidlWare
|
9 | 10 | {
|
@@ -40,40 +41,57 @@ public ValidateJwtMiddleware(RequestDelegate next)
|
40 | 41 | public async Task InvokeAsync(HttpContext context, IConfiguration configuration)
|
41 | 42 | {
|
42 | 43 | /*
|
43 |
| - Authentifie les utilisateurs |
44 |
| - */ |
| 44 | + * Authentifie les utilisateur |
| 45 | + * |
| 46 | + * Renvoi certains messages d'érreur |
| 47 | + */ |
| 48 | + var path = context.Request.Path.Value; |
| 49 | + if (path.Contains("script", StringComparison.OrdinalIgnoreCase)) path = HttpUtility.HtmlEncode(path); //XSS protection ;) |
45 | 50 |
|
46 | 51 | string authHeader = context.Request.Headers["Authorization"];
|
47 |
| - string UnauthMsg = "Welcome to vulnerableLightApp. You are not authenticated. Source code is available at https://github.com/Aif4thah/VulnerableLightApp"; |
48 | 52 |
|
49 |
| - // URL Without Authentication |
50 |
| - var path = context.Request.Path.Value; |
| 53 | + string UnauthMsg = "<html><head><title>Accès interdit</title></head>" + |
| 54 | + "<body><h1>Erreur 401 - Accès non autorisé</h1>" + |
| 55 | + "<p>START HACKING !</p></body></html>"; |
| 56 | + |
| 57 | + string NotFoundMsg = "<html><head><title>Page introuvable</title></head>" + |
| 58 | + "<body><h1>Erreur 404 - Page non trouvée</h1>" + |
| 59 | + "<p>La ressource " + path + " n'existe pas ou a été déplacée.</p></body></html>"; |
| 60 | + |
51 | 61 | if (path.Equals("/login", StringComparison.OrdinalIgnoreCase) || path.StartsWith("/swagger", StringComparison.OrdinalIgnoreCase))
|
52 | 62 | {
|
53 | 63 | await _next(context);
|
54 | 64 | return;
|
55 | 65 | }
|
56 | 66 |
|
57 |
| - // User Authentication |
58 | 67 | if (string.IsNullOrEmpty(authHeader) || !VLAIdentity.VLAIdentity.VulnerableValidateToken(authHeader, configuration["Secret"]))
|
59 | 68 | {
|
60 | 69 | context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
61 |
| - var bytes = Encoding.UTF8.GetBytes(UnauthMsg); |
62 |
| - context.Response.Body.WriteAsync(bytes, 0, bytes.Length); |
| 70 | + context.Response.ContentType = "text/html; charset=utf-8"; |
| 71 | + await context.Response.WriteAsync(UnauthMsg, Encoding.UTF8); |
63 | 72 | return;
|
64 | 73 | }
|
65 | 74 |
|
66 |
| - // Admin Authentication |
67 |
| - if (path.StartsWith("/Patch", StringComparison.OrdinalIgnoreCase) && (string.IsNullOrEmpty(authHeader) || !VLAIdentity.VLAIdentity.VulnerableAdminValidateToken(authHeader, configuration["Secret"])) ) |
| 75 | + if (path.StartsWith("/Patch", StringComparison.OrdinalIgnoreCase) && (string.IsNullOrEmpty(authHeader) || !VLAIdentity.VLAIdentity.VulnerableAdminValidateToken(authHeader, configuration["Secret"]))) |
68 | 76 | {
|
69 | 77 | context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
70 |
| - var bytes = Encoding.UTF8.GetBytes(UnauthMsg); |
71 |
| - context.Response.Body.WriteAsync(bytes, 0, bytes.Length); |
| 78 | + context.Response.ContentType = "text/html; charset=utf-8"; |
| 79 | + await context.Response.WriteAsync(UnauthMsg, Encoding.UTF8); |
72 | 80 | return;
|
73 | 81 | }
|
74 | 82 |
|
75 | 83 | await _next(context);
|
| 84 | + |
| 85 | + // 🔹 Gestion du 404 après exécution du pipeline |
| 86 | + if (context.Response.StatusCode == StatusCodes.Status404NotFound) |
| 87 | + { |
| 88 | + context.Response.ContentType = "text/html; charset=utf-8"; |
| 89 | + await context.Response.WriteAsync(NotFoundMsg, Encoding.UTF8); |
| 90 | + } |
76 | 91 | }
|
| 92 | + |
| 93 | + |
| 94 | + |
77 | 95 | }
|
78 | 96 |
|
79 | 97 | }
|
0 commit comments