Installations
npm install @labelbox/magic-bytes.js
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.14.2
NPM Version
8.5.0
Score
75.7
Supply Chain
97.1
Quality
82.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
31,603
Last Day
2
Last Week
12
Last Month
41
Last Year
5,187
GitHub Statistics
136 Stars
213 Commits
26 Forks
1 Watching
7 Branches
14 Contributors
Bundle Size
14.70 kB
Minified
3.17 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.15
Package Id
@labelbox/magic-bytes.js@1.0.15
Unpacked Size
209.03 kB
Size
16.91 kB
File Count
29
NPM Version
8.5.0
Node Version
16.14.2
Total Downloads
Cumulative downloads
Total Downloads
31,603
Last day
0%
2
Compared to previous day
Last week
100%
12
Compared to previous week
Last month
78.3%
41
Compared to previous month
Last year
-70.7%
5,187
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. The procedure is based on https://en.wikipedia.org/wiki/List_of_file_signatures.
Installation
Run npm install magic-bytes.js
Usage
On server:
1import filetype from 'magic-bytes.js' 2 3filetype(fs.readFileSync("myimage.png")) // ["png"]
Using HTML:
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>
API
The following functions are availble:
filetypeinfo(bytes: number[])
Contains typeinformation like name, extension and mime type:[{typename: "zip"}, {typename: "jar"}]
filetypenames(bytes: number[])
: Contains type names only:["zip", "jar"]
filetypemime(bytes: number[])
: Contains type mime types only:["application/zip", "application/jar"]
filetypeextensions(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.
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. To check all available entries, have a look at pattern-tree.js
and its
generated pattern-tree.snapshot
, which acts as a static resource.
Supported types
Please refer to src/pattern-tree.js
Roadmap
- Specialize type detection(like zip) using offset-subtrees
- Add encoding detection
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
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
no effort to earn an OpenSSF best practices badge detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
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
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
25 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-vg6x-rcgg-rjx6
- 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-27
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