From 97d8a92408a0ba3cb16417dbca7c7b080db66fe2 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Feb 2026 21:36:08 +0000 Subject: [PATCH] Fix JS code quality and safety issues in fossbilling.js (#3277) * Fix JS code quality issues in fossbilling.js Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> * Refactor complex clipboard handler into focused helper functions Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --- .../admin_default/assets/fossbilling.js | 156 +++++++++++------- 1 file changed, 98 insertions(+), 58 deletions(-) diff --git a/src/themes/admin_default/assets/fossbilling.js b/src/themes/admin_default/assets/fossbilling.js index 809af030b..fd1c703ae 100644 --- a/src/themes/admin_default/assets/fossbilling.js +++ b/src/themes/admin_default/assets/fossbilling.js @@ -2,7 +2,7 @@ import './js/ui/modals'; import { initAvatars } from './js/avatar.js'; import { coloris, init } from '@melloware/coloris'; import * as tabler from '@tabler/core/js/tabler.js'; -import './js/tomselect' +import './js/tomselect'; import './js/datepicker' import ApexCharts from 'apexcharts'; import './js/ui/theme_settings'; @@ -19,14 +19,104 @@ coloris({ }); +/** + * Extracts text from the clipboard target element referenced by the button. + * Returns null if the selector is missing, invalid, or the element is not found. + */ +function getClipboardTargetText(button) { + const targetSelector = button.dataset.clipboardTarget; + if (typeof targetSelector !== 'string') return null; + + // Only allow simple ID selectors such as "#element-id" + if (!/^#[A-Za-z0-9_\-.]+$/.test(targetSelector)) { + return null; + } + + const targetElement = document.querySelector(targetSelector); + if (!targetElement) return null; + + return targetElement.value || targetElement.textContent; +} + +/** + * Attempts to copy the given text to the clipboard. + * Falls back to a legacy execCommand approach when the Clipboard API is unavailable. + */ +async function copyTextToClipboard(text) { + if (navigator.clipboard?.writeText) { + try { + await navigator.clipboard.writeText(text); + return true; + } catch (err) { + // Fall through to legacy fallback + } + } + + let textarea; + try { + textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); + return document.execCommand('copy'); + } catch (err) { + return false; + } finally { + if (textarea && textarea.parentNode) { + textarea.parentNode.removeChild(textarea); + } + } +} + +/** + * Shows a "Copied" tooltip on success or an error message on failure. + */ +function handleClipboardResult(button, success) { + if (success) { + let tooltip = bootstrap.Tooltip.getInstance(button); + if (!tooltip) { + tooltip = new bootstrap.Tooltip(button, { trigger: 'manual' }); + } + + const originalTitle = button.dataset.bsOriginalTitle; + button.dataset.bsOriginalTitle = 'Copied'; + tooltip.show(); + setTimeout(() => { + button.dataset.bsOriginalTitle = originalTitle; + tooltip.hide(); + }, 2000); + } else { + if (typeof FOSSBilling !== 'undefined' && FOSSBilling.message) { + FOSSBilling.message('Failed to copy to clipboard', 'error'); + } + } +} + document.addEventListener('DOMContentLoaded', () => { initAvatars(); document.querySelectorAll('.js-theme-toggler').forEach(element => { element.addEventListener('click', event => { event.preventDefault(); - localStorage.setItem('theme', element.getAttribute('href').split('=')[1]); - document.documentElement.setAttribute("data-bs-theme", localStorage.getItem('theme')) + const href = element.getAttribute('href') || ''; + let theme = null; + + // Try to extract theme value safely from href + if (href.includes('=')) { + const parts = href.split('='); + if (parts.length > 1 && parts[1]) { + theme = parts[1]; + } + } + + if (!theme) { + return; + } + + localStorage.setItem('theme', theme); + document.documentElement.setAttribute('data-bs-theme', theme); }); }); @@ -34,7 +124,7 @@ document.addEventListener('DOMContentLoaded', () => { /** * Enable Bootstrap Tooltip */ - const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')); + const tooltipTriggerList = Array.from(document.querySelectorAll('[data-bs-toggle="tooltip"]')); tooltipTriggerList.forEach(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl, { 'trigger': 'hover' @@ -49,60 +139,10 @@ document.addEventListener('DOMContentLoaded', () => { const button = event.target.closest('.clipboard-copy'); if (!button) return; - const targetSelector = button.dataset.clipboardTarget; - const targetElement = document.querySelector(targetSelector); - if (!targetElement) return; + const text = getClipboardTargetText(button); + if (text === null) return; - const text = targetElement.value || targetElement.textContent || targetElement.innerText; - let success = false; - - if (navigator.clipboard?.writeText) { - try { - await navigator.clipboard.writeText(text); - success = true; - } catch (err) { - // Fall through to legacy fallback - } - } - - if (!success) { - let textarea; - try { - textarea = document.createElement('textarea'); - textarea.value = text; - textarea.style.position = 'fixed'; - textarea.style.opacity = '0'; - document.body.appendChild(textarea); - textarea.select(); - success = document.execCommand('copy'); - } catch (err) { - // Fall through to error - } finally { - if (textarea && textarea.parentNode) { - textarea.parentNode.removeChild(textarea); - } - } - } - - if (success) { - let tooltip = bootstrap.Tooltip.getInstance(button); - if (!tooltip) { - tooltip = new bootstrap.Tooltip(button, { - trigger: 'manual' - }); - } - - const originalTitle = button.dataset.bsOriginalTitle; - button.dataset.bsOriginalTitle = 'Copied'; - tooltip.show(); - setTimeout(() => { - button.dataset.bsOriginalTitle = originalTitle; - tooltip.hide(); - }, 2000); - } else { - if (typeof FOSSBilling !== 'undefined' && FOSSBilling.message) { - FOSSBilling.message('Failed to copy to clipboard', 'error'); - } - } + const success = await copyTextToClipboard(text); + handleClipboardResult(button, success); }); });