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

View File

@@ -25,7 +25,7 @@ public class GeneralUseHelpers
{ {
// Zwróci null, gdy nie znaleziono użytkownika // Zwróci null, gdy nie znaleziono użytkownika
if (t is null) return null; if (t is null) return null;
User? user = await _context.WebUsers.FindAsync(t.Value); User? user = await _context.WebUsers.FindAsync(t.UserId);
return user; return user;
} }
@@ -35,7 +35,7 @@ public class GeneralUseHelpers
User? user = await GetUserFromToken(t); User? user = await GetUserFromToken(t);
if (user is not null && user.IsOrganisation) if (user is not null && user.IsOrganisation)
{ {
Organisation? org = await _context.Organisations.FindAsync(t.UserId); Organisation? org = await _context.Organisations.FirstOrDefaultAsync(o => o.UserId == t.UserId);
if (org is null) if (org is null)
{ {