feat: secure existing events endpoints by validating the token cookie

This commit is contained in:
2025-05-18 13:45:54 +02:00
parent fc1ff88f3d
commit 9034c058f0
2 changed files with 17 additions and 12 deletions

View File

@@ -28,21 +28,23 @@ namespace WebApp.Endpoints
// GET /events/1
group.MapGet("/{id}",
async (int id, ApplicationDbContext dbContext, HttpContext httpContext) =>
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
{
Event? Eve = await dbContext.Events.FindAsync(id);
if (Eve is null) return Results.NotFound();
// Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
// ...
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token);
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
// przed jego wysłaniem!
// ...
Eve.ToEventDetailsDto();
if (org is null || org.OrganisationId != Eve.OrganisationId) Eve.EventRegistrations = [];
return Results.Ok(Eve.ToEventDetailsDto()); //EventDetailsDto
return Results.Ok(Eve); //EventDetailsDto
})
.WithName(GetEventEndpointName);
@@ -54,13 +56,13 @@ namespace WebApp.Endpoints
// Uzyskaj organizację z tokenu
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token);
if (org is null) return Results.StatusCode(403);
if (org is null) return Results.Unauthorized();
Event Eve = newEvent.ToEntity();
// Wyzeruj EventRegistrations, ponieważ nie są to dane,
// które powinniśmy przyjmować bez zgody wolontariuszy!
// ...
Eve.EventRegistrations = [];
dbContext.Events.Add(Eve);
await dbContext.SaveChangesAsync();
@@ -79,7 +81,7 @@ namespace WebApp.Endpoints
// Uzyskaj organizację z tokenu
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token);
if (org is null) return Results.StatusCode(403);
if (org is null) return Results.Unauthorized();
var existingEvent = await dbContext.Events.FindAsync(id);
if (existingEvent is null)
@@ -89,12 +91,13 @@ namespace WebApp.Endpoints
// Sprawdź, czy organizacja ma prawo
// do zmodyfikowania tego (EventId = id) eventu.
// ...
if (org.OrganisationId != existingEvent.OrganisationId) return Results.StatusCode(403);
// Nadpisz organisationId (obecne w updatedEvent,
// lecz nie sprawdzane poniżej) na to, co odczytaliśmy
// do existingEvent.
// ...
// ... trzeba by było tworzyć obiekt od nowa, zamiast tego po prostu zwróćmy błąd.
if (existingEvent.OrganisationId != updatedEvent.OrganisationId) return Results.StatusCode(403);
dbContext.Entry(existingEvent)
.CurrentValues
@@ -121,7 +124,9 @@ namespace WebApp.Endpoints
// Sprawdź, czy organizacja ma prawo
// do usunięcia tego (EventId = id) eventu.
// ...
Event? Eve = await dbContext.Events.FindAsync(id);
if (Eve is null) return Results.NotFound();
else if (org.OrganisationId != Eve.OrganisationId) return Results.StatusCode(403);
await dbContext.Events
.Where(Eve => Eve.EventId == id)