Installations
npm install magic-bytes.js
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.19.1
NPM Version
10.1.0
Score
99.5
Supply Chain
99.6
Quality
77.8
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (87.25%)
HTML (6.97%)
CSS (3.48%)
JavaScript (2.3%)
Developer
LarsKoelpin
Download Statistics
Total Downloads
11,677,162
Last Day
23,151
Last Week
193,049
Last Month
830,267
Last Year
8,485,621
GitHub Statistics
136 Stars
213 Commits
26 Forks
1 Watching
7 Branches
14 Contributors
Bundle Size
19.04 kB
Minified
3.80 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.10.0
Package Id
magic-bytes.js@1.10.0
Unpacked Size
47.97 kB
Size
12.40 kB
File Count
26
NPM Version
10.1.0
Node Version
18.19.1
Publised On
29 Feb 2024
Total Downloads
Cumulative downloads
Total Downloads
11,677,162
Last day
-1.6%
23,151
Compared to previous day
Last week
-9.6%
193,049
Compared to previous week
Last month
1.5%
830,267
Compared to previous month
Last year
202.7%
8,485,621
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Magic bytes
Magic Bytes is a javascript library analyzing the first bytes of a file to tell you its type. Use it inside your browser or serversided using nodejs.
The procedure is based on https://en.wikipedia.org/wiki/List_of_file_signatures.
[!NOTE]
A small note on versioning. Strictly speaking, each new filetype which is supported by this library can break someones' API. Please note that this library adds new filetypes with minor release. This means files, which validate to "null" in some versions, may find a result in a new version.Or in some cases the library will find more results, than before. So don't depend on the found-array size in any shape or form. Filetypes will not be remoevd tho
Installation
Run npm install magic-bytes.js
Interactive example
There is an interactive example present at https://larskoelpin.github.io/magic-bytes/.
Usage
The following functions are available:
filetypeinfo(bytes: number[])
Contains typeinformation like name, extension and mime type:[{typename: "zip"}, {typename: "jar"}]
filetypename(bytes: number[])
: Contains type names only:["zip", "jar"]
filetypemime(bytes: number[])
: Contains type mime types only:["application/zip", "application/jar"]
filetypeextension(bytes: number[])
: Contains type extensions only:["zip", "jar"]
Both function return an empty array []
otherwise, which means it could not detect the file signature. Keep in mind that
txt files for example fall in this category.
You don't have to load the whole file in memory. For validating a file uploaded to S3 using Lambda for example, it may be
enough to load the files first 100 bytes and validate against them. This is especially useful for big files.
see examples for practical usage.
On server:
1import filetype from 'magic-bytes.js' 2 3filetype(fs.readFileSync("myimage.png")) // ["png"]
To run an HTML-Example checkout the project and run
npm install; npm run example
This opens an HTML example using magic bytes as a window variable. It kinda looks like that.
1<input type="file" id="file" />
2
3 <script src="node_modules/magic-bytes.js/dist/browser.js" type="application/javascript"></script>
4<script>
5 document.getElementById("file").addEventListener('change', (event, x) => {
6 const fileReader = new FileReader();
7 fileReader.onloadend = (f) => {
8 const bytes = new Uint8Array(f.target.result);
9 console.log("Possible filetypes: " + filetypeinfo(bytes))
10 }
11 fileReader.readAsArrayBuffer(event.target.files[0])
12 })
13</script>
Tests
Run npm test
Example
See examples/
How does it work
The create-snapshot.js
creates a new tree. The tree has a similar shape to the following
1{ 2 "0x47": { 3 "0x49": { 4 "0x46": { 5 "0x38": { 6 "0x37": { 7 "0x61": { 8 "matches": [ 9 { 10 "typename": "gif", 11 "mime": "image/gif", 12 "extension": "gif" 13 } 14 ] 15 } 16 }, 17 } 18 } 19 } 20 } 21}
It acts as a giant lookup map for the given byte signatures.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: jobLevel 'contents' permission set to 'write': .github/workflows/build-and-test.yml:12
- Warn: no topLevel permission defined: .github/workflows/build-and-test.yml:1
- Info: topLevel 'contents' permission set to 'read': .github/workflows/static-webapp.yml:10
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/static-webapp.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/static-webapp.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/static-webapp.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/static-webapp.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/static-webapp.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/static-webapp.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/static-webapp.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/static-webapp.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/static-webapp.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/LarsKoelpin/magic-bytes/static-webapp.yml/master?enable=pin
- Info: 0 out of 7 GitHub-owned GitHubAction dependencies pinned
- Info: 2 out of 2 npmCommand dependencies pinned
Reason
Found 5/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Reason
24 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-84p7-fh9c-6g8h
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-92r3-m2mg-pj97
- Warn: Project is vulnerable to: GHSA-c24v-8rfc-w8vw
- Warn: Project is vulnerable to: GHSA-8jhw-289h-jh2g
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986 / GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
3.6
/10
Last Scanned on 2025-01-20
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More