fix: lower vulnerabilities + harden docker setup

also includes a github workflow
This commit is contained in:
2026-06-17 22:18:55 -04:00
parent 4f5ca9a069
commit d16f1f872f
16 changed files with 164 additions and 137 deletions
+12 -5
View File
@@ -146,11 +146,18 @@ namespace SharpbinV3.Server.Services
var tokenHash = Utilities.ComputeSha256(refreshToken);
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));
var revokedCount = await _db.Database.ExecuteSqlInterpolatedAsync(
$"""
UPDATE RefreshTokens
SET Used = 1, Revoked = 1
WHERE TokenHash = {tokenHash}
AND Used = 0
AND Revoked = 0
AND ExpiresAt >= {now}
AND JwtId = {jti}
AND UserUUID = {userGuid}
"""
);
if (revokedCount != 1)
return new CreateJWT { Success = false, Errors = ["Invalid or expired refresh token."] };
+10 -6
View File
@@ -22,15 +22,17 @@ namespace SharpbinV3.Server.Services
data = _cs.Compress(content);
double compressionRatio = (double)data.Length / originalSize;
var isCompressed = compressionRatio < _pasteSettings.CompressionThreshold;
var storedContent = isCompressed ? data : Encoding.UTF8.GetBytes(content);
var comment = new Comment
{
PastePID = paste.PID,
ParentCommentID = parentCommentId,
Content = compressionRatio < _pasteSettings.CompressionThreshold ? data : Encoding.UTF8.GetBytes(content),
StoredSize = data.Length,
Content = storedContent,
StoredSize = storedContent.Length,
OriginalSize = originalSize,
IsCompressed = compressionRatio < _pasteSettings.CompressionThreshold,
IsCompressed = isCompressed,
UserUUID = author.UUID,
CreatedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
};
@@ -51,12 +53,14 @@ namespace SharpbinV3.Server.Services
data = _cs.Compress(newContent);
double compressionRatio = (double)data.Length / originalSize;
var isCompressed = compressionRatio < _pasteSettings.CompressionThreshold;
var storedContent = isCompressed ? data : Encoding.UTF8.GetBytes(newContent);
comment.Content = compressionRatio < _pasteSettings.CompressionThreshold ? data : Encoding.UTF8.GetBytes(newContent);
comment.StoredSize = data.Length;
comment.Content = storedContent;
comment.StoredSize = storedContent.Length;
comment.OriginalSize = originalSize;
comment.UpdatedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
comment.IsCompressed = compressionRatio < _pasteSettings.CompressionThreshold;
comment.IsCompressed = isCompressed;
_db.Comments.Update(comment);
var result = await _db.SaveChangesAsync();
+25 -76
View File
@@ -1,6 +1,5 @@
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using SharpbinV3.Server.Data;
using SharpbinV3.Server.Data.Entities;
@@ -10,14 +9,11 @@ using SharpbinV3.Server.Settings;
namespace SharpbinV3.Server.Services
{
public sealed class PasteService(AppDbContext db, ICompressionService cs, IMemoryCache cache, IOptions<PasteSettings> options)
public sealed class PasteService(AppDbContext db, ICompressionService cs, IOptions<PasteSettings> options)
{
private readonly AppDbContext _db = db;
private readonly ICompressionService _cs = cs;
private readonly IMemoryCache _cache = cache;
private readonly PasteSettings _pasteSettings = options.Value;
private readonly Lock _invalidationLock = new();
private readonly HashSet<string> _pasteListCacheKeys = [];
public async Task<Paste> Create(
User? author,
@@ -35,6 +31,8 @@ namespace SharpbinV3.Server.Services
data = _cs.Compress(content);
double compressionRatio = (double)data.Length / originalSize;
var isCompressed = compressionRatio < _pasteSettings.CompressionThreshold;
var storedContent = isCompressed ? data : Encoding.UTF8.GetBytes(content);
var paste = new Paste
{
@@ -43,10 +41,10 @@ namespace SharpbinV3.Server.Services
Title = title,
AuthorUUID = author?.UUID,
User = author,
Content = compressionRatio < _pasteSettings.CompressionThreshold ? data : Encoding.UTF8.GetBytes(content),
StoredSize = data.Length,
Content = storedContent,
StoredSize = storedContent.Length,
OriginalSize = originalSize,
IsCompressed = compressionRatio < _pasteSettings.CompressionThreshold,
IsCompressed = isCompressed,
Syntax = syntax,
Visibility = visibility,
ExpiresAt = expiresAt,
@@ -59,8 +57,6 @@ namespace SharpbinV3.Server.Services
}
await _db.SaveChangesAsync();
InvalidatePasteListCache();
return paste;
}
@@ -69,8 +65,6 @@ namespace SharpbinV3.Server.Services
_db.Pastes.Remove(paste);
var result = await _db.SaveChangesAsync();
InvalidatePasteCache(paste.ID);
return result > 0;
}
@@ -79,8 +73,6 @@ namespace SharpbinV3.Server.Services
_db.Pastes.Update(paste);
await _db.SaveChangesAsync();
InvalidatePasteCache(paste.ID);
return paste;
}
@@ -102,17 +94,17 @@ namespace SharpbinV3.Server.Services
data = _cs.Compress(text);
double compressionRatio = (double)data.Length / originalSize;
var isCompressed = compressionRatio < _pasteSettings.CompressionThreshold;
var storedContent = isCompressed ? data : Encoding.UTF8.GetBytes(text);
paste.Content = compressionRatio < _pasteSettings.CompressionThreshold ? data : Encoding.UTF8.GetBytes(text);
paste.StoredSize = data.Length;
paste.Content = storedContent;
paste.StoredSize = storedContent.Length;
paste.OriginalSize = originalSize;
paste.EditedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
paste.IsCompressed = compressionRatio < _pasteSettings.CompressionThreshold;
paste.IsCompressed = isCompressed;
_db.Pastes.Update(paste);
var result = await _db.SaveChangesAsync();
InvalidatePasteCache(paste.ID);
return result > 0;
}
@@ -151,7 +143,6 @@ namespace SharpbinV3.Server.Services
return (null, true);
}
InvalidatePasteCache(paste.ID);
return (paste, false);
}
@@ -172,7 +163,6 @@ namespace SharpbinV3.Server.Services
try
{
await _db.SaveChangesAsync();
InvalidatePasteCache(paste.ID);
return interaction;
}
catch (DbUpdateException)
@@ -192,56 +182,31 @@ namespace SharpbinV3.Server.Services
_db.PasteInteractions.Remove(interaction);
await _db.SaveChangesAsync();
InvalidatePasteCache(paste.ID);
return true;
}
public async Task<Paste?> Get(string id)
{
string key = $"v1:paste:{id}";
var paste = await _cache.GetOrCreateAsync(
key,
async entry =>
{
entry.SetSlidingExpiration(TimeSpan.FromMinutes(10));
var _paste = await _db.Pastes.AsNoTracking().Include(p => p.User).FirstOrDefaultAsync(p => p.ID == id);
if (_paste != null)
{
_paste.PositiveInteractionCount = await _db
.PasteInteractions.Where(pi => pi.PasteID == _paste.PID && pi.Type == Interaction.Positive)
.CountAsync();
_paste.NegativeInteractionCount = await _db
.PasteInteractions.Where(pi => pi.PasteID == _paste.PID && pi.Type == Interaction.Negative)
.CountAsync();
}
return _paste;
}
);
var paste = await _db.Pastes.AsNoTracking().Include(p => p.User).FirstOrDefaultAsync(p => p.ID == id);
if (paste != null)
{
paste.PositiveInteractionCount = await _db
.PasteInteractions.Where(pi => pi.PasteID == paste.PID && pi.Type == Interaction.Positive)
.CountAsync();
paste.NegativeInteractionCount = await _db
.PasteInteractions.Where(pi => pi.PasteID == paste.PID && pi.Type == Interaction.Negative)
.CountAsync();
}
return paste ?? null;
}
public async Task<List<Paste>> GetList(int offset, int count, bool publicOnly = false)
{
string key = $"v1:list:pastes:public={publicOnly}:offset={offset}:count={count}";
var query = _db.Pastes.AsQueryable();
if (publicOnly)
query = query.Where(p => p.Visibility == 0);
var pastes = await _cache.GetOrCreateAsync(
key,
async entry =>
{
entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
lock (_invalidationLock)
_pasteListCacheKeys.Add(key);
var query = _db.Pastes.AsQueryable();
if (publicOnly)
query = query.Where(p => p.Visibility == 0);
return await query.OrderByDescending(p => p.PID).Skip(offset).Take(count).Include(p => p.User).ToListAsync();
}
);
return pastes ?? [];
return await query.OrderByDescending(p => p.PID).Skip(offset).Take(count).Include(p => p.User).ToListAsync();
}
public async Task<PasteInteraction?> GetUserInteraction(Paste paste, Guid userUUID)
@@ -275,21 +240,5 @@ namespace SharpbinV3.Server.Services
return expiresAt > currentTime - 1000;
}
private void InvalidatePasteCache(string pasteId)
{
string key = $"v1:paste:{pasteId}";
_cache.Remove(key);
}
private void InvalidatePasteListCache()
{
lock (_invalidationLock)
{
foreach (var listKey in _pasteListCacheKeys)
_cache.Remove(listKey);
_pasteListCacheKeys.Clear();
}
}
}
}
+21 -17
View File
@@ -85,27 +85,31 @@ namespace SharpbinV3.Server.Services
public async Task<List<ReportResponseDto>> GetReports(ReportQuery query)
{
return await BuildReportQuery(query)
var pagedReports = BuildReportQuery(query)
.OrderByDescending(r => r.CreatedAt)
.Skip((query.Page - 1) * query.PageSize)
.Take(query.PageSize)
.Take(query.PageSize);
return await pagedReports
.GroupJoin(_db.Users, r => r.ReporterUUID, reporter => reporter.UUID, (report, reporters) => new { report, reporters })
.SelectMany(r => r.reporters.DefaultIfEmpty(), (r, reporter) => new { r.report, reporter })
.Select(r => new ReportResponseDto
{
ReportID = r.ReportID,
Type = r.Type,
Status = r.Status,
Description = r.Description,
CreatedAt = r.CreatedAt,
UpdatedAt = r.UpdatedAt,
ReporterUUID = r.ReporterUUID,
ReporterUsername = _db.Users.Where(u => u.UUID == r.ReporterUUID).Select(u => u.Username).FirstOrDefault(),
ReporterDisplayName = _db.Users.Where(u => u.UUID == r.ReporterUUID).Select(u => u.DisplayName).FirstOrDefault(),
TargetType = r.TargetType,
PasteId = r.Paste != null ? r.Paste.ID : null,
PasteTitle = r.Paste != null ? r.Paste.Title : null,
UserUUID = r.UserUUID,
TargetUsername = r.User != null ? r.User.Username : null,
TargetDisplayName = r.User != null ? r.User.DisplayName : null,
ReportID = r.report.ReportID,
Type = r.report.Type,
Status = r.report.Status,
Description = r.report.Description,
CreatedAt = r.report.CreatedAt,
UpdatedAt = r.report.UpdatedAt,
ReporterUUID = r.report.ReporterUUID,
ReporterUsername = r.reporter != null ? r.reporter.Username : null,
ReporterDisplayName = r.reporter != null ? r.reporter.DisplayName : null,
TargetType = r.report.TargetType,
PasteId = r.report.Paste != null ? r.report.Paste.ID : null,
PasteTitle = r.report.Paste != null ? r.report.Paste.Title : null,
UserUUID = r.report.UserUUID,
TargetUsername = r.report.User != null ? r.report.User.Username : null,
TargetDisplayName = r.report.User != null ? r.report.User.DisplayName : null,
})
.ToListAsync();
}