Skip to content
Draft
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
31 changes: 28 additions & 3 deletions lib/features/copy-paste/CopyPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
this._modeling = modeling;
this._mouse = mouse;
this._rules = rules;
this._localStorageKey = 'djs-clipboard';
this._sessionId = Math.random().toString(36).substring(2, 9);

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.
Copy link
Member Author

Choose a reason for hiding this comment

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

will strengthen this if the approach will be validated


eventBus.on('copyPaste.copyElement', function(context) {
var descriptor = context.descriptor,
Expand Down Expand Up @@ -198,9 +200,16 @@
// to copy was empty.
this._clipboard.set(tree);

console.log('Tree prior serialization', tree);

localStorage.setItem(this._localStorageKey, JSON.stringify({
sessionId: this._sessionId,
tree
}));

this._eventBus.fire('copyPaste.elementsCopied', {
elements: elements,
tree: tree
tree
});

return tree;
Expand All @@ -215,9 +224,21 @@
* @param {Object} [context.hints] The optional hints.
*/
CopyPaste.prototype.paste = function(context) {
var tree = this._clipboard.get();
const sessionClipboard = localStorage.getItem(this._localStorageKey);
let tree = this._clipboard.get();
const { sessionId, tree: sessionTree } = JSON.parse(sessionClipboard);

if (sessionClipboard) {
if (sessionId !== this._sessionId) {
console.log('Pasting from different session', sessionId, this._sessionId);
console.log(sessionTree);
tree = sessionTree;

}
}

if (this._clipboard.isEmpty()) {
if (this._clipboard.isEmpty() && !tree) {
console.log('Clipboard is empty, aborting paste');
return;
}

Expand All @@ -229,6 +250,8 @@

var elements = this._createElements(tree);

console.log('Pasting elements', elements, 'with tree', tree);

// paste directly
if (context && context.element && context.point) {
return this._paste(elements, context.element, context.point, hints);
Expand Down Expand Up @@ -351,6 +374,8 @@

});

console.log('returning elements from tree', elements);

return elements;
};

Expand Down
Loading