New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

validate-image-type

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validate-image-type

Check the image file of a Buffer/Uint8Array that matched expected image MIME-type.

latest
Source
npmnpm
Version
3.0.0
Version published
Weekly downloads
2.9K
-43.8%
Maintainers
1
Weekly downloads
 
Created
Source

validate-image-type Actions Status

Check the image file of a Buffer/Uint8Array that matched expected image MIME-type.

This library check the file contents instead of file extensions using following:

Features

  • Support various image types: popular images types + SVG
  • Reject File Camouflage by checking actual content buffer

Install

Install with npm:

npm install validate-image-type

Usage

import { validateMIMEType } from "validate-image-type";
const result = await validateMIMEType("./image.png", {
    allowMimeTypes: ['image/jpeg', 'image/gif', 'image/png', 'image/svg+xml']
});
if (!result.ok) {
    console.error(result.error);
    return;
}
console.log("This image is supported!");

See following document about MimeType.

Supported file types

Basic images file types and SVG(image/svg+xml).

"jpg",
"png",
"gif",
"webp",
"flif",
"cr2",
"tif",
"bmp",
"jxr",
"psd",
"ico",
"bpg",
"jp2",
"jpm",
"jpx",
"heic",
"cur",
"dcm",
"svg"

Examples

Integration with Multer middleware.

const multer = require('multer');
const temp_local_img_dir = path.join(__dirname, `/.temp_local_img_dir`);
const upload = multer({ dest: temp_local_img_dir });
const asyncWrapper = fn => {
    return (req, res, next) => {
        return fn(req, res, next).catch(next);
    }
};

app.post(
  '/upload',
  upload.single('image'), 
  asyncWrapper(async (req, res, next) => {
    const validationResult = await validateMIMEType(req.file.path, {
      originalFilename: req.file.originalname,
      allowMimeTypes: ['image/jpeg', 'image/gif', 'image/png', 'image/svg+xml'],
    });
    console.log('validationResult', validationResult);
    if (!validationResult.ok) {
      return res.send(400);
    }
    // uploading task
    // ...
  })
);

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

Author

License

MIT © azu

Keywords

image

FAQs

Package last updated on 22 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts