mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
fix: multiple vulnerabilities
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<string[]>() ?? ["http://localhost:5173"];
|
||||
o.AddDefaultPolicy(p => p.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod());
|
||||
});
|
||||
|
||||
builder
|
||||
|
||||
@@ -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<ApiKey?> GetByKeyAsync(string key)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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"];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
"DefaultConnection": "Data Source=sharpbin.db"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"AllowedCorsOrigins": [
|
||||
"http://localhost:5173"
|
||||
],
|
||||
"JwtSettings": {
|
||||
"Secret": "",
|
||||
"Issuer": "SharpbinApi",
|
||||
|
||||
Reference in New Issue
Block a user