Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): safeguard stack operations from foreign objects #9772

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(): safeguard stack operations from foreign objects [#9772](https://github.com/fabricjs/fabric.js/pull/9772)
- feat(LayoutManager): Handle the case of activeSelection with objects inside different groups [#9651](https://github.com/fabricjs/fabric.js/pull/9651)

## [6.0.0-beta20]
Expand Down
41 changes: 14 additions & 27 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
object: FabricObject,
index: number,
array: FabricObject[]
) => any

Check warning on line 96 in src/Collection.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
) {
this.getObjects().forEach((object, index, objects) =>
callback(object, index, objects)
Expand Down Expand Up @@ -176,7 +176,7 @@
* @returns {boolean} true if change occurred
*/
sendObjectToBack(object: FabricObject) {
if (!object || object === this._objects[0]) {
if (object === this._objects[0] || !this._objects.includes(object)) {
return false;
}
removeFromArray(this._objects, object);
Expand All @@ -192,7 +192,10 @@
* @returns {boolean} true if change occurred
*/
bringObjectToFront(object: FabricObject) {
if (!object || object === this._objects[this._objects.length - 1]) {
if (
object === this._objects[this._objects.length - 1] ||
!this._objects.includes(object)
) {
return false;
}
removeFromArray(this._objects, object);
Expand All @@ -212,11 +215,8 @@
* @returns {boolean} true if change occurred
*/
sendObjectBackwards(object: FabricObject, intersecting?: boolean) {
if (!object) {
return false;
}
const idx = this._objects.indexOf(object);
if (idx !== 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here for example when idx === -1

if (idx > 0) {
// if object is not on the bottom of stack
const newIdx = this.findNewLowerIndex(object, idx, intersecting);
removeFromArray(this._objects, object);
Expand All @@ -238,11 +238,8 @@
* @returns {boolean} true if change occurred
*/
bringObjectForward(object: FabricObject, intersecting?: boolean) {
if (!object) {
return false;
}
const idx = this._objects.indexOf(object);
if (idx !== this._objects.length - 1) {
if (idx > -1 && idx < this._objects.length - 1) {
// if object is not on top of stack (last item in an array)
const newIdx = this.findNewUpperIndex(object, idx, intersecting);
removeFromArray(this._objects, object);
Expand All @@ -260,7 +257,7 @@
* @returns {boolean} true if change occurred
*/
moveObjectTo(object: FabricObject, index: number) {
if (object === this._objects[index]) {
if (object === this._objects[index] || !this._objects.includes(object)) {
return false;
}
removeFromArray(this._objects, object);
Expand All @@ -274,45 +271,35 @@
idx: number,
intersecting?: boolean
) {
let newIdx;

if (intersecting) {
newIdx = idx;
// traverse down the stack looking for the nearest intersecting object
for (let i = idx - 1; i >= 0; --i) {
if (object.isOverlapping(this._objects[i])) {
newIdx = i;
break;
return i;
}
}
return idx;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is desired or should it fallback to

Suggested change
return idx;
return idx - 1;

} else {
newIdx = idx - 1;
return idx - 1;
}

return newIdx;
}

findNewUpperIndex(
object: FabricObject,
idx: number,
intersecting?: boolean
) {
let newIdx;

if (intersecting) {
newIdx = idx;
// traverse up the stack looking for the nearest intersecting object
for (let i = idx + 1; i < this._objects.length; ++i) {
if (object.isOverlapping(this._objects[i])) {
newIdx = i;
break;
return i;
}
}
return idx;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same but

Suggested change
return idx;
return idx + 1;

} else {
newIdx = idx + 1;
return idx + 1;
}

return newIdx;
}

/**
Expand Down
Loading