Selaa lähdekoodia

fix: guard null DOM elements in PasteStatus and I18n after jQuery removal

- I18n.translate: treat null as a missing optional element (jQuery's
  $() silently ignored null; null is not instanceof HTMLElement so it
  was falling through to become the messageId, crashing on .length)
- PasteStatus.createPasteNotification: guard pasteUrl and pasteSuccess
  which can be absent when the function is called from a minimal DOM
  (e.g. extractUrl tests that only need #pastelink)
- PasteStatus.extractUrl: guard shortenButton before classList access,
  matching the existing guard pattern in createPasteNotification

The shortenButton/pasteSuccess null-dereferences caused fast-check to
enter shrinking mode and spawn hundreds of extra iterations, each
reloading privatebin.js via globalThis.cleanup(), exhausting the heap.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
rugk 1 kuukausi sitten
vanhempi
sitoutus
e1b07b180f
1 muutettua tiedostoa jossa 11 lisäystä ja 5 poistoa
  1. 11 5
      js/privatebin.js

+ 11 - 5
js/privatebin.js

@@ -732,8 +732,8 @@ window.PrivateBin = (function () {
                 element = null;
 
             // parse arguments
-            if (args[0] instanceof HTMLElement) {
-                // optional HTMLElement as first parameter
+            if (args[0] === null || args[0] instanceof HTMLElement) {
+                // optional HTMLElement as first parameter (null means element not found)
                 element = args[0];
                 args.shift();
             }
@@ -2006,7 +2006,9 @@ window.PrivateBin = (function () {
             // save newly created element
             pasteUrl = document.getElementById('pasteurl');
             // and add click event
-            pasteUrl.addEventListener('click', pasteLinkClick);
+            if (pasteUrl) {
+                pasteUrl.addEventListener('click', pasteLinkClick);
+            }
 
             // delete link
             const deleteLink = document.getElementById('deletelink');
@@ -2021,7 +2023,9 @@ window.PrivateBin = (function () {
             }
 
             // show result
-            pasteSuccess.classList.remove('hidden');
+            if (pasteSuccess) {
+                pasteSuccess.classList.remove('hidden');
+            }
             // we pre-select the link so that the user only has to [Ctrl]+[c] the link
             Helper.selectText(pasteUrl);
         };
@@ -2072,7 +2076,9 @@ window.PrivateBin = (function () {
                 })[0];
                 if (typeof shortUrl === 'string' && shortUrl.length > 0) {
                     // we disable the button to avoid calling shortener again
-                    shortenButton.classList.add('buttondisabled');
+                    if (shortenButton) {
+                        shortenButton.classList.add('buttondisabled');
+                    }
                     // update link
                     pasteUrl.textContent = shortUrl;
                     pasteUrl.href = shortUrl;