
Security News
/Research
Popular node-ipc npm Package Infected with Credential Stealer
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.
is-real-image
Advanced tools
is-real-image is a lightweight and efficient Node.js package that allows you to verify if a file is a real image based on its extension, signature (magic number), or both. This package ensures that the provided image file is valid by checking the file's e
is-real-image is a lightweight and efficient Node.js package that allows you to verify if a file is a real image based on its extension, signature (magic number), or both. This package ensures that the provided image file is valid by checking the file's extension, inspecting the file's binary signature, or combining both methods for extra security.
.png, .jpeg, .gif, etc.npm install is-real-image
import fs from "fs";
import { isRealImage } from "../src/index";
const assets = path.join(__dirname, "assets");
const imagePath = path.join(assets, "image.png");
function main() {
const buffer = fs.readFileSync(imagePath);
const result = await isRealImage(buffer) // { mime: "image/png", fileExt: "png", realExt: "png"};
}
import path from "path";
import { isRealImage } from "../src/index";
const assets = path.join(__dirname, "assets");
async function main() {
// Path to the image with incorrect extension (PNG image saved as .webp)
const imagePath = path.join(assets, "image.webp");
// Option 1: Validate using the file signature (magic numbers) only.
// This method ignores the file extension and checks the internal binary data (signature)
// to determine the actual image format.
const pngInfo = await isRealImage({
input: imagePath,
check: "header-only"
});
console.log(pngInfo);
// Output: { mime: "image/png", fileExt: "webp", realExt: "png" }
// This indicates that the file has a .webp extension, but is actually a PNG image.
// Option 2: Validate using the file extension only.
// This method checks if the file extension (.webp) is a valid image extension.
const isImage = await isRealImage({
input: imagePath,
check: "extension-only"
});
console.log(isImage);
// Output: true
// The .webp extension is recognized as a valid image extension,
// regardless of the file's actual content.
// Option 3: Perform a full validation, checking both the file signature and extension.
// This method ensures that both the extension and the actual content match.
const imageInfo = await isRealImage({
input: imagePath,
check: "full-check"
});
console.log(imageInfo);
// Mismatch Output: null
// Expected Output: { mime: "image/png", fileExt: "png", realExt: "png" }
// This confirms a mismatch: the file extension (webp) does not match the actual format (png).
// Image MIME types with identical signatures are treated as equivalent (e.g., JPEG and JPG).
};
main();
BufferBuffer, you can directly pass it to the isRealImage function. This skips the need to read the file from disk and checks the signature of the image based on its binary content.{ input, check }input (required)stringcheck (optional)"extension-only" | "header-only" | "full-check""header-only""extension-only": Checks if the file has a valid image extension (e.g., .png, .jpeg). Does not inspect the actual file contents."header-only": Checks the file's binary signature (magic number) to determine the file type, ignoring the file extension."full-check": Performs both extension and header validation, checking that the file extension matches its actual contents.allowedTypes (optional)Set<ImageExt>undefinedThis package offers two methods for verifying image file types:
import { isRealImage, isRealImageSync } from "is-real-image";
The async method allows you to check if a file is a valid image using either the file extension, the file signature (magic number), or both. This method is non-blocking and processes the file as a stream, making it efficient for larger files and applications that require high concurrency.
The sync method provides a similar functionality to the async method but in a synchronous, blocking manner. This method reads the file in one go (not as a stream) and processes the image's extension and/or signature. It is suitable for scenarios where performance is less critical, or where you need to process smaller files quickly.
FAQs
is-real-image is a lightweight and efficient Node.js package that allows you to verify if a file is a real image based on its extension, signature (magic number), or both. This package ensures that the provided image file is valid by checking the file's e
The npm package is-real-image receives a total of 25 weekly downloads. As such, is-real-image popularity was classified as not popular.
We found that is-real-image demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
/Research
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.

Security News
TeamPCP and BreachForums are promoting a Shai-Hulud supply chain attack contest with a $1,000 prize for the biggest package compromise.

Security News
Packagist urges PHP projects to update Composer after a GitHub token format change exposed some GitHub Actions tokens in CI logs.