From 59fe4caa4dce6674b830d90f534288b686c39f0d Mon Sep 17 00:00:00 2001 From: whoswhip Date: Mon, 19 Aug 2024 04:46:58 -0400 Subject: [PATCH] Commit Message Generated by Copilot Enhance date handling, error checks, data limits, and rate limiting - Added `using System.Globalization;` to support date and time formatting. - Handled empty/null paste titles with defaults and truncation. - Display paste dates as human-readable time differences. - Changed rate limiting policy to "fixed-bigger". - Improved error handling for `/api/create` endpoint. - Updated `ConvertToBytes` method for clarity. - Added `GetDifference` method for date comparisons. - Enforced length limits on input fields in `index.html`. --- PasteBin/Program.cs | 79 +++++++++++++++++++++++++++++++++----- PasteBin/assets/index.html | 4 +- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index a637fab..fbd5a22 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -4,6 +4,7 @@ using System.Threading.RateLimiting; using Newtonsoft.Json.Linq; using System.Data; using System.Runtime.InteropServices; +using System.Globalization; class Program { @@ -120,6 +121,14 @@ class Program context.Response.StatusCode = 404; return; } + if (paste.Title == "" || string.IsNullOrEmpty(paste.Title)) + { + paste.Title = $"Untitled {reader.GetString(0)}"; + } + if (paste.Title.Length > 40) + { + paste.Title = paste.Title.Substring(0, 50) + "..."; + } var html = File.ReadAllText("assets\\paste.html"); html = html.Replace("{pastetitle}", paste.Title); html = html.Replace("{content}", content); @@ -170,7 +179,15 @@ class Program { paste.Title = $"Untitled {reader.GetString(0)}"; } - pastes += $"
  • {paste.Title} {paste.Date} {ConvertToBytes(paste.Size)}
  • "; + if (paste.Title.Length > 40) + { + paste.Title = paste.Title.Substring(0, 50) + "..."; + } + DateTime date = DateTime.ParseExact(paste.Date, "dd/MM/yyyy-HH:mm:ss", CultureInfo.InvariantCulture); + DateTime now = DateTime.Now; + var difference = GetDifference(date, now); + + pastes += $"
  • {paste.Title} {difference} {ConvertToBytes(paste.Size)}
  • "; } if(string.IsNullOrEmpty(pastes)) { @@ -181,7 +198,7 @@ class Program await context.Response.WriteAsync(html); return; } - }).RequireRateLimiting("fixed"); + }).RequireRateLimiting("fixed-bigger"); app.MapGet("/archive", async (HttpContext context) => { var html = File.ReadAllText("assets\\archives.html"); @@ -220,7 +237,14 @@ class Program { paste.Title = $"Untitled {reader.GetString(0)}"; } - pastes += $"
  • {paste.Title} {paste.Date} {ConvertToBytes(paste.Size)}
  • "; + if (paste.Title.Length > 40) + { + paste.Title = paste.Title.Substring(0, 50) + "..."; + } + DateTime date = DateTime.ParseExact(paste.Date, "dd/MM/yyyy-HH:mm:ss", CultureInfo.InvariantCulture); + DateTime now = DateTime.Now; + var difference = GetDifference(date, now); + pastes += $"
  • {paste.Title} {difference} {ConvertToBytes(paste.Size)}
  • "; } var findCountCommand = connection.CreateCommand(); findCountCommand.CommandText = "SELECT COUNT(*) FROM pastes WHERE Visibility != 'Private'"; @@ -259,11 +283,11 @@ class Program app.MapPost("/api/create", async (HttpContext context) => { string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); - if (requestBody.Length > 500_000 || string.IsNullOrEmpty(requestBody)) + if (string.IsNullOrEmpty(requestBody)) { 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"] = "No content uploaded" }.ToString()); return; } var data = JObject.Parse(requestBody); @@ -271,6 +295,14 @@ class Program var content = SanitizeInput(data["content"]?.ToString() ?? ""); var visibility = SanitizeInput(data["visibility"]?.ToString() ?? "Public"); + if(content.Length > 500_000 || title.Length > 500) + { + context.Response.StatusCode = 400; + context.Response.ContentType = "application/json"; + await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Content or Title is too long" }.ToString()); + return; + } + if (visibility != "Public" && visibility != "Unlisted" && visibility != "Private") { context.Response.StatusCode = 400; @@ -355,10 +387,10 @@ class Program double.Parse(length); } catch - { - return "0 B"; + { + return "0 B"; } - string[] sizes = { "B", "KB", "MB", "GB", "TB" }; + string[] sizes = { "Bytes", "KB", "MB", "GB", "TB" }; double len = double.Parse(length); int order = 0; while (len >= 1024 && order < sizes.Length - 1) @@ -366,8 +398,37 @@ class Program order++; len = len / 1024; } - return $"{len:0.##} {sizes[order]}"; + if (sizes[order] == "Bytes") + { + return $"{len:0.##} {sizes[order]}"; + } + else + { + return $"{len:0.##}{sizes[order]}"; + } } + public static string GetDifference(DateTime startDate, DateTime endDate) + { + TimeSpan span = endDate.Subtract(startDate); + + if (span.TotalSeconds < 60) + { + return $"{Math.Round(span.TotalSeconds, 0)} sec ago"; + } + else if (span.TotalMinutes < 60) + { + return $"{Math.Round(span.TotalMinutes, 0)} min"; + } + else if (span.TotalHours < 24) + { + return $"{Math.Round(span.TotalHours,1)} hrs ago"; + } + else + { + return $"{Math.Round(span.TotalDays,1)} days ago"; + } + } + public static string GenerateRandomString(int length) { var random = new Random(); diff --git a/PasteBin/assets/index.html b/PasteBin/assets/index.html index 8a75f0f..4a68395 100644 --- a/PasteBin/assets/index.html +++ b/PasteBin/assets/index.html @@ -17,9 +17,9 @@

    create paste

    - + - +