MEDIUM: Missing error handling for canvas.toDataURL (SecurityError risk) #5

Closed
opened 2026-05-09 05:10:03 +00:00 by Mark · 0 comments
Owner

Severity: MEDIUM

Location

tokengenerator.html, lines 381-388:

downloadBtn.addEventListener('click', () => {
    const link = document.createElement('a');
    const textPart = textInput.value.replace(/\s+/g, '_') || 'token';
    const iconPart = generatedIconImage ? 'ai_generated' : iconSelect.value;
    link.download = `token_${textPart}_${iconPart}.png`;
    link.href = canvas.toDataURL('image/png');
    link.click();
});

Description

canvas.toDataURL() throws a SecurityError if the canvas has been "tainted" by drawing cross-origin images (e.g., through the AI-generated icon loaded from a data URI or if third-party content is drawn). There is no try/catch around this call.

If the canvas is tainted, the download silently fails with a JavaScript error in console, and no feedback is given to the user.

  • Wrap toDataURL() in a try/catch block.
  • Display a user-friendly error message if the canvas cannot be exported.
  • Consider using canvas.toBlob() with proper error handling as an alternative.

Impact

Users may attempt to download tokens but get a silent failure with no feedback.

## Severity: MEDIUM ## Location `tokengenerator.html`, lines 381-388: ```javascript downloadBtn.addEventListener('click', () => { const link = document.createElement('a'); const textPart = textInput.value.replace(/\s+/g, '_') || 'token'; const iconPart = generatedIconImage ? 'ai_generated' : iconSelect.value; link.download = `token_${textPart}_${iconPart}.png`; link.href = canvas.toDataURL('image/png'); link.click(); }); ``` ## Description `canvas.toDataURL()` throws a `SecurityError` if the canvas has been "tainted" by drawing cross-origin images (e.g., through the AI-generated icon loaded from a data URI or if third-party content is drawn). There is no try/catch around this call. If the canvas is tainted, the download silently fails with a JavaScript error in console, and no feedback is given to the user. ### Recommended Fix - Wrap `toDataURL()` in a try/catch block. - Display a user-friendly error message if the canvas cannot be exported. - Consider using `canvas.toBlob()` with proper error handling as an alternative. ### Impact Users may attempt to download tokens but get a silent failure with no feedback.
Mark closed this issue 2026-05-09 08:02:28 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Mark/tokengenerator#5
No description provided.