Handling untrusted files
cadkit treats every input as hostile, because drawings routinely arrive from third parties. The readers are pure Rust and must not panic on any input. Every size, count and offset read from a file is checked against configurable limits before memory is allocated or a loop runs, recursion is bounded by a depth limit, and the XML reader resolves no external entities and makes no network requests. Readers are fuzzed in CI, and public samples are truncated and bit-flipped in tests.
Parsing a format is still not a security boundary on its own. For a service that accepts uploads, combine three layers: lower limits, careful error handling and an isolated process.
1. Lower the limits
Section titled “1. Lower the limits”The default limits are generous, for trusted desktop use: 2 GiB input, 4 GiB decompressed,
20 million objects, depth 64. Limits::strict() in Rust is meant for untrusted multi-tenant
input: 64 MiB input, 256 MiB decompressed, 500,000 objects, depth 32, 4 MiB per string and
1 million vertices per entity. The other interfaces take a subset of them as options; the table below lists which.
use cadkit::{Error, Limits, ReadOptions, SvgOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> { // 64 MiB input, 256 MiB decompressed, 500k objects, depth 32. let options = ReadOptions::new().with_limits(Limits::strict()); let doc = match cadkit::open_with("upload.dxf", &options) { Ok(doc) => doc, Err(Error::LimitExceeded { .. }) => return Err("rejected: over a limit".into()), Err(e) => return Err(format!("rejected: {e}").into()), }; // A complete SVG within 16 MiB, or Error::LimitExceeded: never a truncated drawing. let svg_options = SvgOptions::new().with_max_output_bytes(16 << 20).with_images(false); let svg = cadkit::to_svg_bounded(&doc, &svg_options, false)?; std::fs::write("upload.svg", svg)?; Ok(())}import cadkit
MIB = 1024 * 1024
try: doc = cadkit.read( "upload.dxf", max_input_bytes=64 * MIB, max_decompressed_bytes=256 * MIB, max_objects=500_000, max_depth=32, )except cadkit.LimitExceededError as e: print("rejected, over a limit:", e)except cadkit.UnknownFormatError: print("rejected, not a drawing")except cadkit.CadkitError as e: # UnsupportedError, InvalidDataError print("rejected:", type(e).__name__, e)else: print(doc, len(doc.warnings), "warnings")import { readFileSync, writeFileSync } from "node:fs";import { read } from "cadkit-wasm/node";
const MIB = 1024 * 1024;let doc;try { doc = read(readFileSync("upload.dxf"), { maxInputBytes: 64 * MIB, maxDecompressedBytes: 256 * MIB, maxObjects: 500_000, maxDepth: 32, }); // A complete SVG within 16 MiB, or a limit_exceeded error: never a truncated drawing. writeFileSync("upload.svg", doc.toSvgBounded({ widthPx: 1200, maxOutputBytes: 16 * MIB, images: false }));} catch (error) { // error.kind: unknown_format, unsupported, invalid, truncated, limit_exceeded, io, bad_argument console.log("rejected:", error.kind, error.message);} finally { doc?.free();}#include <stdio.h>#include "cadkit.h"
int main(void) { cadkit_options options; cadkit_options_init(&options); /* defaults; zero selects the default of a field */ options.max_input_bytes = 64ull << 20; options.max_decompressed_bytes = 256ull << 20; options.max_objects = 500000; options.max_depth = 32; options.max_string_bytes = 4u << 20; options.max_vertices = 1000000;
cadkit_document *doc = NULL; cadkit_status st = cadkit_read_file("upload.dxf", &options, &doc); if (st == CADKIT_LIMIT_EXCEEDED) { fprintf(stderr, "rejected, over a limit: %s\n", cadkit_last_error_message()); return 1; } if (st != CADKIT_OK) { fprintf(stderr, "rejected: %s: %s\n", cadkit_status_string(st), cadkit_last_error_message()); return 1; } cadkit_info info; if (cadkit_document_info(doc, &info) == CADKIT_OK) printf("%llu entities, %llu warnings\n", (unsigned long long)info.entity_count, (unsigned long long)info.warning_count); cadkit_document_free(doc); return 0;}cadkit --max-input-bytes 67108864 --max-decompressed-bytes 268435456 \ --max-objects 500000 info upload.dxf| Interface | Limits available |
|---|---|
| Rust | every field of Limits, including string bytes, vertices per entity and total vertices |
| C | cadkit_options: input, decompressed, objects, depth, string bytes, vertices |
| Python | max_input_bytes, max_decompressed_bytes, max_objects, max_depth |
| JavaScript | maxInputBytes, maxDecompressedBytes, maxObjects, maxDepth; defaults are already lower (512 MiB, 1 GiB, 5 million, 64) |
| CLI | --max-input-bytes, --max-decompressed-bytes, --max-objects |
2. Handle errors and warnings
Section titled “2. Handle errors and warnings”A file over a limit fails with a typed error (Error::LimitExceeded, LimitExceededError,
kind: "limit_exceeded", CADKIT_LIMIT_EXCEEDED, exit code 1). Other failures are
UnknownFormat (not a drawing), Unsupported (valid input using a feature cadkit does not
handle), and Truncated or Invalid (damaged data). Readers fail only when no useful
document can be built; recoverable damage becomes a warning on the document, which you can
show to the user. In the C API, an internal panic is caught at the boundary and returned as
CADKIT_PANIC; please report it.
Bound what you produce, too. The bounded SVG export (cadkit::to_svg_bounded,
toSvgBounded) counts every byte of the SVG it builds and fails with a limit error rather
than returning a truncated drawing. In a browser, pickEntityBounded caps the metadata it
returns. Neither bounds the memory of the already parsed document: that is what the read
limits are for.
3. Isolate the process
Section titled “3. Isolate the process”Run conversions of untrusted files in a separate process with an operating-system memory
limit and a time limit, such as a worker process under a container, cgroup or job-object limit
that you can kill and restart. Large inputs within the configured limits can still take time
and memory; the security policy treats that as out of scope. In the browser, the equivalent is
a Web Worker that you terminate on limit_exceeded or a WebAssembly trap; see
Build a browser viewer.
Also out of scope for cadkit: software that consumes its output. If you render generated SVG, sanitise or isolate it like any other untrusted markup, for example with a Content Security Policy or by showing it as an image.
Reporting a vulnerability
Section titled “Reporting a vulnerability”Report privately, through GitHub private vulnerability reporting or the address on the security page. Do not attach drawings that contain confidential data; a reduced or synthetic file is preferred.