From 6a7b8653dc154366c85de32dccf242a071eebdf0 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Wed, 17 Jun 2026 21:43:41 -0400 Subject: [PATCH] fix: multiple vulnerabilities --- SharpbinV3.Client/src/lib/server/proxy.ts | 28 +++++++++++++++++++ .../src/routes/[id]/+page.svelte | 2 +- .../src/routes/api/[...path]/+server.ts | 13 +++++++++ .../src/routes/openapi/+server.ts | 6 ++++ .../src/routes/openapi/[...path]/+server.ts | 6 ++++ .../Controllers/PasteController.cs | 4 +-- .../Controllers/UserController.cs | 5 +--- SharpbinV3.Server/Program.cs | 4 ++- SharpbinV3.Server/Services/ApiKeyService.cs | 2 +- SharpbinV3.Server/Services/AuthService.cs | 26 ++++++++--------- SharpbinV3.Server/Settings/AppSettings.cs | 1 + SharpbinV3.Server/appsettings.json | 3 ++ backend.env.example | 4 ++- docker-compose.yml | 3 +- frontend.env.example | 3 +- 15 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 SharpbinV3.Client/src/lib/server/proxy.ts create mode 100644 SharpbinV3.Client/src/routes/api/[...path]/+server.ts create mode 100644 SharpbinV3.Client/src/routes/openapi/+server.ts create mode 100644 SharpbinV3.Client/src/routes/openapi/[...path]/+server.ts diff --git a/SharpbinV3.Client/src/lib/server/proxy.ts b/SharpbinV3.Client/src/lib/server/proxy.ts new file mode 100644 index 0000000..6f10f66 --- /dev/null +++ b/SharpbinV3.Client/src/lib/server/proxy.ts @@ -0,0 +1,28 @@ +import { apiUrl } from '$lib/server/api'; + +const methodsWithoutBody = new Set(['GET', 'HEAD']); + +export async function proxyBackendRequest( + prefix: string, + path: string, + request: Request, + url: URL +) { + const targetUrl = new URL(`${prefix}/${path}`.replace(/\/+$/, ''), apiUrl); + targetUrl.search = url.search; + + const headers = new Headers(request.headers); + headers.delete('host'); + + const init: RequestInit = { + method: request.method, + headers, + redirect: 'manual' + }; + + if (!methodsWithoutBody.has(request.method)) { + init.body = await request.arrayBuffer(); + } + + return fetch(targetUrl, init); +} diff --git a/SharpbinV3.Client/src/routes/[id]/+page.svelte b/SharpbinV3.Client/src/routes/[id]/+page.svelte index a17851c..814bdfc 100644 --- a/SharpbinV3.Client/src/routes/[id]/+page.svelte +++ b/SharpbinV3.Client/src/routes/[id]/+page.svelte @@ -1094,7 +1094,7 @@ class={data.paste.syntax === 'markdown' ? 'markdown rounded-b-md bg-neutral-800 p-4' : data.paste.syntax === 'csv' - ? 'csv-container scrollbar overflow-x-auto overflow-y-auto scrollbar-thumb-neutral-600 scrollbar-track-neutral-800' + ? 'csv-container scrollbar scrollbar-thumb-neutral-600 scrollbar-track-neutral-800 overflow-x-auto overflow-y-auto' : 'codeblock-with-lines overflow-x-auto overflow-y-auto'} class:hidden={!contentRendered} onclick={handleMarkdownClick} diff --git a/SharpbinV3.Client/src/routes/api/[...path]/+server.ts b/SharpbinV3.Client/src/routes/api/[...path]/+server.ts new file mode 100644 index 0000000..c3e61da --- /dev/null +++ b/SharpbinV3.Client/src/routes/api/[...path]/+server.ts @@ -0,0 +1,13 @@ +import { proxyBackendRequest } from '$lib/server/proxy'; +import type { RequestHandler } from './$types'; + +const handle: RequestHandler = ({ params, request, url }) => { + return proxyBackendRequest('/api', params.path, request, url); +}; + +export const GET = handle; +export const POST = handle; +export const PUT = handle; +export const PATCH = handle; +export const DELETE = handle; +export const OPTIONS = handle; diff --git a/SharpbinV3.Client/src/routes/openapi/+server.ts b/SharpbinV3.Client/src/routes/openapi/+server.ts new file mode 100644 index 0000000..16d5c4f --- /dev/null +++ b/SharpbinV3.Client/src/routes/openapi/+server.ts @@ -0,0 +1,6 @@ +import { proxyBackendRequest } from '$lib/server/proxy'; +import type { RequestHandler } from './$types'; + +export const GET: RequestHandler = ({ request, url }) => { + return proxyBackendRequest('/openapi', '', request, url); +}; diff --git a/SharpbinV3.Client/src/routes/openapi/[...path]/+server.ts b/SharpbinV3.Client/src/routes/openapi/[...path]/+server.ts new file mode 100644 index 0000000..0c1a359 --- /dev/null +++ b/SharpbinV3.Client/src/routes/openapi/[...path]/+server.ts @@ -0,0 +1,6 @@ +import { proxyBackendRequest } from '$lib/server/proxy'; +import type { RequestHandler } from './$types'; + +export const GET: RequestHandler = ({ params, request, url }) => { + return proxyBackendRequest('/openapi', params.path, request, url); +}; diff --git a/SharpbinV3.Server/Controllers/PasteController.cs b/SharpbinV3.Server/Controllers/PasteController.cs index 789c5b1..28439a5 100644 --- a/SharpbinV3.Server/Controllers/PasteController.cs +++ b/SharpbinV3.Server/Controllers/PasteController.cs @@ -294,9 +294,9 @@ namespace SharpbinV3.Server.Controllers ) return Unauthorized(new { success = false, message = "Invalid API key." }); - if (paste.AuthorUUID != HttpContext.GetJwtUser()?.UUID) + if (paste.AuthorUUID == HttpContext.GetJwtUser()?.UUID) return Ok(new { success = true, message = "View not recorded for author's own paste." }); - if (paste.ExpiresAt < DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() && paste.Visibility != 0) + if (paste.ExpiresAt != 0 && paste.ExpiresAt < DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() && paste.Visibility != 0) return Ok(new { success = true, message = "View not recorded for expired paste." }); var result = await _pasteService.RecordView(paste, HttpContext); diff --git a/SharpbinV3.Server/Controllers/UserController.cs b/SharpbinV3.Server/Controllers/UserController.cs index 6e7ad98..caef0c3 100644 --- a/SharpbinV3.Server/Controllers/UserController.cs +++ b/SharpbinV3.Server/Controllers/UserController.cs @@ -124,10 +124,7 @@ namespace SharpbinV3.Server.Controllers var actorIsAdmin = actorUser.Roles.HasFlag(Role.Admin); var isSelfUpdate = targetUser.UUID == actorUser.UUID; - if ( - (targetUser.Roles.HasFlag(Role.Admin) && !actorIsAdmin) - || (!actorIsAdmin && !isSelfUpdate) - ) + if ((targetUser.Roles.HasFlag(Role.Admin) && !actorIsAdmin) || (!actorIsAdmin && !isSelfUpdate)) return StatusCode(403, new { success = false, message = "You do not have permission to modify this user." }); if ((updatedUser.Roles.HasValue || updatedUser.IsBanned.HasValue) && !actorIsAdmin) diff --git a/SharpbinV3.Server/Program.cs b/SharpbinV3.Server/Program.cs index 74d0bd0..1bebb24 100644 --- a/SharpbinV3.Server/Program.cs +++ b/SharpbinV3.Server/Program.cs @@ -276,7 +276,9 @@ namespace SharpbinV3.Server builder.Services.AddCors(o => { - o.AddDefaultPolicy(p => p.WithOrigins("http://localhost:5173").AllowAnyHeader().AllowAnyMethod()); + var allowedOrigins = + builder.Configuration.GetSection(nameof(AppSettings.AllowedCorsOrigins)).Get() ?? ["http://localhost:5173"]; + o.AddDefaultPolicy(p => p.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod()); }); builder diff --git a/SharpbinV3.Server/Services/ApiKeyService.cs b/SharpbinV3.Server/Services/ApiKeyService.cs index b3ee93b..fa9bec8 100644 --- a/SharpbinV3.Server/Services/ApiKeyService.cs +++ b/SharpbinV3.Server/Services/ApiKeyService.cs @@ -18,7 +18,7 @@ namespace SharpbinV3.Server.Services if (string.IsNullOrEmpty(_authSettings.API_Key_HMAC_Secret)) return Utilities.ComputeSha256(input); else - return Utilities.ComputeHmacSha256(input, _authSettings.API_Key_HMAC_Secret); + return Utilities.ComputeHmacSha256(_authSettings.API_Key_HMAC_Secret, input); } public async Task GetByKeyAsync(string key) diff --git a/SharpbinV3.Server/Services/AuthService.cs b/SharpbinV3.Server/Services/AuthService.cs index cf0318b..ca71382 100644 --- a/SharpbinV3.Server/Services/AuthService.cs +++ b/SharpbinV3.Server/Services/AuthService.cs @@ -141,23 +141,21 @@ namespace SharpbinV3.Server.Services var jti = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Jti).Value; var userUUID = principal.Claims.First(c => c.Type == "uuid").Value; + var userGuid = Guid.Parse(userUUID); var tokenHash = Utilities.ComputeSha256(refreshToken); - var storedToken = await _db.RefreshTokens.FirstOrDefaultAsync(rt => rt.TokenHash == tokenHash); + await using var transaction = await _db.Database.BeginTransactionAsync(); + var now = DateTimeOffset.UtcNow; + var revokedCount = await _db + .RefreshTokens.Where(rt => + rt.TokenHash == tokenHash && !rt.Used && !rt.Revoked && rt.ExpiresAt >= now && rt.JwtId == jti && rt.UserUUID == userGuid + ) + .ExecuteUpdateAsync(setters => setters.SetProperty(rt => rt.Used, true).SetProperty(rt => rt.Revoked, true)); - if ( - storedToken == null - || storedToken.Used - || storedToken.Revoked - || storedToken.ExpiresAt < DateTimeOffset.UtcNow - || storedToken.JwtId != jti - || storedToken.UserUUID != Guid.Parse(userUUID) - ) - { + if (revokedCount != 1) return new CreateJWT { Success = false, Errors = ["Invalid or expired refresh token."] }; - } - var user = await _db.Users.FirstOrDefaultAsync(u => u.UUID == Guid.Parse(userUUID)); + var user = await _db.Users.FirstOrDefaultAsync(u => u.UUID == userGuid); if (user == null) return new CreateJWT { Success = false, Errors = ["Invalid token."] }; @@ -165,9 +163,7 @@ namespace SharpbinV3.Server.Services if (!newTokenResult.Success) return newTokenResult; - storedToken.Used = true; - storedToken.Revoked = true; - await _db.SaveChangesAsync(); + await transaction.CommitAsync(); return newTokenResult; } diff --git a/SharpbinV3.Server/Settings/AppSettings.cs b/SharpbinV3.Server/Settings/AppSettings.cs index 176be9b..eec34d3 100644 --- a/SharpbinV3.Server/Settings/AppSettings.cs +++ b/SharpbinV3.Server/Settings/AppSettings.cs @@ -7,5 +7,6 @@ namespace SharpbinV3.Server.Settings [Required, MaxLength(256)] public string Domain { get; set; } = string.Empty; public bool Https { get; set; } = true; + public string[] AllowedCorsOrigins { get; set; } = ["http://localhost:5173"]; } } diff --git a/SharpbinV3.Server/appsettings.json b/SharpbinV3.Server/appsettings.json index 4688df4..9adaa23 100644 --- a/SharpbinV3.Server/appsettings.json +++ b/SharpbinV3.Server/appsettings.json @@ -9,6 +9,9 @@ "DefaultConnection": "Data Source=sharpbin.db" }, "AllowedHosts": "*", + "AllowedCorsOrigins": [ + "http://localhost:5173" + ], "JwtSettings": { "Secret": "", "Issuer": "SharpbinApi", diff --git a/backend.env.example b/backend.env.example index 887aac5..7fccfa4 100644 --- a/backend.env.example +++ b/backend.env.example @@ -1,7 +1,9 @@ # Required Misc. Variables Domain=example.com Https=true +AllowedCorsOrigins__0=https://example.com JwtSettings__Secret=totally_random_and_secure_string +ConnectionStrings__DefaultConnection=Data Source=/app/data/sharpbin.db # Auth Settings AuthSettings__CF_Turnstile_SecretKey=1x0000000000000000000000000000000AA @@ -18,4 +20,4 @@ PasteSettings__RequiresVerification=true PasteSettings__View_HMAC_Secret=random_minimum_32_char_string # Used so that views are only counted from the frontend # Must be the same on both the frontend and backend environment -PasteSettings__View_Internal_API_Key=another_random_minimum_32_char_string \ No newline at end of file +PasteSettings__View_Internal_API_Key=another_random_minimum_32_char_string diff --git a/docker-compose.yml b/docker-compose.yml index 11ad215..a92aca3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - "backend.env" environment: DATA_PROTECTION_KEY_PATH: /app/data/keys + ConnectionStrings__DefaultConnection: "Data Source=/app/data/sharpbin.db" volumes: - sharpbin-data:/app/data - ./SharpbinV3.Server/appsettings.json:/app/appsettings.json @@ -53,4 +54,4 @@ volumes: networks: sharpbin-net: - driver: bridge \ No newline at end of file + driver: bridge diff --git a/frontend.env.example b/frontend.env.example index 85a8206..4198e0f 100644 --- a/frontend.env.example +++ b/frontend.env.example @@ -1,5 +1,6 @@ +VITE_API_URL=http://backend:8080 VITE_ALLOWED_HOSTS=localhost,example.com # Used so that views are only counted from the frontend # Must be the same on both the frontend and backend environment # The frontend will also try to parse 'PasteSettings__View_Internal_API_Key' -VIEW_INTERNAL_API_KEY=another_random_minimum_32_char_string \ No newline at end of file +VIEW_INTERNAL_API_KEY=another_random_minimum_32_char_string