mirror of
https://github.com/bitwarden/server.git
synced 2025-05-22 12:04:27 -05:00

* Direct upload to azure To validate file sizes in the event of a rogue client, Azure event webhooks will be hooked up to AzureValidateFile. Sends outside of a grace size will be deleted as non-compliant. TODO: LocalSendFileStorageService direct upload method/endpoint. * Quick respond to no-body event calls These shouldn't happen, but might if some errant get requests occur * Event Grid only POSTS to webhook * Enable local storage direct file upload * Increase file size difference leeway * Upload through service * Fix LocalFileSendStorage It turns out that multipartHttpStreams do not have a length until read. this causes all long files to be "invalid". We need to write the entire stream, then validate length, just like Azure. the difference is, We can return an exception to local storage admonishing the client for lying * Update src/Api/Utilities/ApiHelpers.cs Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> * Do not delete directory if it has files * Allow large uploads for self hosted instances * Fix formatting * Re-verfiy access and increment access count on download of Send File * Update src/Core/Services/Implementations/SendService.cs Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> * Add back in original Send upload * Update size and mark as validated upon Send file validation * Log azure file validation errors * Lint fix Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com>
81 lines
3.0 KiB
C#
81 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Azure.EventGrid;
|
|
using Microsoft.Azure.EventGrid.Models;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Bit.Api.Utilities
|
|
{
|
|
public static class ApiHelpers
|
|
{
|
|
public async static Task<T> ReadJsonFileFromBody<T>(HttpContext httpContext, IFormFile file, long maxSize = 51200)
|
|
{
|
|
T obj = default(T);
|
|
if (file != null && httpContext.Request.ContentLength.HasValue && httpContext.Request.ContentLength.Value <= maxSize)
|
|
{
|
|
try
|
|
{
|
|
using (var stream = file.OpenReadStream())
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
var s = await reader.ReadToEndAsync();
|
|
if (!string.IsNullOrWhiteSpace(s))
|
|
{
|
|
obj = JsonConvert.DeserializeObject<T>(s);
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates Azure event subscription and calls the appropriate event handler. Responds HttpOk.
|
|
/// </summary>
|
|
/// <param name="request">HttpRequest received from Azure</param>
|
|
/// <param name="eventTypeHandlers">Dictionary of eventType strings and their associated handlers.</param>
|
|
/// <returns>OkObjectResult</returns>
|
|
/// <remarks>Reference https://docs.microsoft.com/en-us/azure/event-grid/receive-events</remarks>
|
|
public async static Task<OkObjectResult> HandleAzureEvents(HttpRequest request,
|
|
Dictionary<string, Func<EventGridEvent, Task>> eventTypeHandlers)
|
|
{
|
|
var response = string.Empty;
|
|
var requestContent = await new StreamReader(request.Body).ReadToEndAsync();
|
|
if (string.IsNullOrWhiteSpace(requestContent))
|
|
{
|
|
return new OkObjectResult(response);
|
|
}
|
|
|
|
var eventGridSubscriber = new EventGridSubscriber();
|
|
var eventGridEvents = eventGridSubscriber.DeserializeEventGridEvents(requestContent);
|
|
|
|
foreach (var eventGridEvent in eventGridEvents)
|
|
{
|
|
if (eventGridEvent.Data is SubscriptionValidationEventData eventData)
|
|
{
|
|
// Might want to enable additional validation: subject, topic etc.
|
|
|
|
var responseData = new SubscriptionValidationResponse()
|
|
{
|
|
ValidationResponse = eventData.ValidationCode
|
|
};
|
|
|
|
return new OkObjectResult(responseData);
|
|
}
|
|
else if (eventTypeHandlers.ContainsKey(eventGridEvent.EventType))
|
|
{
|
|
await eventTypeHandlers[eventGridEvent.EventType](eventGridEvent);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(response);
|
|
}
|
|
}
|
|
}
|