Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gabriel-vasile/mimetype
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.4.7
Choose a base ref
...
head repository: gabriel-vasile/mimetype
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.4.8
Choose a head ref
  • 5 commits
  • 11 files changed
  • 3 contributors

Commits on Nov 20, 2024

  1. recover gif.gif (#611)

    I removed it in #603, but this file needs to remain here because it is
    used inside the README.
    gabriel-vasile authored Nov 20, 2024
    Copy the full SHA
    511d137 View commit details

Commits on Dec 19, 2024

  1. zstd: check for skippable frames as well; fix #619 (#621)

    gabriel-vasile authored Dec 19, 2024
    Copy the full SHA
    4657583 View commit details

Commits on Jan 4, 2025

  1. Bump the github-actions group across 1 directory with 2 updates (#622)

    Bumps the github-actions group with 2 updates in the / directory: [actions/setup-go](https://github.com/actions/setup-go) and [github/codeql-action](https://github.com/github/codeql-action).
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Jan 4, 2025
    Copy the full SHA
    040ac4b View commit details
  2. bump golang.org/x/net to v0.33.0 to fix CVE-2024-45338 (#623)

    Signed-off-by: Ashutosh Kumar <ashutosh.kumar@elastic.co>
    sonasingh46 authored Jan 4, 2025
    Copy the full SHA
    8278245 View commit details
  3. Add support for APK; close #345 (#624)

    gabriel-vasile authored Jan 4, 2025
    Copy the full SHA
    66d56c2 View commit details
Showing with 44 additions and 13 deletions.
  1. +1 −1 .github/workflows/benchmark.yml
  2. +2 −2 .github/workflows/codeql.yml
  3. +2 −2 .github/workflows/go.yml
  4. +1 −1 go.mod
  5. +2 −2 go.sum
  6. +8 −3 internal/magic/archive.go
  7. +19 −0 internal/magic/zip.go
  8. +1 −0 mimetype_test.go
  9. +2 −1 supported_mimes.md
  10. BIN testdata/gif.gif
  11. +6 −1 tree.go
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ jobs:
with:
ref: master
- name: Install Go
uses: actions/setup-go@v5.1.0
uses: actions/setup-go@v5.2.0
with:
go-version-file: 'go.mod'

4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -23,10 +23,10 @@ jobs:
uses: actions/checkout@v4.2.2

- name: Initialize CodeQL
uses: github/codeql-action/init@v3.27.1
uses: github/codeql-action/init@v3.28.0
with:
languages: go
queries: security-and-quality

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3.27.1
uses: github/codeql-action/analyze@v3.28.0
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4.2.2
- name: Install Go
uses: actions/setup-go@v5.1.0
uses: actions/setup-go@v5.2.0
with:
go-version-file: 'go.mod'
- name: Run linters
@@ -30,7 +30,7 @@ jobs:
uses: actions/checkout@v4.2.2
- name: Install Go
if: success()
uses: actions/setup-go@v5.1.0
uses: actions/setup-go@v5.2.0
with:
go-version-file: 'go.mod'
- run: go test -race ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ module github.com/gabriel-vasile/mimetype

go 1.20

require golang.org/x/net v0.31.0
require golang.org/x/net v0.33.0

// v1.4.4 had a test file detected as malicious by antivirus software. #575
retract v1.4.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
11 changes: 8 additions & 3 deletions internal/magic/archive.go
Original file line number Diff line number Diff line change
@@ -52,10 +52,15 @@ func InstallShieldCab(raw []byte, _ uint32) bool {
}

// Zstd matches a Zstandard archive file.
// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md
func Zstd(raw []byte, limit uint32) bool {
return len(raw) >= 4 &&
(0x22 <= raw[0] && raw[0] <= 0x28 || raw[0] == 0x1E) && // Different Zstandard versions.
bytes.HasPrefix(raw[1:], []byte{0xB5, 0x2F, 0xFD})
if len(raw) < 4 {
return false
}
sig := binary.LittleEndian.Uint32(raw)
// Check for Zstandard frames and skippable frames.
return (sig >= 0xFD2FB522 && sig <= 0xFD2FB528) ||
(sig >= 0x184D2A50 && sig <= 0x184D2A5F)
}

// CRX matches a Chrome extension file: a zip archive prepended by a package header.
19 changes: 19 additions & 0 deletions internal/magic/zip.go
Original file line number Diff line number Diff line change
@@ -110,3 +110,22 @@ func zipContains(raw, sig []byte, msoCheck bool) bool {
}
return false
}

// APK matches an Android Package Archive.
// The source of signatures is https://github.com/file/file/blob/1778642b8ba3d947a779a36fcd81f8e807220a19/magic/Magdir/archive#L1820-L1887
func APK(raw []byte, _ uint32) bool {
apkSignatures := [][]byte{
[]byte("AndroidManifest.xml"),
[]byte("META-INF/com/android/build/gradle/app-metadata.properties"),
[]byte("classes.dex"),
[]byte("resources.arsc"),
[]byte("res/drawable"),
}
for _, sig := range apkSignatures {
if zipContains(raw, sig, false) {
return true
}
}

return false
}
1 change: 1 addition & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
@@ -262,6 +262,7 @@ var testcases = []testcase{
{"xz", "\xfd7zXZ\x00", "application/x-xz", true},
{"zip", "PK\x03\x04", "application/zip", true},
{"zst", "(\xb5/\xfd", "application/zstd", true},
{"zst skippable frame", "\x50\x2A\x4D\x18", "application/zstd", false},
}

func TestDetect(t *testing.T) {
3 changes: 2 additions & 1 deletion supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 177 Supported MIME types
## 178 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type | Aliases
@@ -11,6 +11,7 @@ Extension | MIME type | Aliases
**.docx** | application/vnd.openxmlformats-officedocument.wordprocessingml.document | -
**.pptx** | application/vnd.openxmlformats-officedocument.presentationml.presentation | -
**.epub** | application/epub+zip | -
**.apk** | application/vnd.android.package-archive | -
**.jar** | application/jar | -
**.odt** | application/vnd.oasis.opendocument.text | application/x-vnd.oasis.opendocument.text
**.ott** | application/vnd.oasis.opendocument.text-template | application/x-vnd.oasis.opendocument.text-template
Binary file added testdata/gif.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
@@ -44,7 +44,11 @@ var (
"application/gzip-compressed", "application/x-gzip-compressed",
"gzip/document")
sevenZ = newMIME("application/x-7z-compressed", ".7z", magic.SevenZ)
zip = newMIME("application/zip", ".zip", magic.Zip, xlsx, docx, pptx, epub, jar, odt, ods, odp, odg, odf, odc, sxc).
// APK must be checked before JAR because APK is a subset of JAR.
// This means APK should be a child of JAR detector, but in practice,
// the decisive signature for JAR might be located at the end of the file
// and not reachable because of library readLimit.
zip = newMIME("application/zip", ".zip", magic.Zip, xlsx, docx, pptx, epub, apk, jar, odt, ods, odp, odg, odf, odc, sxc).
alias("application/x-zip", "application/x-zip-compressed")
tar = newMIME("application/x-tar", ".tar", magic.Tar)
xar = newMIME("application/x-xar", ".xar", magic.Xar)
@@ -57,6 +61,7 @@ var (
pptx = newMIME("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx", magic.Pptx)
epub = newMIME("application/epub+zip", ".epub", magic.Epub)
jar = newMIME("application/jar", ".jar", magic.Jar)
apk = newMIME("application/vnd.android.package-archive", ".apk", magic.APK)
ole = newMIME("application/x-ole-storage", "", magic.Ole, msi, aaf, msg, xls, pub, ppt, doc)
msi = newMIME("application/x-ms-installer", ".msi", magic.Msi).
alias("application/x-windows-installer", "application/x-msi")