createImageData() with negative width and height aborts the process

已关闭 适合新手
#2,633 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
backend, web-dev

调研方向

先阅读 issue 中提到的 Context2d::CreateImageData,并追踪它在分配缓冲区之前如何转换宽度和高度。将其行为与引用的 Canvas 规范进行比较,并复现所提供脚本中的两个负尺寸情况。完成标准是这两种情况都返回正尺寸,且不会抛出异常或中止;为这些情况添加或运行有针对性的测试。

由索引模型根据 Issue 内容生成。

描述

Version of node-canvas

v3.2.3, v4.0.0-rc3 (master 6cc138b)

Version of node

v26.13.0

What operating system does the problem occur on?

Linux

Steps to reproduce

ctx.createImageData(sw, sh) should use the absolute values of sw and sh. In node-canvas a negative size is read as a huge unsigned number: one negative argument throws, and two negative arguments abort Node with a fatal out-of-memory error that cannot be caught.

const { createCanvas } = require('canvas');
const ctx = createCanvas(10, 10).getContext('2d');
try { const d = ctx.createImageData(-3, 2); console.log(d.width, d.height); } catch (e) { console.log('threw:', e.message); }
const d = ctx.createImageData(-3, -2);
console.log(d.width, d.height);

Run node repro.js; echo $?:

Actual:

threw: buffer exceeds 2147483647 bytes
FATAL ERROR: v8::ArrayBuffer::New Allocation failed - process out of memory
...
134

Expected (matches Chrome 153):

3 2
3 2
Additional information/comments

Per https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createimagedata, createImageData(sw, sh) must initialize the ImageData object with the absolute magnitude of sw and sh. In Context2d::CreateImageData, the arguments are converted with Uint32Value(), turning -3 into 4294967293; with two negative values Bpp * width * height overflows int64_t, bypasses the nBytes > INT32_MAX check, and passes a negative nBytes to Napi::ArrayBuffer::New.

Credit: https://ucsbplse.github.io/

主要语言
JavaScript
星标
10.7k
派生
1.2k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Automattic/node-canvas 的其他 Issue

查看 Automattic/node-canvas 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。