Harden backend auth, validation and error handling
- Rate limit login (5 attempts/min per IP) with a JSON 429 response - Constant-time password comparison; refuse login if no password is configured - Explicit SameSite=Strict auth cookie; log failed logins with client IP - Stop logging door codes (update log lines and the SMS body) - Replace per-action try/catch -> 400 with a global exception handler (JSON 500) - Return proper statuses: 404 for missing person, 502 for failed lock writes - Validate codes as ASCII digits only (int.TryParse accepted +/-/whitespace) - Validate person name and E.164 phone number; reject sending an unset code - Rename TwiloOptions -> TwilioOptions and fix message typos - Expose Program for integration tests
This commit is contained in:
1 parent
768bcc90e4
commit
d0425c8cb2
38 files changed
+189
-487
No files matched your search
@@ -2,6 +2,7 @@
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.RegularExpressions;
|
||||
using YaleAccess.Data;
|
||||
using YaleAccess.Models;
|
||||
|
||||
@@ -11,66 +12,62 @@ namespace YaleAccess.Controllers
|
||||
[Route("api/[controller]")]
|
||||
[EnableCors]
|
||||
[Authorize]
|
||||
public class PeopleController(ILogger<PeopleController> logger, YaleContext context) : ControllerBase
|
||||
public partial class PeopleController(YaleContext context) : ControllerBase
|
||||
{
|
||||
// Twilio requires numbers in E.164 format, e.g. +61400000000
|
||||
[GeneratedRegex(@"^\+[1-9]\d{6,14}$")]
|
||||
private static partial Regex PhoneNumberRegex();
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetPeople()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Return all people
|
||||
List<Person> people = await context.People.ToListAsync();
|
||||
return Ok(new ApiResponse(people));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occured retriving the people.");
|
||||
return BadRequest(new ApiResponse("An error occured retriving the people."));
|
||||
}
|
||||
// Return all people
|
||||
List<Person> people = await context.People.ToListAsync();
|
||||
return Ok(new ApiResponse(people));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CreatePerson([FromBody] Person person)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create a new person
|
||||
Person newPerson = new() { Name = person.Name, PhoneNumber = person.PhoneNumber };
|
||||
// Validate the person
|
||||
string name = person.Name?.Trim() ?? string.Empty;
|
||||
string phoneNumber = person.PhoneNumber?.Trim() ?? string.Empty;
|
||||
|
||||
// Add the person
|
||||
await context.AddAsync(newPerson);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Return the newly created person
|
||||
return Ok(new ApiResponse(newPerson));
|
||||
}
|
||||
catch (Exception ex)
|
||||
if (name.Length == 0 || name.Length > 100)
|
||||
{
|
||||
logger.LogError(ex, "An error occured creating the person.");
|
||||
return BadRequest(new ApiResponse("An error occured creating the person."));
|
||||
return BadRequest(new ApiResponse("The name is required and must be 100 characters or fewer."));
|
||||
}
|
||||
|
||||
if (!PhoneNumberRegex().IsMatch(phoneNumber))
|
||||
{
|
||||
return BadRequest(new ApiResponse("The phone number must be in international format, e.g. +61400000000."));
|
||||
}
|
||||
|
||||
// Create a new person and add them
|
||||
Person newPerson = new() { Name = name, PhoneNumber = phoneNumber };
|
||||
await context.AddAsync(newPerson);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Return the newly created person
|
||||
return Ok(new ApiResponse(newPerson));
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeletePerson([FromRoute] int id)
|
||||
{
|
||||
try
|
||||
// Ensure the person exists
|
||||
Person? person = await context.People.FindAsync(id);
|
||||
if (person is null)
|
||||
{
|
||||
// Ensure the person exists
|
||||
Person person = await context.People.FindAsync(id) ?? throw new Exception("Person not found.");
|
||||
|
||||
// Remove the person
|
||||
context.Remove(person);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Return the newly removed person
|
||||
return Ok(new ApiResponse(person));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "An error occured deletiong the person.");
|
||||
return BadRequest(new ApiResponse("An error occured deletiong the person."));
|
||||
return NotFound(new ApiResponse("Person not found."));
|
||||
}
|
||||
|
||||
// Remove the person
|
||||
context.Remove(person);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
// Return the removed person
|
||||
return Ok(new ApiResponse(person));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user