🚀 Porting libmagic to Rust: Safer File Identification 🚀
A little while ago (maybe a month or two), I started porting a great project to Rust: libmagic, the library behind the file
command utility.
🤔 Why did I do that?
For years, I've faced the same issue with file identification: embedding a C library that does intensive parsing into my memory-safe code (Rust, Go, Python, etc.). While I trust the file/libmagic developer community's code quality, I know there are skilled people capable of finding and exploiting bugs—especially in C/C++ parsers. So I've always been reluctant to run libmagic on untrusted input, which creates a tricky situation that kind of defeats its original purpose: categorizing files you know nothing about.
🦀 Why Rust?
Rust is the perfect fit for this port. It provides strong safety guarantees while maintaining high performance and easy bindings to other languages (C/C++, Python, Go, Elixir, etc.). Bonus: the library and tool will be portable to all Rust-supported targets.
💻 Show me the code!
Here it is: https://github.com/qjerome/magic-rs/pull/1
⚠️ Note: This isn't a full implementation yet—it's still a work in progress and needs more work for a usable, clean version (see PR description). That said, if you're feeling adventurous, you can test it out. It already successfully identifies several file types: MS-DOS executables, ELF binaries, scripts, and more.
🔄 Compatibility with C libmagic file format?
This project aims for 99% compatibility with libmagic's rule file format. A few rules (such as those using ternary notations in messages) may need adjustments, but the goal is to stay true to the existing specification.
📌 What's next?
- Finalize the library implementation and publish a Rust crate
- Complete the CLI tool (a file
-equivalent implementation)
- Create bindings for other languages
If you're interested in this work, don't hesitate to follow along or reach out!
Special thanks to @adulau and @circl for supporting this work! 🙏
#Rust#Libmagic#Security#OpenSource#Programming#SoftwareDevelopment