Skip to content

Commit ec8ade4

Browse files
theosotrchearon
authored andcommitted
Check a type tag instead of instanceof before unwrapping a native object
1 parent 9570be0 commit ec8ade4

10 files changed

Lines changed: 73 additions & 16 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1010
### Changed
1111
### Added
1212
### Fixed
13+
* Check a type tag before unwrapping a native object, so a forged prototype chain can no longer crash the process (#2628)
1314
* Load images from Node.js object URLs (#2525)
1415

1516
3.2.3

‎src/Canvas.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Canvas::Initialize(Napi::Env& env, Napi::Object& exports) {
6363

6464
Canvas::Canvas(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Canvas>(info), env(info.Env()) {
6565
InstanceData* data = env.GetInstanceData<InstanceData>();
66+
info.This().As<Napi::Object>().TypeTag(&data->tags.Canvas);
6667
ctor = Napi::Persistent(data->CanvasCtor.Value());
6768

6869
_surface = nullptr;
@@ -710,6 +711,7 @@ Canvas::getFormat() {
710711

711712
void
712713
Canvas::resurface(Napi::Object This, uint16_t width, uint16_t height) {
714+
InstanceData* data = env.GetInstanceData<InstanceData>();
713715
Napi::Value context;
714716

715717
if (type == CANVAS_TYPE_PDF) {
@@ -722,7 +724,8 @@ Canvas::resurface(Napi::Object This, uint16_t width, uint16_t height) {
722724
this->width = width;
723725
this->height = height;
724726
ensureSurface();
725-
if (This.Get("context").UnwrapTo(&context) && context.IsObject()) {
727+
if (This.Get("context").UnwrapTo(&context) && context.IsObject()
728+
&& context.As<Napi::Object>().CheckTypeTag(&data->tags.Context2d)) {
726729
// Reset context
727730
Context2d *context2d = Context2d::Unwrap(context.As<Napi::Object>());
728731
cairo_t *prev = context2d->context();

‎src/CanvasGradient.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Gradient::Initialize(Napi::Env& env, Napi::Object& exports) {
2727
*/
2828

2929
Gradient::Gradient(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Gradient>(info), env(info.Env()) {
30+
InstanceData* data = env.GetInstanceData<InstanceData>();
31+
info.This().As<Napi::Object>().TypeTag(&data->tags.Gradient);
3032
// Linear
3133
if (
3234
4 == info.Length() &&

‎src/CanvasPattern.cc‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ Pattern::Initialize(Napi::Env& env, Napi::Object& exports) {
2929
*/
3030

3131
Pattern::Pattern(const Napi::CallbackInfo& info) : ObjectWrap<Pattern>(info), env(info.Env()) {
32+
InstanceData* data = env.GetInstanceData<InstanceData>();
33+
info.This().As<Napi::Object>().TypeTag(&data->tags.Pattern);
34+
3235
if (!info[0].IsObject()) {
3336
Napi::TypeError::New(env, "Image or Canvas expected").ThrowAsJavaScriptException();
3437
return;
3538
}
3639

3740
Napi::Object obj = info[0].As<Napi::Object>();
38-
InstanceData* data = env.GetInstanceData<InstanceData>();
3941

4042
// Image
41-
if (obj.InstanceOf(data->ImageCtor.Value()).UnwrapOr(false)) {
43+
if (obj.CheckTypeTag(&data->tags.Image)) {
4244
image = Image::Unwrap(obj);
4345
if (!image->isComplete()) {
4446
Napi::Error::New(env, "Image given has not completed loading").ThrowAsJavaScriptException();
@@ -47,7 +49,7 @@ Pattern::Pattern(const Napi::CallbackInfo& info) : ObjectWrap<Pattern>(info), en
4749
value = Napi::Persistent(obj);
4850

4951
// Canvas
50-
} else if (obj.InstanceOf(data->CanvasCtor.Value()).UnwrapOr(false)) {
52+
} else if (obj.CheckTypeTag(&data->tags.Canvas)) {
5153
canvas = Canvas::Unwrap(obj);
5254
value = Napi::Persistent(obj);
5355
// Invalid

‎src/CanvasRenderingContext2d.cc‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ Context2d::Initialize(Napi::Env& env, Napi::Object& exports) {
168168

169169
Context2d::Context2d(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Context2d>(info), env(info.Env()) {
170170
InstanceData* data = env.GetInstanceData<InstanceData>();
171+
info.This().As<Napi::Object>().TypeTag(&data->tags.Context2d);
171172

172173
if (!info[0].IsObject()) {
173174
Napi::TypeError::New(env, "Canvas expected").ThrowAsJavaScriptException();
174175
return;
175176
}
176177

177178
Napi::Object obj = info[0].As<Napi::Object>();
178-
if (!obj.InstanceOf(data->CanvasCtor.Value()).UnwrapOr(false)) {
179+
if (!obj.CheckTypeTag(&data->tags.Canvas)) {
179180
if (!env.IsExceptionPending()) {
180181
Napi::TypeError::New(env, "Canvas expected").ThrowAsJavaScriptException();
181182
}
@@ -761,7 +762,7 @@ Context2d::PutImageData(const Napi::CallbackInfo& info) {
761762
}
762763
Napi::Object obj = info[0].As<Napi::Object>();
763764
InstanceData* data = env.GetInstanceData<InstanceData>();
764-
if (!obj.InstanceOf(data->ImageDataCtor.Value()).UnwrapOr(false)) {
765+
if (!obj.CheckTypeTag(&data->tags.ImageData)) {
765766
if (!env.IsExceptionPending()) {
766767
Napi::TypeError::New(env, "ImageData expected").ThrowAsJavaScriptException();
767768
}
@@ -1259,7 +1260,9 @@ Context2d::DrawImage(const Napi::CallbackInfo& info) {
12591260
Napi::Object obj = info[0].As<Napi::Object>();
12601261

12611262
// Image
1262-
if (obj.InstanceOf(env.GetInstanceData<InstanceData>()->ImageCtor.Value()).UnwrapOr(false)) {
1263+
InstanceData* data = env.GetInstanceData<InstanceData>();
1264+
1265+
if (obj.CheckTypeTag(&data->tags.Image)) {
12631266
Image *img = Image::Unwrap(obj);
12641267
if (!img->isComplete()) {
12651268
Napi::Error::New(env, "Image given has not completed loading").ThrowAsJavaScriptException();
@@ -1270,7 +1273,7 @@ Context2d::DrawImage(const Napi::CallbackInfo& info) {
12701273
surface = img->surface.surface();
12711274

12721275
// Canvas
1273-
} else if (obj.InstanceOf(env.GetInstanceData<InstanceData>()->CanvasCtor.Value()).UnwrapOr(false)) {
1276+
} else if (obj.CheckTypeTag(&data->tags.Canvas)) {
12741277
Canvas *canvas = Canvas::Unwrap(obj);
12751278
source_w = sw = canvas->getWidth();
12761279
source_h = sh = canvas->getHeight();
@@ -1903,9 +1906,9 @@ Context2d::SetFillStyle(const Napi::CallbackInfo& info, const Napi::Value& value
19031906
} else if (value.IsObject()) {
19041907
InstanceData *data = env.GetInstanceData<InstanceData>();
19051908
Napi::Object obj = value.As<Napi::Object>();
1906-
if (obj.InstanceOf(data->CanvasGradientCtor.Value()).UnwrapOr(false)) {
1909+
if (obj.CheckTypeTag(&data->tags.Gradient)) {
19071910
state->fillGradient.Reset(obj);
1908-
} else if (obj.InstanceOf(data->CanvasPatternCtor.Value()).UnwrapOr(false)) {
1911+
} else if (obj.CheckTypeTag(&data->tags.Pattern)) {
19091912
state->fillPattern.Reset(obj);
19101913
}
19111914
}
@@ -1940,9 +1943,9 @@ Context2d::SetStrokeStyle(const Napi::CallbackInfo& info, const Napi::Value& val
19401943
} else if (value.IsObject()) {
19411944
InstanceData *data = env.GetInstanceData<InstanceData>();
19421945
Napi::Object obj = value.As<Napi::Object>();
1943-
if (obj.InstanceOf(data->CanvasGradientCtor.Value()).UnwrapOr(false)) {
1946+
if (obj.CheckTypeTag(&data->tags.Gradient)) {
19441947
state->strokeGradient.Reset(obj);
1945-
} else if (obj.InstanceOf(data->CanvasPatternCtor.Value()).UnwrapOr(false)) {
1948+
} else if (obj.CheckTypeTag(&data->tags.Pattern)) {
19461949
state->strokePattern.Reset(obj);
19471950
}
19481951
}

‎src/FontFace.cc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ FontFace::FontFace(const Napi::CallbackInfo& info) :
1414
id(++lastId),
1515
env(info.Env())
1616
{
17+
InstanceData* data = env.GetInstanceData<InstanceData>();
18+
info.This().As<Napi::Object>().TypeTag(&data->tags.FontFace);
19+
1720
if (info.Length() < 2) {
1821
Napi::TypeError::New(env, "Family and source arguments are required").ThrowAsJavaScriptException();
1922
return;

‎src/FontFaceSet.cc‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ FontFaceSet::FontFaceSet(Napi::CallbackInfo& info) :
99
env(info.Env()),
1010
ready(Napi::Promise::Deferred::New(info.Env())),
1111
Napi::ObjectWrap<FontFaceSet>(info) {
12+
InstanceData* data = env.GetInstanceData<InstanceData>();
13+
info.This().As<Napi::Object>().TypeTag(&data->tags.FontFaceSet);
1214
}
1315

1416
void
@@ -52,7 +54,7 @@ FontFaceSet::Add(const Napi::CallbackInfo& info) {
5254
isFontFace = false;
5355
} else {
5456
obj = info[0].As<Napi::Object>();
55-
if (!obj.InstanceOf(data->FontFaceCtor.Value()).UnwrapTo(&isFontFace)) return env.Undefined();
57+
isFontFace = obj.CheckTypeTag(&data->tags.FontFace);
5658
}
5759
if (!isFontFace) {
5860
Napi::TypeError::New(env, "Expected instance of FontFace").ThrowAsJavaScriptException();
@@ -85,7 +87,7 @@ FontFaceSet::Has(const Napi::CallbackInfo& info) {
8587
isFontFace = false;
8688
} else {
8789
obj = info[0].As<Napi::Object>();
88-
if (!obj.InstanceOf(data->FontFaceCtor.Value()).UnwrapTo(&isFontFace)) return env.Undefined();
90+
isFontFace = obj.CheckTypeTag(&data->tags.FontFace);
8991
}
9092
if (!isFontFace) {
9193
Napi::TypeError::New(env, "Expected instance of FontFace").ThrowAsJavaScriptException();
@@ -124,7 +126,7 @@ FontFaceSet::Delete(const Napi::CallbackInfo& info) {
124126
isFontFace = false;
125127
} else {
126128
obj = info[0].As<Napi::Object>();
127-
if (!obj.InstanceOf(data->FontFaceCtor.Value()).UnwrapTo(&isFontFace)) return env.Undefined();
129+
isFontFace = obj.CheckTypeTag(&data->tags.FontFace);
128130
}
129131
if (!isFontFace) {
130132
Napi::TypeError::New(env, "Expected instance of FontFace").ThrowAsJavaScriptException();

‎src/Image.cc‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,8 @@ Image::Initialize(Napi::Env& env, Napi::Object& exports) {
14881488
*/
14891489

14901490
Image::Image(const Napi::CallbackInfo& info) : ObjectWrap<Image>(info), env(info.Env()) , surface(env) {
1491+
InstanceData* data = env.GetInstanceData<InstanceData>();
1492+
info.This().As<Napi::Object>().TypeTag(&data->tags.Image);
14911493
info.This().ToObject().Unwrap().Set("onload", env.Null());
14921494
info.This().ToObject().Unwrap().Set("onerror", env.Null());
14931495
}
@@ -1586,7 +1588,14 @@ Image::SetHeight(const Napi::CallbackInfo& info, const Napi::Value& value) {
15861588
Napi::Value
15871589
Image::GetSource(const Napi::CallbackInfo& info){
15881590
Napi::Env env = info.Env();
1589-
Image *img = Image::Unwrap(info.This().As<Napi::Object>());
1591+
Napi::Object This = info.This().As<Napi::Object>();
1592+
InstanceData* data = env.GetInstanceData<InstanceData>();
1593+
1594+
if (!This.CheckTypeTag(&data->tags.Image)) {
1595+
Napi::TypeError::New(env, "Image expected").ThrowAsJavaScriptException();
1596+
return env.Undefined();
1597+
}
1598+
Image *img = Image::Unwrap(This);
15901599
return Napi::String::New(env, img->surface.filename ? img->surface.filename : "");
15911600
}
15921601

@@ -1598,6 +1607,12 @@ void
15981607
Image::SetSource(const Napi::CallbackInfo& info){
15991608
Napi::Env env = info.Env();
16001609
Napi::Object This = info.This().As<Napi::Object>();
1610+
InstanceData* data = env.GetInstanceData<InstanceData>();
1611+
1612+
if (!This.CheckTypeTag(&data->tags.Image)) {
1613+
Napi::TypeError::New(env, "Image expected").ThrowAsJavaScriptException();
1614+
return;
1615+
}
16011616
Image *img = Image::Unwrap(This);
16021617
cairo_status_t status = CAIRO_STATUS_READ_ERROR;
16031618

‎src/ImageData.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ ImageData::Initialize(Napi::Env& env, Napi::Object& exports) {
2525
*/
2626

2727
ImageData::ImageData(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ImageData>(info), env(info.Env()) {
28+
InstanceData* data = env.GetInstanceData<InstanceData>();
29+
info.This().As<Napi::Object>().TypeTag(&data->tags.ImageData);
2830
Napi::TypedArray dataArray;
2931
uint32_t width;
3032
uint32_t height;

‎src/InstanceData.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <napi.h>
4+
#include <cstdint>
5+
#include <random>
46
#include <freetype/freetype.h>
57
#include "FontFaceSet.h"
68
#ifdef _WIN32
@@ -25,7 +27,29 @@ struct FT_Wrapper {
2527
}
2628
};
2729

30+
// Per-class type tags; applied on construction and checked before napi_unwrap.
31+
struct TypeTags {
32+
napi_type_tag Canvas;
33+
napi_type_tag Context2d;
34+
napi_type_tag Image;
35+
napi_type_tag ImageData;
36+
napi_type_tag Gradient;
37+
napi_type_tag Pattern;
38+
napi_type_tag FontFace;
39+
napi_type_tag FontFaceSet;
40+
41+
TypeTags() {
42+
std::random_device rd;
43+
for (napi_type_tag* tag : {&Canvas, &Context2d, &Image, &ImageData,
44+
&Gradient, &Pattern, &FontFace, &FontFaceSet}) {
45+
tag->lower = (static_cast<uint64_t>(rd()) << 32) | rd();
46+
tag->upper = (static_cast<uint64_t>(rd()) << 32) | rd();
47+
}
48+
}
49+
};
50+
2851
struct InstanceData {
52+
TypeTags tags;
2953
Napi::FunctionReference CanvasCtor;
3054
Napi::FunctionReference CanvasGradientCtor;
3155
Napi::FunctionReference DOMMatrixCtor;

0 commit comments

Comments
 (0)