Skip to main content
Menu
Advisories

Summary When ag-psd decodes enumerated values from Photoshop descriptors, it looks the file-supplied value up in a plain object without checking that the key is an own property

GHSA-92M5-76FP-R4X7Published Updated GitHub

Description

When ag-psd decodes enumerated values from Photoshop descriptors, it looks the file-supplied value up in a plain object without checking that the key is an own property. If a crafted PSD supplies an enum value that is the name of an inherited property, such as __proto__, constructor, toString or hasOwnProperty, the parser returns Object.prototype or a native function where it should return one of the documented string values.

ag-psd does not modify the returned object itself. But any application that later writes to that field, for example while normalising or annotating parsed data in place, writes to Object.prototype. That pollutes the prototype of every object in the process.

Details

createEnum in src/helpers.ts:

export function createEnum<T extends string>(prefix: string, def: T, map: { [K in T]: string; }) {
	const rev = revMap(map);           // plain object: {} with reversed keys

	const decode = (val: string): T => {
		const value = val.split('.')[1];  // attacker-controlled, arbitrary length
		if (value && !rev[value]) {       // rev['__proto__'] is truthy -> check skipped
			if (Object.prototype.hasOwnProperty.call(map, value)) return value as any;
			const spaced = value.replace(/([A-Z])/g, ' $1').toLowerCase();
			if (Object.prototype.hasOwnProperty.call(map, spaced)) return spaced as any;
			throw new Error(`Unrecognized value for enum: '${val}'`);
		}
		return (rev[value] as any) || def; // returns inherited property
	};
	...
}

rev is created by revMap() as an ordinary object literal, so it inherits from Object.prototype. Descriptor enum values are read with readAsciiStringOrClassId, which accepts strings of any length. That means values like __proto__ and constructor (unlike 4-character OSType keys) can be encoded in a file.

The fallback branch in the same function already uses Object.prototype.hasOwnProperty.call, and the engine data parsers (setProperty in engineData.ts, getKey in engineData2.ts) already guard against inherited keys. The main lookup in decode is the remaining gap.

createEnum is used to decode many fields, including blend modes, layer effect settings, text settings, slices, print settings and animation frame disposal. All of them can be reached from an untrusted PSD.

Proof of concept

A minimal 1×1 RGB PSD with one image resource, Print Information (ID 1082), holding a version-16 descriptor with a single enum item:

Item keyTypeEnum typeEnum value
InteenumInte__proto__

Parsing it:

const { readPsd } = require('ag-psd');
const psd = readPsd(buffer, { useImageData: true });

const ri = psd.imageResources.printInformation.renderingIntent;
console.log(ri === Object.prototype); // true

Results observed for other values:

Enum valuerenderingIntent returned
__proto__Object.prototype
constructorfunction Object() { [native code] }
toStringfunction toString() { [native code] }
hasOwnPropertyfunction hasOwnProperty() { [native code] }

The documented type is a string union ('perceptual' | 'saturation' | 'relative colorimetric' | 'absolute colorimetric').

Example of an application-side write leading to global pollution:

const info = psd.imageResources.printInformation;
if (typeof info.renderingIntent === 'object') info.renderingIntent.flag = true;
console.log(({}).flag); // true, Object.prototype has been modified

Impact

  • Type confusion (always): applications that rely on the TypeScript types and treat enum fields as strings may crash or behave unexpectedly, for example layer.effects.dropShadow[0].blendMode.toUpperCase() throwing, or switch/lookup logic taking unintended branches.
  • Prototype pollution (conditional): if the consuming application, or a framework it passes parsed data to, writes properties onto a decoded enum value, those writes land on Object.prototype. Depending on the application, that can lead to logic bypasses, denial of service or worse. This needs the consuming application to perform such a write; ag-psd does not modify Object.prototype on its own.
  • Write path: a document containing such a value throws when written back with writePsd, because encode does not recognise it. A read → modify → write pipeline can therefore be made to fail on demand.

Applications that parse PSD, PSB or ABR files from untrusted sources (file upload handlers, conversion services, browser-based editors) are exposed to the type confusion in every case.

Affected versions

References

GitHub · Agamnentzar/ag-psd