CVE-2023-51447 (JavaScript, published 2024-02-20T18:15Z)
Description: value: Decidim is a participatory democracy framework. Starting in version 0.27.0 and prior to versions 0.27.5 and 0.28.0, the dynamic file upload feature is subject to potential cross-site scripting attacks in case the attacker manages to modify the file names of the records being uploaded to the server. This appears in sections where the user controls the file upload dialogs themselves and has the technical knowledge to change the file names through the dynamic upload endpoint. Therefore I believe it would require the attacker to control the whole session of the particular user but in any case, this needs to be fixed. Successful exploit of this vulnerability would require the user to have successfully uploaded a file blob to the server with a malicious file name and then have the possibility to direct the other user to the edit page of the record where the attachment is attached. The users are able to craft the direct upload requests themselves controlling the file name that gets stored to the database. The attacker is able to change the filename e.g. to `<svg onload=alert('XSS')>` if they know how to craft these requests themselves. And then enter the returned blob ID to the form inputs manually by modifying the edit page source. Versions 0.27.5 and 0.28.0 contain a patch for this issue. As a workaround, disable dynamic uploads for the instance, e.g. from proposals.
Severity: nan (CVSS v3 base score: )
Weakness: NVD-CWE-noinfo \u2014 Insufficient Information
There is insufficient information about the issue to classify it; details are unkown or unspecified.
Repository: https://github.com/decidim/decidim
Files changed:
decidim-core/app/cells/decidim/upload_modal_cell.rb
decidim-core/app/packs/src/decidim/direct_uploads/redesigned_upload_modal.js
decidim-core/app/packs/src/decidim/direct_uploads/upload_modal.js
decidim-core/app/packs/src/decidim/utilities/text.js
decidim-core/spec/cells/decidim/upload_modal_cell_spec.rb
decidim-proposals/spec/system/edit_proposal_spec.rb
Commit message: Fix issues with the file uploader input display (#11612)



Vulnerable code:
      filename = file_name_for(attachment)
      return filename if filename.length <= max_length
      name.truncate(max_length, omission: "...#{name.last((max_length / 2) - 3)}#{File.extname(filename)}")
      determine_filename(attachment)
      <span>${truncateFilename(file.name)}</span>
        <span>${truncateFilename(file.name)}</span>
          <span>${truncateFilename(file.name)}</span>
          <input class="sm" type="text" value="${opts.title || truncateFilename(file.name)}" />
      <li ${attachmentId} data-filename="${file.name}" data-state="${state}">
    this.uploadItems.querySelector(`[data-filename="${name}"] progress`).value = value
        attachmentDetails.dataset.filename = file.name;
          titleAndFileNameSpan.innerHTML = `${title} (${file.name})`;
          titleAndFileNameSpan.innerHTML = file.name;
    wrapper.setAttribute("data-filename", fileName);
    fileNameSpan.innerHTML = truncateFilename(fileName);
      const item = this.uploadItems.querySelector(`[data-filename='${fileName}']`);
      this.trashCan.append(item);
    titleAndFileNameSpan.innerHTML = `${title} (${truncateFilename(fileName)})`;

Fixed code:
      filename = determine_filename(attachment)
      return decidim_html_escape(filename).html_safe if filename.length <= max_length
      decidim_html_escape(name.truncate(max_length, omission: "...#{name.last((max_length / 2) - 3)}#{File.extname(filename)}")).html_safe
      decidim_html_escape(determine_filename(attachment)).html_safe
import { escapeHtml, escapeQuotes } from "src/decidim/utilities/text";
      <span>${escapeHtml(truncateFilename(file.name))}</span>
        <span>${escapeHtml(truncateFilename(file.name))}</span>
          <span>${escapeHtml(truncateFilename(file.name))}</span>
          <input class="sm" type="text" value="${escapeQuotes(opts.title || truncateFilename(file.name))}" />
      <li ${attachmentId} data-filename="${escapeQuotes(file.name)}" data-state="${state}">
    this.uploadItems.querySelector(`[data-filename="${escapeQuotes(name)}"] progress`).value = value
import { escapeHtml } from "src/decidim/utilities/text";
        attachmentDetails.dataset.filename = escapeHtml(file.name);
          titleAndFileNameSpan.innerHTML = escapeHtml(`${title} (${file.name})`);
          titleAndFileNameSpan.innerHTML = escapeHtml(file.name);
    wrapper.setAttribute("data-filename", escapeHtml(fileName));
    fileNameSpan.innerHTML = escapeHtml(truncateFilename(fileName));
      this.trashCan.append(wrapper);
    titleAndFileNameSpan.innerHTML = escapeHtml(`${title} (${truncateFilename(fileName)})`);
export const escapeHtml = (text) => {
  if (!text) {
    return "";
  }

  const el = document.createElement("div");
  el.appendChild(document.createTextNode(text));
  return el.innerHTML;
}

export const escapeQuotes = (text) => {
  if (!text) {
    return "";
  }

  return text.replace(/"/g, "&quot;");
}

    context "when there is rich content in the filename" do
      let(:blob) { ActiveStorage::Blob.find_signed(attachments.first) }

      before do
        blob.update!(filename: "<svg onload=alert('ALERT')>.pdf")
      end

      it "escapes the truncated filename" do
        expect(my_cell.send(:truncated_file_name_for, attachments.first)).to eq("&lt;svg onload=alert(&#39;ALERT&#39;)&gt;.pdf")
      end

      it "escapes the filename" do
        expect(my_cell.send(:file_name_for, attachments.first)).to eq("&lt;svg onload=alert(&#39;ALERT&#39;)&gt;.pdf")
      end
    end

        context "with problematic file titles" do
          let!(:photo) { create(:attachment, :with_image, weight: 0, attached_to: proposal) }
          let!(:document) { create(:attachment, :with_pdf, weight: 1, attached_to: proposal) }

          before do
            document.update!(title: { en: "<svg onload=alert('ALERT')>.pdf" })
            photo.update!(title: { en: "<svg onload=alert('ALERT')>.jpg" })
          end

          it "displays them correctly on the edit form" do
            # With problematic code, should raise Selenium::WebDriver::Error::UnexpectedAlertOpenError
            click_link "Edit proposal"
            expect(page).to have_content("Required fields are marked with an asterisk")
            click_button("Edit documents")
            within "[data-dialog]" do
              click_button("Next")
            end
            click_button("Send")
            expect(page).to have_content("Proposal successfully updated.")
          end
        end

        context "with problematic file names" do
          let!(:photo) { create(:attachment, :with_image, weight: 0, attached_to: proposal) }
          let!(:document) { create(:attachment, :with_pdf, weight: 1, attached_to: proposal) }

          before do
            document.file.blob.update!(filename: "<svg onload=alert('ALERT')>.pdf")
            photo.file.blob.update!(filename: "<svg onload=alert('ALERT')>.jpg")
          end

          it "displays them correctly on the edit form" do
            # With problematic code, should raise Selenium::WebDriver::Error::UnexpectedAlertOpenError
            click_link "Edit proposal"
            expect(page).to have_content("Required fields are marked with an asterisk")
            click_button("Edit documents")
            within "[data-dialog]" do
              click_button("Next")
            end
            click_button("Send")
            expect(page).to have_content("Proposal successfully updated.")
          end
        end