Secures user-uploaded files in Orchard Core 4.0 using the file upload security pipeline. Covers FileCreationService, IFileEventHandler, file signature validation, and integration with Media API, TUS, and deployment imports.
Ships in orchardcore. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an Orchard Core expert. Generate code that runs every user-uploaded file through Orchard Core's file upload security pipeline before it is stored permanently, and handlers that inspect or reject those files.
FileCreationService (namespace OrchardCore.FileStorage, package OrchardCore.FileStorage.Abstractions) calls every registered IFileEventHandler.CreatingAsync() before a file is written, and CreatedAsync() after it was stored.CreateAsync() before any permanent write. When the returned FileCreatingResult.Succeeded is false, abort and do not store the file. Never save first and scan afterwards.result.Stream, not the original upload stream: a handler may have replaced it.CreatedAsync() only after the file was stored successfully, with the stored entry.await using, whether it succeeded or failed, and keep it alive while you read result.Stream.AntivirusScanningException, a FileStoreException, when it cannot scan). Treat an exception as a rejected upload.IMediaFileStore.CreateFileFromStreamAsync() overload that takes a FileCreationService. The plain CreateFileFromStreamAsync(path, stream, overwrite) only runs IMediaCreatingEventHandler, not this pipeline.MediaOptions allowed extensions and MaxFileSize. The pipeline adds to them.sealed modifier, except View Models.| Type | Members |
|---|---|
FileCreationService | CreateAsync(context, stream, cancellationToken), CreateAsync(context, stream, leaveOpen, cancellationToken), CreatedAsync(IFileStoreEntry, cancellationToken) |
FileCreatingContext | Constructor (path, length = null, contentType = null). Path, FileName (from Path), Length, ContentType, all settable by handlers except FileName. |
FileCreatingResult | Derives from OrchardCore.Infrastructure.Result and is IAsyncDisposable. Stream, Succeeded, Errors, ErrorMessage (first non-empty error). Factories Success(stream) and Failed(stream = null, params ResultError[] errors). |
IFileEventHandler | Task<FileCreatingResult> CreatingAsync(FileCreatingContext, Stream, CancellationToken), Task CreatedAsync(IFileStoreEntry, CancellationToken) |
Pipeline behavior:
null makes CreateAsync() throw InvalidOperationException. A result whose Stream is null keeps the current stream.CreateAsync() returns a successful result over the original stream and CreatedAsync() does nothing.| Upload surface | How it uses the pipeline |
|---|---|
Media Library uploads (api/media upload endpoint, used by the admin Media Gallery and media fields) | Calls IMediaFileStore.CreateFileFromStreamAsync(fileCreationService, ...) for each file after the extension and folder permission checks. A rejection is returned as the file's Error. |
media recipe step | Stores each file through the same overload, so files imported by recipes and deployment packages are checked. |
XML-RPC metaWeblog.newMediaObject (OrchardCore.RemotePublishing) | Stores the file through the same overload. |
| Deployment package import (Tools → Deployments → Package Import) | Runs CreateAsync() on the uploaded .zip or .json before copying it to a temporary archive. A rejection shows an error notification. CreatedAsync() is not called because the package is never stored permanently. |
Remote deployment import endpoint (OrchardCore.Deployment.Remote) | Same as the package import; a rejection returns 400 Bad Request with the error message. |
TUS resumable uploads (OrchardCore.Media.Tus) | On completion the file is moved with the plain IMediaFileStore.CreateFileFromStreamAsync(path, stream), so it does not run this pipeline in the current source. Check the Orchard Core version you target before relying on TUS uploads being scanned. |
FileCreationService is registered with TryAddTransient by the OrchardCore.Media, OrchardCore.Deployment and OrchardCore.Deployment.Remote features. A feature that can run without them must register it itself.
Use this for custom controllers, minimal APIs, admin endpoints, importers, or background flows that accept a file and store it anywhere other than the media library.
using Microsoft.AspNetCore.Http;
using OrchardCore.FileStorage;
namespace MyModule.Services;
public sealed class AttachmentUploadService
{
private readonly FileCreationService _fileCreationService;
private readonly IFileStore _fileStore;
public AttachmentUploadService(
FileCreationService fileCreationService,
IFileStore fileStore)
{
_fileCreationService = fileCreationService;
_fileStore = fileStore;
}
public async Task<string> UploadAsync(IFormFile file, CancellationToken cancellationToken)
{
await using var uploadedStream = file.OpenReadStream();
await using var result = await _fileCreationService.CreateAsync(
new FileCreatingContext(file.FileName, file.Length, file.ContentType),
uploadedStream,
cancellationToken);
if (!result.Succeeded)
{
throw new FileStoreException(result.ErrorMessage
?? $"The uploaded file '{file.FileName}' was rejected before it could be stored.");
}
// Store the processed stream while the result is still in scope.
var path = await _fileStore.CreateFileFromStreamAsync(file.FileName, result.Stream);
var fileInfo = await _fileStore.GetFileInfoAsync(path);
await _fileCreationService.CreatedAsync(fileInfo, cancellationToken);
return path;
}
}
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OrchardCore.FileStorage;
using OrchardCore.Modules;
namespace MyModule;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
// Needed when neither Media nor Deployment is guaranteed to be enabled.
services.TryAddTransient<FileCreationService>();
services.AddScoped<AttachmentUploadService>();
}
}
Return the rejection to the user instead of throwing when that fits the endpoint better, for example TypedResults.Problem(result.ErrorMessage, statusCode: StatusCodes.Status400BadRequest) or a notifier error.
MediaFileStoreExtensions (namespace OrchardCore.Media, package OrchardCore.Media.Abstractions) wraps the whole flow: it calls CreateAsync() with leaveOpen: true, throws FileStoreException when the file is rejected, stores result.Stream, and calls CreatedAsync().
using Microsoft.AspNetCore.Http;
using OrchardCore.FileStorage;
using OrchardCore.Media;
namespace MyModule.Services;
public sealed class AvatarUploadService
{
private readonly IMediaFileStore _mediaFileStore;
private readonly FileCreationService _fileCreationService;
public AvatarUploadService(
IMediaFileStore mediaFileStore,
FileCreationService fileCreationService)
{
_mediaFileStore = mediaFileStore;
_fileCreationService = fileCreationService;
}
public async Task<string> UploadAsync(IFormFile file, string userId, CancellationToken cancellationToken)
{
// IFileStore.Combine() is an extension in OrchardCore.FileStorage.
var path = _mediaFileStore.Combine("avatars", userId, Path.GetFileName(file.FileName));
await using var stream = file.OpenReadStream();
// Throws FileStoreException (or AntivirusScanningException) when a handler rejects the file.
return await _mediaFileStore.CreateFileFromStreamAsync(
_fileCreationService,
path,
stream,
overwrite: true,
length: file.Length,
contentType: file.ContentType,
cancellationToken: cancellationToken);
}
}
Do not wrap this overload in another CreateAsync() call; that runs every handler twice.
CreateAsync() owns the original stream: the returned result disposes the final stream when it is disposed. Disposing your own await using copy as well is harmless.leaveOpen: true when the caller must keep ownership of the original stream, for example when it reuses or disposes it in a loop. A replacement stream created by a handler is still owned and disposed by the result.leaveOpen is true).Return FileCreatingResult.Failed(stream, ...) to reject, and FileCreatingResult.Success(stream) to continue with the same or a replacement stream. Use ResultError (namespace OrchardCore.Infrastructure) with a LocalizedString message; it becomes ErrorMessage.
using Microsoft.Extensions.Localization;
using OrchardCore.FileStorage;
using OrchardCore.Infrastructure;
namespace MyModule.Services;
public sealed class DocumentTypeFileEventHandler : IFileEventHandler
{
private static readonly HashSet<string> s_blockedExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".exe", ".dll", ".bat", ".cmd", ".ps1", ".msi", ".js",
};
private static readonly byte[] s_pdfSignature = "%PDF-"u8.ToArray();
private readonly IStringLocalizer S;
public DocumentTypeFileEventHandler(IStringLocalizer<DocumentTypeFileEventHandler> stringLocalizer)
{
S = stringLocalizer;
}
public async Task<FileCreatingResult> CreatingAsync(
FileCreatingContext context,
Stream stream,
CancellationToken cancellationToken = default)
{
var extension = Path.GetExtension(context.FileName);
if (s_blockedExtensions.Contains(extension))
{
return Reject(stream, S["Files of type {0} are not allowed.", extension]);
}
if (string.Equals(extension, ".pdf", StringComparison.OrdinalIgnoreCase))
{
if (context.ContentType is not null && context.ContentType != "application/pdf")
{
return Reject(stream, S["The file {0} is not a PDF document.", context.FileName]);
}
// Sniff the signature only when the stream can be rewound for the next handler.
if (stream.CanSeek)
{
var header = new byte[s_pdfSignature.Length];
stream.Position = 0;
var read = await stream.ReadAtLeastAsync(header, header.Length, throwOnEndOfStream: false, cancellationToken);
stream.Position = 0;
if (read < header.Length || !header.AsSpan().SequenceEqual(s_pdfSignature))
{
return Reject(stream, S["The file {0} is not a PDF document.", context.FileName]);
}
}
}
return FileCreatingResult.Success(stream);
}
public Task CreatedAsync(IFileStoreEntry fileInfo, CancellationToken cancellationToken = default)
=> Task.CompletedTask;
private static FileCreatingResult Reject(Stream stream, LocalizedString message)
=> FileCreatingResult.Failed(stream, new ResultError { Message = message });
}
Register handlers with TryAddEnumerable, so they are never added twice:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OrchardCore.FileStorage;
using OrchardCore.Modules;
namespace MyModule;
public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.TryAddEnumerable(ServiceDescriptor.Scoped<IFileEventHandler, DocumentTypeFileEventHandler>());
}
}
Handler rules:
Singleton for stateless handlers (the ClamAV handler is a singleton) and Scoped when the handler needs scoped services such as ISiteService or a localizer bound to the request culture.Position on a seekable stream, or copy a non-seekable stream to a temporary file from ITempDirectoryProvider (open it with FileOptions.DeleteOnClose) and return Success(tempStream). See orchardcore-temp-file-storage.context.Path, Length or ContentType, but the caller decides the storage path; built-in callers keep the path they passed in.CreatedAsync() for post-storage work such as auditing or indexing. It receives the stored IFileStoreEntry.FileStoreException when the handler cannot reach a decision (for example a scanner outage); callers treat it as a rejection.orchardcore-antivirus covers the OrchardCore.Antivirus.ClamAV handler and its configuration.orchardcore-temp-file-storage covers ITempDirectoryProvider; temporary files are not scanned, so run uploads through this pipeline before storing them permanently.orchardcore-media covers MediaOptions extension and size limits, IMediaCreatingEventHandler and the Media API.orchardcore-deployments, orchardcore-deployment-remote and orchardcore-xml-rpc cover those upload surfaces.IFileStore, disk or blob storage should now call FileCreationService.CreateAsync() first and CreatedAsync() after storing.await mediaFileStore.CreateFileFromStreamAsync(path, stream) for a user upload. After: await mediaFileStore.CreateFileFromStreamAsync(fileCreationService, path, stream, length: file.Length, contentType: file.ContentType).IMediaCreatingEventHandler is unchanged and still runs inside IMediaFileStore; use IFileEventHandler for checks that must cover every upload surface.| Symptom | Check |
|---|---|
| Custom upload is not checked | The code writes directly to storage or calls the plain IMediaFileStore.CreateFileFromStreamAsync(path, stream); route it through CreateAsync() or the overload that takes a FileCreationService. |
| Files are checked twice | Remove the extra CreateAsync() around the media overload. |
Unable to resolve service for type FileCreationService | No enabled feature registers it; add services.TryAddTransient<FileCreationService>(). |
ObjectDisposedException when storing | The FileCreatingResult was disposed before result.Stream was stored; keep it in scope with await using. |
| Stored file is empty or truncated | A handler read the stream without rewinding it. |
InvalidOperationException mentioning a null FileCreatingResult | A handler returned null; return Success(stream) instead. |
Official Orchard Core videos shown on the documentation page:
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
orchard-core, file-upload, security-pipeline, file-validation, antivirus
/orchardcore:orchardcore-file-upload-securityfrom orchardcore
npx claudepluginhub crestapps/crestapps.agentskills --plugin orchardcore140 skills in this plugin
/orchardcore-adminGuidance for the Orchard Core admin panel, including admin controllers and routes, admin menu registration with AdminNavigationProvider, ocat editor forms grouped in cards, admin pages rendering Model.Editor, the UI text casing standard, TheAdmin layout and menu colors, admin branding, the Navbar shape, the Ctrl+K quick navigation palette and QuickNavigationSource, AdminSettings such as MenuBehavior, ShowBreadcrumb, DisplayQuickNavigation, DisplayTitlesInTopbar and ActionsLayout, the AdminDashboard shape, settings pages, and admin filters and zones. Use this skill when requests mention the Orchard Core admin panel, TheAdmin, admin controllers, admin menu registration, an icon on an admin menu item, the AdminSettings recipe step, quick navigation, navigation menu behavior, the admin URL prefix, UI text casing. Strong matches include OrchardCore.Admin, AdminSettings, AdminPermissions, AdminOptions, AdminAttribute, SiteDisplayDriver, IDisplayManager./orchardcore-admin-dashboardSkill for creating admin dashboard widgets in Orchard Core. Covers the DashboardWidget stereotype, DashboardPart position and sizing, the built-in HtmlDashboardWidget, ContentsMetadata and ContentsTags widget types, custom widget content types via recipe or migration, arranging widgets in Manage Dashboard mode, widget template customization, provisioning widgets with recipes, dashboard permissions, the DashboardBreadcrumbProvider Dashboard ancestor, and the AdminDashboardContent shape used when the feature is disabled. Use this skill when requests mention Orchard Core Admin Dashboard, Create and Configure Dashboard Widgets, Enabling Admin Dashboard, Widget Configuration Properties, dashboard widget recipes, Dashboard breadcrumb, or related implementation, setup, or troubleshooting work. Strong matches include OrchardCore.AdminDashboard, DashboardPart, DashboardBreadcrumbProvider, AccessAdminDashboard, ManageAdminDashboard.Other plugins' skills for the same job