From 9f55aa1b2273ee0298892dba380c71630c743289 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Thu, 8 Jan 2026 15:25:42 -0500 Subject: [PATCH] feat: paste compression & verification enforcement settings; enforce 2fa on admins setting; increase paste validation; anonymous uploads fix: minor styling issue on main elements; resolve lint issue refactor: remove 2FA badge on user pages; enhance paste creation flow; body background color always neutral-950; improve paste creation paste info (lines, cchars etc) for mobile --- SharpbinV3.Client/src/lib/utils/misc.ts | 16 +- SharpbinV3.Client/src/routes/+layout.svelte | 2 +- SharpbinV3.Client/src/routes/+page.server.ts | 8 +- SharpbinV3.Client/src/routes/+page.svelte | 146 ++++++++++++++---- .../src/routes/[id]/+page.svelte | 2 +- SharpbinV3.Client/src/routes/layout.css | 2 +- .../src/routes/login/+page.svelte | 2 +- .../src/routes/register/+page.svelte | 2 +- .../src/routes/user/[username]/+page.svelte | 11 +- .../Controllers/AuthController.cs | 2 +- .../Controllers/PasteController.cs | 38 ++++- .../Controllers/UserController.cs | 12 +- SharpbinV3.Server/Services/PasteService.cs | 13 +- SharpbinV3.Server/Settings/AuthSettings.cs | 1 + SharpbinV3.Server/Settings/JWTSettings.cs | 2 +- SharpbinV3.Server/Settings/PasteSettings.cs | 2 + 16 files changed, 186 insertions(+), 75 deletions(-) diff --git a/SharpbinV3.Client/src/lib/utils/misc.ts b/SharpbinV3.Client/src/lib/utils/misc.ts index 4f56eed..3b851e0 100644 --- a/SharpbinV3.Client/src/lib/utils/misc.ts +++ b/SharpbinV3.Client/src/lib/utils/misc.ts @@ -1,16 +1,16 @@ export function formatBytes(bytes: number, decimals = 2): string { - if (bytes === 0) return '0 Bytes'; + if (bytes === 0) return '0 Bytes'; - const unit = 1024; - const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + const unit = 1024; + const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const exponent = Math.floor(Math.log(bytes) / Math.log(unit)); - const value = bytes / Math.pow(unit, exponent); + const exponent = Math.floor(Math.log(bytes) / Math.log(unit)); + const value = bytes / Math.pow(unit, exponent); - const unitLabel = value === 1 && exponent === 0 ? 'Byte' : units[exponent]; - const formattedValue = Number.isInteger(value) ? value : value.toFixed(decimals); + const unitLabel = value === 1 && exponent === 0 ? 'Byte' : units[exponent]; + const formattedValue = Number.isInteger(value) ? value : value.toFixed(decimals); - return `${formattedValue} ${unitLabel}`; + return `${formattedValue} ${unitLabel}`; } export function formatNumber(num: number): string { diff --git a/SharpbinV3.Client/src/routes/+layout.svelte b/SharpbinV3.Client/src/routes/+layout.svelte index f00ab80..0cddc84 100644 --- a/SharpbinV3.Client/src/routes/+layout.svelte +++ b/SharpbinV3.Client/src/routes/+layout.svelte @@ -38,7 +38,7 @@ - {#if data.options?.cf_turnstile_site_key && (page.url.pathname === '/login' || page.url.pathname === '/register')} + {#if data.options?.cf_turnstile_site_key && (page.url.pathname === '/login' || page.url.pathname === '/register' || page.url.pathname === '/')}

Login

diff --git a/SharpbinV3.Client/src/routes/register/+page.svelte b/SharpbinV3.Client/src/routes/register/+page.svelte index d2fda59..97d7aea 100644 --- a/SharpbinV3.Client/src/routes/register/+page.svelte +++ b/SharpbinV3.Client/src/routes/register/+page.svelte @@ -113,7 +113,7 @@

Register

diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte index 79646eb..577f156 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte @@ -204,7 +204,7 @@

@@ -259,12 +259,6 @@ {data.user?.uid}

- {#if isOwner} -
- - 2FA {totpEnabled ? 'enabled' : 'disabled'} -
- {/if} {#if isOwner && data.user?.lastLogin}
@@ -433,9 +427,6 @@ show={showTotpModal} mode="totpSetup" title="Two-Factor Authentication" - message={totpEnabled - ? 'Two-factor authentication is currently enabled.' - : 'Enable two-factor authentication with an authenticator app.'} error={modalError} totpActive={totpEnabled} onConfirm={(value) => handleTotpConfirm(value)} diff --git a/SharpbinV3.Server/Controllers/AuthController.cs b/SharpbinV3.Server/Controllers/AuthController.cs index c1b5de2..b4dabf7 100644 --- a/SharpbinV3.Server/Controllers/AuthController.cs +++ b/SharpbinV3.Server/Controllers/AuthController.cs @@ -19,7 +19,7 @@ namespace SharpbinV3.Server.Controllers { [ApiController] [Route("api/[controller]")] - public class AuthController(UserService userService, AuthService authService, IOptions options, + public class AuthController(UserService userService, AuthService authService, IOptions options, VerificationService verification, TotpVerificationProvider totp, AppDbContext db) : ControllerBase { private readonly AuthService _authService = authService; diff --git a/SharpbinV3.Server/Controllers/PasteController.cs b/SharpbinV3.Server/Controllers/PasteController.cs index 3408d1d..e658697 100644 --- a/SharpbinV3.Server/Controllers/PasteController.cs +++ b/SharpbinV3.Server/Controllers/PasteController.cs @@ -5,23 +5,25 @@ using SharpbinV3.Server.Data.Entities; using SharpbinV3.Server.DTOs.Paste; using SharpbinV3.Server.Extensions; using SharpbinV3.Server.Services; +using SharpbinV3.Server.Services.Verification; using SharpbinV3.Server.Settings; namespace SharpbinV3.Server.Controllers { [Route("api/[controller]")] [ApiController] - public class PasteController(PasteService pasteService, UserService userService, AuthService authService, IOptions options) : ControllerBase + public class PasteController(PasteService pasteService, VerificationService verificationService, AuthService authService, IOptions options, IOptions authSettings) : ControllerBase { private readonly PasteService _pasteService = pasteService; - private readonly UserService _userService = userService; + private readonly VerificationService _verificationService = verificationService; private readonly AuthService _authService = authService; private readonly PasteSettings _pasteSettings = options.Value; + private readonly AuthSettings _authSettings = authSettings.Value; [HttpPost] [Route("create")] [Authorize(Policy = "NotBanned")] - public async Task CreatePaste(string title = "", string syntax = "plaintext", int visibility = 0, long expiresAt = 0) + public async Task CreatePaste(string title = "", string syntax = "plaintext", int visibility = 0, long expiresAt = 0, string? token = null) { if (!await _pasteService.ValidateExpiresAt(expiresAt)) return BadRequest(new { message = "Invalid expiration time." }); @@ -33,10 +35,20 @@ namespace SharpbinV3.Server.Controllers return BadRequest(new { message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." }); string content = await new StreamReader(Request.Body).ReadToEndAsync(); + if (content.Length == 0) + return BadRequest(new { message = "Paste content cannot be empty." }); if (System.Text.Encoding.UTF8.GetByteCount(content) > _pasteSettings.MaxPasteSizeInBytes) return BadRequest(new { message = $"Paste size exceeds the maximum allowed size of {_pasteSettings.MaxPasteSizeInBytes} bytes." }); + + if (_pasteSettings.RequiresVerfication && !await _verificationService.VerifyAsync(new VerificationContext + { + Token = token, + Ip = HttpContext.GetRequestIP() + })) + return BadRequest(new { message = "Verification failed." }); + var user = await _authService.GetUserFromHttpContext(HttpContext); - Paste paste = await _pasteService.Create(user, content, title, syntax, visibility, expiresAt); + Paste paste = await _pasteService.Create(user, content, title, syntax, visibility, expiresAt, _pasteSettings.EnablePasteCompression); return Ok(new { paste.ID, @@ -102,11 +114,16 @@ namespace SharpbinV3.Server.Controllers if (content == null || id == null) return NotFound(); var paste = await _pasteService.Get(id); if (paste == null) return NotFound(); + var user = HttpContext.GetJwtUser(); if (user == null) return Forbid(); + var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255); if (paste.AuthorUUID != user.UUID && !hasPrivilegedRole) return Forbid(); + if (user.Roles.Contains(255) && !user.TotpEnabled && _authSettings.Admins_Require_2FA) + return Forbid(); + bool result = await _pasteService.EditText(paste, content); if (!result) return NotFound(); return Ok(new { message = "Paste edited successfully." }); @@ -119,11 +136,16 @@ namespace SharpbinV3.Server.Controllers { var paste = await _pasteService.Get(id); if (paste == null) return NotFound(); + var user = HttpContext.GetJwtUser(); if (user == null) return Forbid(); + var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255); + if (paste.AuthorUUID != user.UUID && !hasPrivilegedRole) return Forbid(); + if (user.Roles.Contains(255) && !user.TotpEnabled && _authSettings.Admins_Require_2FA) + return Forbid(); if (request.Title != null) paste.Title = request.Title; if (request.Syntax != null) @@ -168,11 +190,16 @@ namespace SharpbinV3.Server.Controllers { var paste = await _pasteService.Get(id); if (paste == null) return NotFound(); + var user = HttpContext.GetJwtUser(); if (user == null) return Forbid(); + var hasPrivilegedRole = user.Roles.Any(r => r == 1 || r == 255); if (paste.AuthorUUID != user.UUID && !hasPrivilegedRole) return Forbid(); + if (user.Roles.Contains(255) && !user.TotpEnabled && _authSettings.Admins_Require_2FA) + return Forbid(); + bool result = await _pasteService.Delete(paste); if (!result) return NotFound(); return Ok(new { message = "Paste deleted successfully." }); @@ -192,7 +219,8 @@ namespace SharpbinV3.Server.Controllers new { value = 2, displayName = "Private" } }, maxTitleLength = _pasteSettings.MaxTitleLength, - maxPasteSize = _pasteSettings.MaxPasteSizeInBytes + maxPasteSize = _pasteSettings.MaxPasteSizeInBytes, + requiresVerification = _pasteSettings.RequiresVerfication }; return Ok(options); } diff --git a/SharpbinV3.Server/Controllers/UserController.cs b/SharpbinV3.Server/Controllers/UserController.cs index 3759594..b3fc85a 100644 --- a/SharpbinV3.Server/Controllers/UserController.cs +++ b/SharpbinV3.Server/Controllers/UserController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; using SharpbinV3.Server.Data; using SharpbinV3.Server.Data.Entities; using SharpbinV3.Server.DTOs; @@ -9,18 +10,19 @@ using SharpbinV3.Server.Extensions; using SharpbinV3.Server.Services; using SharpbinV3.Server.Services.Verification; using SharpbinV3.Server.Services.Verification.Providers; +using SharpbinV3.Server.Settings; namespace SharpbinV3.Server.Controllers { [ApiController] [Route("api/[controller]")] - public class UserController(UserService userService, AppDbContext db, AuthService authService, TotpVerificationProvider totp, VerificationService verificationService) : ControllerBase + public class UserController(UserService userService, AppDbContext db, TotpVerificationProvider totp, VerificationService verificationService, IOptions authSettings) : ControllerBase { private readonly UserService _userService = userService; private readonly AppDbContext _db = db; - private readonly AuthService _authService = authService; private readonly TotpVerificationProvider _totp = totp; private readonly VerificationService _verificationService = verificationService; + private readonly AuthSettings _authSettings = authSettings.Value; [HttpGet("{username}")] public async Task GetByUsername(string username, [FromQuery] int page = 1) @@ -63,6 +65,8 @@ namespace SharpbinV3.Server.Controllers return Forbid(); if (user.Roles.Contains(255) && !jwtUser.Roles.Contains(255)) return Forbid(); + if (jwtUser.Roles.Contains(255) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA) + return Forbid(); user.DisplayName = updatedUser.DisplayName ?? user.DisplayName; if (jwtUser.Roles.Any(r => r == 255) || user.UUID == jwtUser.UUID) // only admins or self @@ -100,6 +104,8 @@ namespace SharpbinV3.Server.Controllers if (user == null) return Forbid(); if (!jwtUser.Roles.Any(r => r == 255) && user.UUID != jwtUser.UUID) return Forbid(); + if (jwtUser.Roles.Contains(255) && !jwtUser.TotpEnabled && _authSettings.Admins_Require_2FA) + return Forbid(); if (jwtUser.TotpEnabled) { @@ -123,7 +129,7 @@ namespace SharpbinV3.Server.Controllers return Unauthorized(new { message = "Invalid verification token." }); } } - + _db.Users.Remove(user); await _db.SaveChangesAsync(); return Ok(new { message = "User deleted successfully." }); diff --git a/SharpbinV3.Server/Services/PasteService.cs b/SharpbinV3.Server/Services/PasteService.cs index 2c5bdb5..8e03f5e 100644 --- a/SharpbinV3.Server/Services/PasteService.cs +++ b/SharpbinV3.Server/Services/PasteService.cs @@ -13,9 +13,12 @@ namespace SharpbinV3.Server.Services private readonly ICompressionService _cs = cs; private readonly PasteSettings _pasteSettings = options.Value; - public async Task Create(User? author, string content, string title, string syntax, int visibility, long expiresAt) + public async Task Create(User? author, string content, string title, string syntax, int visibility, long expiresAt, bool shouldCompress) { - var compressedData = _cs.Compress(content); + var data = Encoding.UTF8.GetBytes(content); + if (shouldCompress) + data = _cs.Compress(content); + var paste = new Paste { UUID = Guid.CreateVersion7(), @@ -23,10 +26,10 @@ namespace SharpbinV3.Server.Services Title = title, AuthorUUID = author?.UUID, User = author, - Content = compressedData, - Size = compressedData.Length, + Content = data, + Size = data.Length, TrueSize = Encoding.UTF8.GetByteCount(content), - IsCompressed = compressedData.Length < Encoding.UTF8.GetByteCount(content), + IsCompressed = data.Length < Encoding.UTF8.GetByteCount(content), Syntax = syntax, Visibility = visibility, ExpiresAt = expiresAt, diff --git a/SharpbinV3.Server/Settings/AuthSettings.cs b/SharpbinV3.Server/Settings/AuthSettings.cs index f5fbe2b..56f28c6 100644 --- a/SharpbinV3.Server/Settings/AuthSettings.cs +++ b/SharpbinV3.Server/Settings/AuthSettings.cs @@ -6,5 +6,6 @@ public string CF_Turnstile_SecretKey { get; set; } = string.Empty; public bool Registration_Enabled { get; set; } = true; public bool First_User_Admin { get; set; } = true; + public bool Admins_Require_2FA { get; set; } = true; } } diff --git a/SharpbinV3.Server/Settings/JWTSettings.cs b/SharpbinV3.Server/Settings/JWTSettings.cs index 2da3720..965d6bf 100644 --- a/SharpbinV3.Server/Settings/JWTSettings.cs +++ b/SharpbinV3.Server/Settings/JWTSettings.cs @@ -2,7 +2,7 @@ { public class JWTSettings { - public string Secret { get; set; } = Utilities.GenerateRandomString(64); + public string Secret { get; set; } = string.Empty; public string Issuer { get; set; } = "SharpbinApi"; public string Audience { get; set; } = "SharpbinClient"; } diff --git a/SharpbinV3.Server/Settings/PasteSettings.cs b/SharpbinV3.Server/Settings/PasteSettings.cs index 045c647..e36780d 100644 --- a/SharpbinV3.Server/Settings/PasteSettings.cs +++ b/SharpbinV3.Server/Settings/PasteSettings.cs @@ -5,5 +5,7 @@ public string[] ValidSyntaxLanguages { get; set; } = []; public int MaxTitleLength { get; set; } = 500; public int MaxPasteSizeInBytes { get; set; } = 1_048_576; // 1 MB + public bool EnablePasteCompression { get; set; } = true; + public bool RequiresVerfication { get; set; } = true; } }