From f02e68b1d3b0b53f34f7557afac104ba3051e78f Mon Sep 17 00:00:00 2001 From: whoswhip Date: Sat, 7 Sep 2024 13:59:33 -0400 Subject: [PATCH] Enhance validation, logging, and UI/UX improvements ## COMMIT MESSAGE GENERATED BY COPILOT Added JSON validation to multiple endpoints, ensuring valid JSON requests and improved error handling. Updated logging to use `DateTime.Now` and append to `logs.log` for specific actions. Enhanced `/api/accounts/login` with JSON validation and refactored for consistency. Improved admin and user interfaces with search functionality, better date localization, and CSS adjustments. Enhanced error handling and feedback in `login.html` and `signup.html`, and improved search functionality in `archives.html`. --- PasteBin/Program.cs | 221 ++++++++++++--- PasteBin/Properties/launchSettings.json | 15 +- PasteBin/Sharpbin.csproj | 8 +- PasteBin/assets/admin.html | 337 +++++++++++++---------- PasteBin/assets/admin/user.html | 2 +- PasteBin/assets/archives.html | 96 ++++--- PasteBin/assets/index.html | 3 + PasteBin/assets/login.html | 24 ++ PasteBin/assets/signup.html | 347 +++++++++++++----------- PasteBin/assets/user.html | 2 +- 10 files changed, 660 insertions(+), 395 deletions(-) diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index 6dc077d..5947416 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -916,6 +916,17 @@ class Program } } string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); + try + { + JObject.Parse(requestBody); + } + catch + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid JSON" }.ToString()); + return; + } if (string.IsNullOrEmpty(requestBody)) { context.Response.StatusCode = 400; @@ -987,7 +998,8 @@ class Program punishedUsersCommand.Parameters.AddWithValue("@Punisher", loggedInUser.UUID); await punishedUsersCommand.ExecuteNonQueryAsync(); } - Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] Account {user.Username}({user.UUID}) has been banned by {loggedInUser.Username}({loggedInUser.UUID}) for {reason}"); + Console.WriteLine($"[{DateTime.Now}] Account {user.Username}({user.UUID}) has been banned by {loggedInUser.Username}({loggedInUser.UUID}) for {reason}"); + await File.AppendAllTextAsync("logs.log", $"[{DateTime.Now}] Account {user.Username}({user.UUID}) has been banned by {loggedInUser.Username}({loggedInUser.UUID}) for {reason}\n"); context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; await context.Response.WriteAsJsonAsync(new JObject { ["message"] = "User banned" }.ToString()); @@ -1040,7 +1052,17 @@ class Program { uploaderUUID = $"Anonymous-{Guid.NewGuid().ToString()}"; } - + try + { + JObject.Parse(requestBody); + } + catch + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid JSON" }.ToString()); + return; + } var data = JObject.Parse(requestBody); var title = SanitizeInput(data["title"]?.ToString() ?? "Untitled"); var content = SanitizeInput(data["content"]?.ToString() ?? ""); @@ -1082,7 +1104,7 @@ class Program context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; - string time = DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0).DateTime.ToString("yyyy-MM-dd HH:mm:ss"); + string time = DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0).ToString().Split("+")[0].TrimEnd(); JObject responseJson = new JObject(); responseJson["pasteId"] = paste.Id; @@ -1118,6 +1140,17 @@ class Program await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No data uploaded" }.ToString()); return; } + try + { + JObject.Parse(requestBody); + } + catch + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid JSON" }.ToString()); + return; + } var data = JObject.Parse(requestBody); var id = SanitizeInput(data["id"]?.ToString() ?? ""); if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id)) @@ -1188,16 +1221,93 @@ class Program await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No data uploaded" }.ToString()); return; } - var data = JObject.Parse(requestBody); - var query = SanitizeInput(data["query"]?.ToString() ?? ""); - Console.WriteLine($"erm: {query}"); - if (string.IsNullOrEmpty(query) || string.IsNullOrWhiteSpace(query)) + try + { + JObject.Parse(requestBody); + } + catch { context.Response.StatusCode = 400; context.Response.ContentType = "application/json"; - await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString()); + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid JSON" }.ToString()); return; } + var data = JObject.Parse(requestBody); + var query = SanitizeInput(data["query"]?.ToString() ?? ""); + + if (string.IsNullOrEmpty(query) || string.IsNullOrWhiteSpace(query)) + { + using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) + { + await connection.OpenAsync(); + var command = connection.CreateCommand(); + command.CommandText = $"SELECT * FROM pastes WHERE Visibility != 'Private' ORDER BY UID DESC LIMIT 50"; + var reader = await command.ExecuteReaderAsync(); + string pastes = string.Empty; + JArray pastesArray = new JArray(); + while (await reader.ReadAsync()) + { + var paste = new Paste + { + Title = reader.GetString(1), + UnixDate = long.Parse(reader.GetString(2)), + Size = reader.GetString(3), + Visibility = reader.GetString(4), + Id = reader.GetString(5), + }; + string title = paste.Title; + if (paste.Visibility == "Private" || paste.Visibility == "Unlisted") + { + continue; + } + if (paste.Title == "" || string.IsNullOrEmpty(paste.Title)) + { + title = $"Untitled {reader.GetString(0)}"; + } + if (paste.Title.Length > 40) + { + title = paste.Title.Substring(0, 40) + "..."; + } + var difference = GetTimeDifference(DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0), DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.Now.ToUnixTimeSeconds())); + if (string.IsNullOrEmpty(parameters["html"])) + { + JObject pasteJson = new JObject(); + pasteJson["title"] = paste.Title; + pasteJson["id"] = paste.Id; + pasteJson["size"] = ConvertToBytes(paste.Size); + pasteJson["date"] = difference; + pasteJson["visibility"] = paste.Visibility; + pastesArray.Add(pasteJson); + } + else + { + pastes += $"
  • {title} {difference} {ConvertToBytes(paste.Size)}
  • "; + } + } + if (string.IsNullOrEmpty(pastes) && pastesArray.Count == 0) + { + pastes = "

    No pastes found

    "; + pastesArray.Add(new JObject { ["error"] = "No pastes found" }); + context.Response.StatusCode = 404; + } + else + { + context.Response.StatusCode = 200; + } + + if (string.IsNullOrEmpty(parameters["html"])) + { + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(pastesArray.ToString()); + } + else + { + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(pastes); + } + return; + } + } using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) { @@ -1255,7 +1365,19 @@ class Program }).RequireRateLimiting("fixed"); app.MapPost("/api/accounts/create", async (HttpContext context) => { + string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); + try + { + JObject.Parse(requestBody); + } + catch + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid JSON" }.ToString()); + return; + } Log log = await Logging.LogRequestAsync(context); char[] disallowed = { ' ', '\'', '\"', '\\', '/', '(', ')', '[', ']', '{', '}', '<', '>', ';', ':', ',', '.', '!', '?', '@', '#', '$', '%', '^', '&', '*', '-', '+', '=', '~', '`' }; @@ -1300,9 +1422,9 @@ class Program await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid characters in username" }.ToString()); return; } - string ip = Convert.ToBase64String(SHA1.HashData(Encoding.UTF8.GetBytes(log.IP))); - string token = Convert.ToBase64String(Encoding.UTF8.GetBytes(BCrypt.Net.BCrypt.HashPassword(Guid.NewGuid().ToString()))); - string passwordHash = BCrypt.Net.BCrypt.HashPassword(password, salt); + string token = string.Empty; + string ip = string.Empty; + string passwordHash = string.Empty; DateTime expirationDate = DateTime.Now.AddYears(1); using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) @@ -1324,6 +1446,10 @@ class Program { reader.Close(); } + + ip = Convert.ToBase64String(SHA1.HashData(Encoding.UTF8.GetBytes(log.IP))); + token = Convert.ToBase64String(Encoding.UTF8.GetBytes(BCrypt.Net.BCrypt.HashPassword(Guid.NewGuid().ToString()))); + passwordHash = BCrypt.Net.BCrypt.HashPassword(password, salt); var insertCommand = connection.CreateCommand(); insertCommand.CommandText = "INSERT INTO users (UUID, Username, PasswordHash, CreationDate, LastLoginDate, Type, State) VALUES (@UUID, @Username, @PasswordHash, @CreationDate, @LastLoginDate, @Type, @State)"; insertCommand.Parameters.AddWithValue("@UUID", Guid.NewGuid().ToString()); @@ -1356,36 +1482,47 @@ class Program context.Response.Cookies.Append("token", token, cookie); context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; - Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] Account {username} has been created"); - File.AppendAllText("logs.log", $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] Account {username} has been created by {log.IP}\n"); + Console.WriteLine($"[{DateTime.Now}] Account {username} has been created"); + File.AppendAllText("logs.log", $"[{DateTime.Now}] Account {username} has been created by {log.IP}\n"); await context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Account created" }.ToString()); }).RequireRateLimiting("fixed"); - app.MapPost("/api/accounts/login", async (HttpContext Context) => + app.MapPost("/api/accounts/login", (Delegate)(async (HttpContext context) => { - string requestBody = await new StreamReader(Context.Request.Body).ReadToEndAsync(); - Log log = await Logging.LogRequestAsync(Context); - - if (IsLoggedInAsync(Context.Request.Cookies["token"]).Result) + string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); + try { - Context.Response.StatusCode = 400; - Context.Response.ContentType = "application/json"; - await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Already logged in" }.ToString()); + JObject.Parse(requestBody); + } + catch + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid JSON" }.ToString()); + return; + } + Log log = await Logging.LogRequestAsync(context); + + if (IsLoggedInAsync(context.Request.Cookies["token"]).Result) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Already logged in" }.ToString()); return; } if (string.IsNullOrEmpty(requestBody)) { - Context.Response.StatusCode = 400; - Context.Response.ContentType = "application/json"; - await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No data uploaded" }.ToString()); + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No data uploaded" }.ToString()); return; } - if (!requestBody.Contains("username") || !requestBody.Contains("password") || Context.Request.ContentType != "application/json") + if (!requestBody.Contains("username") || !requestBody.Contains("password") || context.Request.ContentType != "application/json") { - Context.Response.StatusCode = 400; - Context.Response.ContentType = "application/json"; - await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString()); + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString()); return; } @@ -1395,9 +1532,9 @@ class Program if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { - Context.Response.StatusCode = 400; - Context.Response.ContentType = "application/json"; - await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); return; } @@ -1414,9 +1551,9 @@ class Program await reader.ReadAsync(); if (!reader.HasRows) { - Context.Response.StatusCode = 400; - Context.Response.ContentType = "application/json"; - await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); return; } if (BCrypt.Net.BCrypt.Verify(password, reader.GetString(3))) @@ -1448,22 +1585,22 @@ class Program SameSite = SameSiteMode.Strict }; - Context.Response.Cookies.Append("token", token, cookie); - Context.Response.StatusCode = 200; - Context.Response.ContentType = "application/json"; + context.Response.Cookies.Append("token", token, cookie); + context.Response.StatusCode = 200; + context.Response.ContentType = "application/json"; Console.WriteLine($"[{DateTime.Now}] Account {username} has logged in"); File.AppendAllText("logs.log", $"[{DateTime.Now}] Account {username} has logged in from {log.IP}\n"); - await Context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Logged in" }.ToString()); + await context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Logged in" }.ToString()); return; } else { - Context.Response.StatusCode = 400; - Context.Response.ContentType = "application/json"; - await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); } } - }).RequireRateLimiting("fixed"); + })).RequireRateLimiting("fixed"); #endregion diff --git a/PasteBin/Properties/launchSettings.json b/PasteBin/Properties/launchSettings.json index cd8d711..aafdd12 100644 --- a/PasteBin/Properties/launchSettings.json +++ b/PasteBin/Properties/launchSettings.json @@ -12,25 +12,16 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "applicationUrl": "http://*:5240", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } + "applicationUrl": "http://*:5240" }, "https": { "commandName": "Project", "dotnetRunMessages": true, - "applicationUrl": "https://*:7220;http://*:5240", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } + "applicationUrl": "https://*:7220;http://*:5240" }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } + "launchBrowser": true } } } diff --git a/PasteBin/Sharpbin.csproj b/PasteBin/Sharpbin.csproj index 1649fde..4bd01a4 100644 --- a/PasteBin/Sharpbin.csproj +++ b/PasteBin/Sharpbin.csproj @@ -13,14 +13,14 @@ - - - - + + + + diff --git a/PasteBin/assets/admin.html b/PasteBin/assets/admin.html index 5db50ef..a102730 100644 --- a/PasteBin/assets/admin.html +++ b/PasteBin/assets/admin.html @@ -1,11 +1,13 @@ + create paste + -
    +
    @@ -37,6 +39,7 @@

    Accounts

    +
    {accounts}
    @@ -53,160 +56,204 @@ return date.toLocaleString(); } - document.addEventListener("DOMContentLoaded", function() { + document.addEventListener("DOMContentLoaded", function () { const creationdateElement = document.getElementById('creationdate'); const lastlogindateElement = document.getElementById('lastlogindate'); - + const creationdateText = creationdateElement.innerText.trim(); const lastlogindateText = lastlogindateElement.innerText.trim(); - + creationdateElement.innerText = "Creation Date: " + localizeDateTime(parseInt(creationdateText)); lastlogindateElement.innerText = "Last Login Date: " + localizeDateTime(parseInt(lastlogindateText)); + }); + function search() { + const input = document.querySelector('.text-input').value; + const accountWrapper = document.querySelector('.accounts-wrapper'); + const accounts = accountWrapper.querySelectorAll('.list-a'); + accounts.forEach(account => { + const username = account.querySelector('.list-item').innerText; + if (username.toLowerCase().includes(input.toLowerCase())) { + account.style.display = 'flex'; + } else { + account.style.display = 'none'; + } }); + } + \ No newline at end of file diff --git a/PasteBin/assets/admin/user.html b/PasteBin/assets/admin/user.html index 766bb4f..44d7aa5 100644 --- a/PasteBin/assets/admin/user.html +++ b/PasteBin/assets/admin/user.html @@ -139,7 +139,7 @@ border-radius: 5px; padding: 20px; height: fit-content; - margin-top: 20px; + margin-top: 26px; height: 77.1%; max-height: 77.1%; } diff --git a/PasteBin/assets/archives.html b/PasteBin/assets/archives.html index 019afe8..4eeba8a 100644 --- a/PasteBin/assets/archives.html +++ b/PasteBin/assets/archives.html @@ -32,54 +32,59 @@ + \ No newline at end of file diff --git a/PasteBin/assets/user.html b/PasteBin/assets/user.html index 174d033..6a35552 100644 --- a/PasteBin/assets/user.html +++ b/PasteBin/assets/user.html @@ -147,7 +147,7 @@ max-width: 100%; background-color: #222222; border-radius: 5px; - padding: 20px; + padding: 26px; height: fit-content; height: 100%; max-height: 100%;