mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: multiple vulnerabilities
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
@@ -1094,7 +1094,7 @@
|
|||||||
class={data.paste.syntax === 'markdown'
|
class={data.paste.syntax === 'markdown'
|
||||||
? 'markdown rounded-b-md bg-neutral-800 p-4'
|
? 'markdown rounded-b-md bg-neutral-800 p-4'
|
||||||
: data.paste.syntax === 'csv'
|
: 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'}
|
: 'codeblock-with-lines overflow-x-auto overflow-y-auto'}
|
||||||
class:hidden={!contentRendered}
|
class:hidden={!contentRendered}
|
||||||
onclick={handleMarkdownClick}
|
onclick={handleMarkdownClick}
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -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);
|
||||||
|
};
|
||||||
@@ -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);
|
||||||
|
};
|
||||||
@@ -294,9 +294,9 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
)
|
)
|
||||||
return Unauthorized(new { success = false, message = "Invalid API key." });
|
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." });
|
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." });
|
return Ok(new { success = true, message = "View not recorded for expired paste." });
|
||||||
|
|
||||||
var result = await _pasteService.RecordView(paste, HttpContext);
|
var result = await _pasteService.RecordView(paste, HttpContext);
|
||||||
|
|||||||
@@ -124,10 +124,7 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
var actorIsAdmin = actorUser.Roles.HasFlag(Role.Admin);
|
var actorIsAdmin = actorUser.Roles.HasFlag(Role.Admin);
|
||||||
var isSelfUpdate = targetUser.UUID == actorUser.UUID;
|
var isSelfUpdate = targetUser.UUID == actorUser.UUID;
|
||||||
|
|
||||||
if (
|
if ((targetUser.Roles.HasFlag(Role.Admin) && !actorIsAdmin) || (!actorIsAdmin && !isSelfUpdate))
|
||||||
(targetUser.Roles.HasFlag(Role.Admin) && !actorIsAdmin)
|
|
||||||
|| (!actorIsAdmin && !isSelfUpdate)
|
|
||||||
)
|
|
||||||
return StatusCode(403, new { success = false, message = "You do not have permission to modify this user." });
|
return StatusCode(403, new { success = false, message = "You do not have permission to modify this user." });
|
||||||
|
|
||||||
if ((updatedUser.Roles.HasValue || updatedUser.IsBanned.HasValue) && !actorIsAdmin)
|
if ((updatedUser.Roles.HasValue || updatedUser.IsBanned.HasValue) && !actorIsAdmin)
|
||||||
|
|||||||
@@ -276,7 +276,9 @@ namespace SharpbinV3.Server
|
|||||||
|
|
||||||
builder.Services.AddCors(o =>
|
builder.Services.AddCors(o =>
|
||||||
{
|
{
|
||||||
o.AddDefaultPolicy(p => p.WithOrigins("http://localhost:5173").AllowAnyHeader().AllowAnyMethod());
|
var allowedOrigins =
|
||||||
|
builder.Configuration.GetSection(nameof(AppSettings.AllowedCorsOrigins)).Get<string[]>() ?? ["http://localhost:5173"];
|
||||||
|
o.AddDefaultPolicy(p => p.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod());
|
||||||
});
|
});
|
||||||
|
|
||||||
builder
|
builder
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace SharpbinV3.Server.Services
|
|||||||
if (string.IsNullOrEmpty(_authSettings.API_Key_HMAC_Secret))
|
if (string.IsNullOrEmpty(_authSettings.API_Key_HMAC_Secret))
|
||||||
return Utilities.ComputeSha256(input);
|
return Utilities.ComputeSha256(input);
|
||||||
else
|
else
|
||||||
return Utilities.ComputeHmacSha256(input, _authSettings.API_Key_HMAC_Secret);
|
return Utilities.ComputeHmacSha256(_authSettings.API_Key_HMAC_Secret, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ApiKey?> GetByKeyAsync(string key)
|
public async Task<ApiKey?> GetByKeyAsync(string key)
|
||||||
|
|||||||
@@ -141,23 +141,21 @@ namespace SharpbinV3.Server.Services
|
|||||||
|
|
||||||
var jti = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Jti).Value;
|
var jti = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Jti).Value;
|
||||||
var userUUID = principal.Claims.First(c => c.Type == "uuid").Value;
|
var userUUID = principal.Claims.First(c => c.Type == "uuid").Value;
|
||||||
|
var userGuid = Guid.Parse(userUUID);
|
||||||
|
|
||||||
var tokenHash = Utilities.ComputeSha256(refreshToken);
|
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;
|
||||||
if (
|
var revokedCount = await _db
|
||||||
storedToken == null
|
.RefreshTokens.Where(rt =>
|
||||||
|| storedToken.Used
|
rt.TokenHash == tokenHash && !rt.Used && !rt.Revoked && rt.ExpiresAt >= now && rt.JwtId == jti && rt.UserUUID == userGuid
|
||||||
|| storedToken.Revoked
|
|
||||||
|| storedToken.ExpiresAt < DateTimeOffset.UtcNow
|
|
||||||
|| storedToken.JwtId != jti
|
|
||||||
|| storedToken.UserUUID != Guid.Parse(userUUID)
|
|
||||||
)
|
)
|
||||||
{
|
.ExecuteUpdateAsync(setters => setters.SetProperty(rt => rt.Used, true).SetProperty(rt => rt.Revoked, true));
|
||||||
return new CreateJWT { Success = false, Errors = ["Invalid or expired refresh token."] };
|
|
||||||
}
|
|
||||||
|
|
||||||
var user = await _db.Users.FirstOrDefaultAsync(u => u.UUID == 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 == userGuid);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
return new CreateJWT { Success = false, Errors = ["Invalid token."] };
|
return new CreateJWT { Success = false, Errors = ["Invalid token."] };
|
||||||
|
|
||||||
@@ -165,9 +163,7 @@ namespace SharpbinV3.Server.Services
|
|||||||
if (!newTokenResult.Success)
|
if (!newTokenResult.Success)
|
||||||
return newTokenResult;
|
return newTokenResult;
|
||||||
|
|
||||||
storedToken.Used = true;
|
await transaction.CommitAsync();
|
||||||
storedToken.Revoked = true;
|
|
||||||
await _db.SaveChangesAsync();
|
|
||||||
|
|
||||||
return newTokenResult;
|
return newTokenResult;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ namespace SharpbinV3.Server.Settings
|
|||||||
[Required, MaxLength(256)]
|
[Required, MaxLength(256)]
|
||||||
public string Domain { get; set; } = string.Empty;
|
public string Domain { get; set; } = string.Empty;
|
||||||
public bool Https { get; set; } = true;
|
public bool Https { get; set; } = true;
|
||||||
|
public string[] AllowedCorsOrigins { get; set; } = ["http://localhost:5173"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
"DefaultConnection": "Data Source=sharpbin.db"
|
"DefaultConnection": "Data Source=sharpbin.db"
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
|
"AllowedCorsOrigins": [
|
||||||
|
"http://localhost:5173"
|
||||||
|
],
|
||||||
"JwtSettings": {
|
"JwtSettings": {
|
||||||
"Secret": "",
|
"Secret": "",
|
||||||
"Issuer": "SharpbinApi",
|
"Issuer": "SharpbinApi",
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
# Required Misc. Variables
|
# Required Misc. Variables
|
||||||
Domain=example.com
|
Domain=example.com
|
||||||
Https=true
|
Https=true
|
||||||
|
AllowedCorsOrigins__0=https://example.com
|
||||||
JwtSettings__Secret=totally_random_and_secure_string
|
JwtSettings__Secret=totally_random_and_secure_string
|
||||||
|
ConnectionStrings__DefaultConnection=Data Source=/app/data/sharpbin.db
|
||||||
|
|
||||||
# Auth Settings
|
# Auth Settings
|
||||||
AuthSettings__CF_Turnstile_SecretKey=1x0000000000000000000000000000000AA
|
AuthSettings__CF_Turnstile_SecretKey=1x0000000000000000000000000000000AA
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ services:
|
|||||||
- "backend.env"
|
- "backend.env"
|
||||||
environment:
|
environment:
|
||||||
DATA_PROTECTION_KEY_PATH: /app/data/keys
|
DATA_PROTECTION_KEY_PATH: /app/data/keys
|
||||||
|
ConnectionStrings__DefaultConnection: "Data Source=/app/data/sharpbin.db"
|
||||||
volumes:
|
volumes:
|
||||||
- sharpbin-data:/app/data
|
- sharpbin-data:/app/data
|
||||||
- ./SharpbinV3.Server/appsettings.json:/app/appsettings.json
|
- ./SharpbinV3.Server/appsettings.json:/app/appsettings.json
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
VITE_API_URL=http://backend:8080
|
||||||
VITE_ALLOWED_HOSTS=localhost,example.com
|
VITE_ALLOWED_HOSTS=localhost,example.com
|
||||||
# Used so that views are only counted from the frontend
|
# Used so that views are only counted from the frontend
|
||||||
# Must be the same on both the frontend and backend environment
|
# Must be the same on both the frontend and backend environment
|
||||||
|
|||||||
Reference in New Issue
Block a user