Set MaxInputBytes before parsing. Use settings.yaml from the zip: name: web and port: 8080 on separate lines.
dotnet add package OneYamlThe library targets .NET 8 and later; the example project uses .NET 10.
using OneYaml; var limits = YamlLimits.Untrusted with { MaxInputBytes = 32 }; var opts = new YamlDocumentOptions { Limits = limits }; using var file = File.OpenRead( "settings.yaml"); using var doc = YamlDocument.Parse(file, opts); long cap = limits.MaxInputBytes; Console.WriteLine($"{cap} bytes: accepted");
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
For the paste path, create settings.yaml next to Program.cs with name: web on the first line and port: 8080 on the second.
If the file is larger than MaxInputBytes, Parse throws a YamlException with Category set to Limit.
Prints:
32 bytes: acceptedThe full program
The download includes this settings.yaml:
name: web port: 8080
The program opens the same file twice: once with a 32-byte cap and once with an 8-byte cap.
using OneYaml; CheckFile(32); CheckFile(8); static void CheckFile(long maxBytes) { var limits = YamlLimits.Untrusted with { MaxInputBytes = maxBytes }; var options = new YamlDocumentOptions { Limits = limits }; using var file = File.OpenRead("settings.yaml"); try { using var doc = YamlDocument.Parse(file, options); Console.WriteLine($"{maxBytes} bytes: accepted"); } catch (YamlException error) when (error.Category == YamlErrorCategory.Limit) { Console.WriteLine($"{maxBytes} bytes: {error.Category}"); } }
Prints:
32 bytes: accepted 8 bytes: Limit
Variations
For a 1 MiB cap, replace the limits declaration in the snippet at the top:
var limits = YamlLimits.Untrusted with { MaxInputBytes = 1024 * 1024 };
Related recipes
The short answer
Set MaxInputBytes on YamlLimits, assign it to YamlDocumentOptions.Limits, and pass the open file stream to YamlDocument.Parse. Start from YamlLimits.Untrusted and use with to change only the byte cap. The preset’s other bounds still apply. The example uses a deliberately small cap so you can see a rejection without downloading a large file.
How do I check YAML file size before reading the file in C#?
Pass a FileStream rather than calling File.ReadAllText first. For a seekable stream, OneYaml checks its length before reading it. That gives you the size check and parser entry point in one call. If you read the file into a string first, the parser can still reject it, but it can’t undo the allocation your code already made.
The cap is in bytes, not characters. OneYaml checks both the input as provided and its UTF-8 form. For a file in an encoding other than UTF-8, passing the check on the file size alone doesn’t guarantee that the decoded document fits the cap. That’s why it’s useful to keep the parser bound even when you already know the size of the file on disk.
What happens if a YAML upload is too large?
For a non-seekable stream, such as a request body without a known length, OneYaml reads in pieces and stops once the input passes the cap. Your server’s upload limit is separate: an application can reject a request at the HTTP boundary before YAML parsing ever starts. Use the parser cap for the body you actually hand to OneYaml, including inputs that don’t arrive over HTTP.
The Limit category also covers other bounds. If you report Limit to users, don’t assume every such error means “file too large.” A small file may still hit a depth, scalar size or alias expansion bound. Default doesn’t set a byte cap, while Untrusted starts at 64 MiB; choose a smaller cap if your app only expects short configuration files. See the preset table for the bounds you inherit.