CVE-2023-35929 (published 2023-07-25T18:15Z)

Tuleap is a free and open source suite to improve management of software development and collaboration. Prior to version 14.10.99.4 of Tuleap Community Edition and prior to versions 14.10-2 and 14.9-5 of Tuleap Enterprise Edition, content displayed in the "card fields" (visible in the kanban and PV2 apps) is not properly escaped. A malicious user with the capability to create an artifact or to edit a field used as a card field could force victim to execute uncontrolled code. Tuleap Community Edition 14.10.99.4, Tuleap Enterprise Edition 14.10-2, and Tuleap Enterprise Edition 14.9-5 contain a fix.

CVSS v3 base score: 5.4, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:L/A:N)

Weakness information:
cwe_id: CWE-79
cwe_name: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
description: The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users.
extended_description: 
            Cross-site scripting (XSS) vulnerabilities occur when:
            
               Untrusted data enters a web application, typically from a web request.
               The web application dynamically generates a web page that contains this untrusted data.
               During page generation, the application does not prevent the data from containing content that is executable by a web browser, such as JavaScript, HTML tags, HTML attributes, mouse events, Flash, ActiveX, etc.
               A victim visits the generated web page through a web browser, which contains malicious script that was injected using the untrusted data.
               Since the script comes from a web page that was sent by the web server, the victim's web browser executes the malicious script in the context of the web server's domain.
               This effectively violates the intention of the web browser's same-origin policy, which states that scripts in one domain should not be able to access resources or run code in a different domain.
            
            There are three main kinds of XSS:
            
               
                  Type 1: Reflected XSS (or Non-Persistent) - 
         			The server reads data directly from the HTTP request and reflects it back in the HTTP response. Reflected XSS exploits occur when an attacker causes a victim to supply dangerous content to a vulnerable web application, which is then reflected back to the victim and executed by the web browser. The most common mechanism for delivering malicious content is to include it as a parameter in a URL that is posted publicly or e-mailed directly to the victim. URLs constructed in this manner constitute the core of many phishing schemes, whereby an attacker convinces a victim to visit a URL that refers to a vulnerable site. After the site reflects the attacker's content back to the victim, the content is executed by the victim's browser.
               
                  Type 2: Stored XSS (or Persistent) - 
               The application stores dangerous data in a database, message forum, visitor log, or other trusted data store. At a later time, the dangerous data is subsequently read back into the application and included in dynamic content. From an attacker's perspective, the optimal place to inject malicious content is in an area that is displayed to either many users or particularly interesting users. Interesting users typically have elevated privileges in the application or interact with sensitive data that is valuable to the attacker. If one of these users executes malicious content, the attacker may be able to perform privileged operations on behalf of the user or gain access to sensitive data belonging to the user. For example, the attacker might inject XSS into a log message, which might not be handled properly when an administrator views the logs.
            
               
                  Type 0: DOM-Based XSS - 
               In DOM-based XSS, the client performs the injection of XSS into the page; in the other types, the server performs the injection. DOM-based XSS generally involves server-controlled, trusted script that is sent to the client, such as Javascript that performs sanity checks on a form before the user submits it. If the server-supplied script processes user-supplied data and then injects it back into the web page (such as with dynamic HTML), then DOM-based XSS is possible.
            
            
            Once the malicious script is injected, the attacker can perform a variety of malicious activities. The attacker could transfer private information, such as cookies that may include session information, from the victim's machine to the attacker. The attacker could send malicious requests to a web site on behalf of the victim, which could be especially dangerous to the site if the victim has administrator privileges to manage that site. Phishing attacks could be used to emulate trusted web sites and trick the victim into entering a password, allowing the attacker to compromise the victim's account on that web site. Finally, the script could exploit a vulnerability in the web browser itself possibly taking over the victim's machine, sometimes referred to as "drive-by hacking."
            In many cases, the attack can be launched without the victim even being aware of it. Even with careful users, attackers frequently use a variety of methods to encode the malicious portion of the attack, such as URL encoding or Unicode, so the request looks less suspicious.
         
         

Fix information:
repo_url: https://github.com/Enalean/tuleap
commit_details: repo_url: https://github.com/Enalean/tuleap
author: Thomas Gerbet
author_date: 2023-06-21 13:45:56+02:00
msg: Fixes request #32629: XSS in the card field of the agile dashboard apps

Tried to keep the changes within the scope of the security issue. The
template engine of hybrids is bit abused but it is still better than
before. A future contribution might want to consider removing angular
completely from @tuleap/plugin-agiledashboard-card-fields.

This change also fixes the display of the legacy colors for selectbox
using them.

Change-Id: I301ed6a72c324054a490605e881646bf317316fb
repository: repo_url: https://github.com/Enalean/tuleap
repo_name: Enalean/tuleap
description: Tuleap is an Open Source Suite to improve management of software developments and collaboration. With a single web-based solution, project managers, developers & quality managers can easily build, deploy software projects.
repo_language: PHP
owner: Enalean
file_changes: filename: package.json
change_type: ModificationType.MODIFY
diff: @@ -18,19 +18,18 @@
   "dependencies": {
     "@tuleap/tlp-swatch-colors": "workspace:*",
     "dompurify": "^3.0.0",
-    "escape-string-regexp": "^2.0.0"
+    "escape-string-regexp": "^2.0.0",
+    "hybrids": "^8.2.1"
   },
   "peerDependencies": {
     "angular": "^1.8.0",
-    "angular-sanitize": "^1.8.0",
     "moment": "^2.29.1"
   },
   "devDependencies": {
     "@tuleap/build-system-configurator": "workspace:*",
     "@types/dompurify": "^2.4.0",
     "angular": "^1.8.2",
-    "angular-mocks": "^1.8.2",
-    "angular-sanitize": "^1.8.2"
+    "angular-mocks": "^1.8.2"
   },
   "scripts": {
     "build": "vite build",

programming_language: JavaScript
filename: pnpm-lock.yaml
change_type: ModificationType.MODIFY
diff: @@ -10,6 +10,9 @@ dependencies:
   escape-string-regexp:
     specifier: ^2.0.0
     version: 2.0.0
+  hybrids:
+    specifier: ^8.2.1
+    version: 8.2.1
 
 devDependencies:
   '@tuleap/build-system-configurator':
@@ -24,9 +27,6 @@ devDependencies:
   angular-mocks:
     specifier: ^1.8.2
     version: 1.8.2
-  angular-sanitize:
-    specifier: ^1.8.2
-    version: 1.8.2
 
 packages:
 
@@ -44,10 +44,6 @@ packages:
     resolution: {integrity: sha512-I5L3P0l21HPdVsP4A4qWmENt4ePjjbkDFdAzOaM7QiibFySbt14DptPbt2IjeG4vFBr4vSLbhIz8Fk03DISl8Q==}
     dev: true
 
-  /angular-sanitize@1.8.2:
-    resolution: {integrity: sha512-OB6Goa+QN3byf5asQ7XRl7DKZejm/F/ZOqa9z1skqYVOWA2hoBxoCmt9E7+i7T/TbxZP5zYzKxNZVVJNu860Hg==}
-    dev: true
-
   /angular@1.8.2:
     resolution: {integrity: sha512-IauMOej2xEe7/7Ennahkbb5qd/HFADiNuLSESz9Q27inmi32zB0lnAsFeLEWcox3Gd1F6YhNd1CP7/9IukJ0Gw==}
     dev: true
@@ -60,3 +56,9 @@ packages:
     resolution: {integrity: sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==}
     engines: {node: '>=8'}
     dev: false
+
+  /hybrids@8.2.1:
+    resolution: {integrity: sha512-ZIjDLZi6aBOL+Cdry1xtBLJJnMzx99HNwmOCQXEvqdawyYu949z469vxgExyAgq0CA4lSfJsjh1rsiIsGdqXdw==}
+    engines: {node: '>=18.0.0'}
+    hasBin: true
+    dev: false

programming_language: Markdown
filename: card-fields-service.js
change_type: ModificationType.MODIFY
diff: @@ -1,12 +1,11 @@
-import moment from "moment";
+import { html } from "hybrids";
+import { highlightFilterElements } from "./highlight-filter-template";
 
 export default CardFieldsService;
 
-CardFieldsService.$inject = ["$sce", "$filter"];
-
-function CardFieldsService($sce, $filter) {
-    const highlight = $filter("tuleapHighlight");
+CardFieldsService.$inject = ["$sce"];
 
+function CardFieldsService($sce) {
     return {
         cardFieldIsSimpleValue,
         cardFieldIsList,
@@ -18,7 +17,6 @@ function CardFieldsService($sce, $filter) {
         cardFieldIsPermissions,
         cardFieldIsUser,
         cardFieldIsComputed,
-        getCardFieldDateValue,
         getCardFieldListValues,
         getCardFieldFileValue,
         getCardFieldPermissionsValue,
@@ -99,35 +97,35 @@ function CardFieldsService($sce, $filter) {
             } else if (value.tlp_color) {
                 return getValueRenderedWithTlpColor(value, filter_terms);
             } else if (value.avatar_url) {
-                return getCardFieldUserValue(value, filter_terms);
+                return getCardFieldUserValueTemplate(value, filter_terms);
             }
 
-            return highlight(value.label, filter_terms);
+            return highlightFilterElements(value.label, filter_terms);
         }
 
         function getValueRenderedWithColor(value, filter_terms) {
             const r = parseInt(value.color.r, 10);
             const g = parseInt(value.color.g, 10);
             const b = parseInt(value.color.b, 10);
-            const color = $sce.getTrustedHtml(`<span class="extra-card-field-color"
-                style="background: rgb(${r}, ${g}, ${b})"></span>`);
 
-            return color + highlight(value.label, filter_terms);
+            const styles = {
+                background: `rgb(${r}, ${g}, ${b})`,
+            };
+
+            return html`<span class="extra-card-field-color" style="${styles}"></span
+                >${highlightFilterElements(value.label, filter_terms)}`;
         }
 
         function getValueRenderedWithTlpColor({ label, tlp_color }, filter_terms) {
-            const color = $sce.getTrustedHtml(
-                `<span class="extra-card-field-color card-field-${tlp_color}"></span>`
-            );
+            const classlist = ["extra-card-field-color", `card-field-${tlp_color}`];
 
-            return color + highlight(label, filter_terms);
+            return html`<span class="${classlist}"></span>${highlightFilterElements(
+                    label,
+                    filter_terms
+                )}`;
         }
 
-        return $sce.trustAsHtml(values.map(getValueRendered).join(", "));
-    }
-
-    function getCardFieldDateValue(value) {
-        return moment(value).fromNow();
+        return getHTMLStringFromTemplate(renderListItems(values, getValueRendered));
     }
 
     function getCardFieldFileValue(artifact_id, field_id, file_descriptions, filter_terms) {
@@ -141,41 +139,64 @@ function CardFieldsService($sce, $filter) {
         }
 
         function getFileLink(file) {
-            var file_name = highlight(file.name, filter_terms);
-
-            return (
-                '<a data-nodrag="true" href="' +
-                getFileUrl(file) +
-                '" title="' +
-                file.description +
-                '"><i class="fas fa-paperclip extra-card-field-file-icon"></i>' +
-                file_name +
-                "</a>"
-            );
+            const file_display = html`<i class="fas fa-paperclip extra-card-field-file-icon"></i
+                >${highlightFilterElements(file.name, filter_terms)}`;
+            return html`<a data-nodrag="true" href="${getFileUrl(file)}" title="${file.description}"
+                >${file_display}</a
+            >`;
         }
 
-        return file_descriptions.map(getFileLink).join(", ");
+        return getHTMLStringFromTemplate(renderListItems(file_descriptions, getFileLink));
     }
 
-    function getCardFieldPermissionsValue(values) {
-        return values.join(", ");
+    function getCardFieldPermissionsValue(values, filter_terms) {
+        return getHTMLStringFromTemplate(
+            renderListItems(
+                values,
+                (value) => html`${highlightFilterElements(value, filter_terms)}`
+            )
+        );
     }
 
-    function getCardFieldUserValue(value, filter_terms) {
-        let display_name;
-
+    function getCardFieldUserValueTemplate(value, filter_terms) {
+        const display_name = highlightFilterElements(value.display_name, filter_terms);
         if (value.user_url === null) {
-            display_name = highlight(value.display_name, filter_terms);
-            return `<div class="tlp-avatar-mini"> </div><span>${display_name}</span>`;
+            return html`<div class="tlp-avatar-mini"></div>
+                <span>${display_name}</span>`;
         }
 
-        display_name = highlight(value.display_name, filter_terms);
-        return `<a data-nodrag="true" class="extra-card-field-user" href="${value.user_url}">
-                            <div class="tlp-avatar-mini"><img src="${value.avatar_url}" /></div><span>${display_name}</span>
-                        </a>`;
+        return html`<a data-nodrag="true" class="extra-card-field-user" href="${value.user_url}">
+            <div class="tlp-avatar-mini"><img loading="lazy" src="${value.avatar_url}" /></div>
+            <span>${display_name}</span>
+        </a>`;
+    }
+
+    function getCardFieldUserValue(value, filter_terms) {
+        return getHTMLStringFromTemplate(getCardFieldUserValueTemplate(value, filter_terms));
     }
 
     function isListBoundToAValueDifferentFromNone(values) {
         return values.find((value) => value.id !== null);
     }
+
+    function renderListItems(items, render_item) {
+        let templated_content = html``;
+
+        for (const [i, file] of items.entries()) {
+            if (i === 0) {
+                templated_content = render_item(file);
+            } else {
+                templated_content = html`${templated_content}, ${render_item(file)}`;
+            }
+        }
+
+        return templated_content;
+    }
+
+    function getHTMLStringFromTemplate(template) {
+        const element = document.createElement("div");
+        template({}, element);
+
+        return $sce.trustAsHtml(element.innerHTML);
+    }
 }

programming_language: JavaScript
filename: card-fields.tpl.html
change_type: ModificationType.MODIFY
diff: @@ -62,7 +62,7 @@
                 ng-if="card_ctrl.cardFieldIsPermissions(card_field.type) && card_field.granted_groups.length > 0"
         >
             <span class="extra-card-field-title">{{ card_field.label }}: </span>
-            <span ng-bind-html="card_ctrl.getCardFieldPermissionsValue(card_field.granted_groups) | tuleapHighlight:card_ctrl.filter_terms"></span>
+            <span ng-bind-html="card_ctrl.getCardFieldPermissionsValue(card_field.granted_groups, card_ctrl.filter_terms)"></span>
         </div>
         <div
                 class="extra-card-field-content"

programming_language: HTML
filename: highlight-filter-template.test.ts
change_type: ModificationType.ADD
diff: @@ -0,0 +1,205 @@
+/**
+ * Copyright (c) Enalean, 2023-Present. All Rights Reserved.
+ *
+ * This file is a part of Tuleap.
+ *
+ * Tuleap is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Tuleap is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import { highlightFilterElements } from "./highlight-filter-template";
+import type { UpdateFunctionWithMethods } from "hybrids";
+
+jest.mock("escape-string-regexp", () => ({
+    default: jest.requireActual("escape-string-regexp"),
+}));
+
+const TEST_PHRASE = "Prefix Highlight Suffix";
+
+describe("highlight-filter-template", () => {
+    it("should highlight a matching phrase", () => {
+        expect(getElementFromTemplate(highlightFilterElements(TEST_PHRASE, "highlight")))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+              
+              Prefix 
+              
+              <span
+                class="highlight"
+              >
+                Highlight
+              </span>
+              
+               Suffix
+            </div>
+        `);
+    });
+
+    it("should highlight nothing if no match found", () => {
+        expect(getElementFromTemplate(highlightFilterElements(TEST_PHRASE, "no match")))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+              
+              Prefix Highlight Suffix
+            </div>
+        `);
+    });
+
+    it("should highlight nothing for the undefined filter", () => {
+        expect(getElementFromTemplate(highlightFilterElements(TEST_PHRASE, undefined)))
+            .toMatchInlineSnapshot(`
+            <div>
+              Prefix Highlight Suffix
+            </div>
+        `);
+    });
+
+    it("should work correctly for number filters", () => {
+        expect(getElementFromTemplate(highlightFilterElements("3210123", 0)))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+              
+              321
+              
+              <span
+                class="highlight"
+              >
+                0
+              </span>
+              
+              123
+            </div>
+        `);
+    });
+
+    it("should work when the text context is undefined", () => {
+        expect(getElementFromTemplate(highlightFilterElements(undefined, "nothing")))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+            </div>
+        `);
+    });
+
+    it("should work correctly for number text", () => {
+        expect(getElementFromTemplate(highlightFilterElements(3210123, "0")))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+              
+              321
+              
+              <span
+                class="highlight"
+              >
+                0
+              </span>
+              
+              123
+            </div>
+        `);
+    });
+
+    it("should highlight nothing if empty filter string passed", () => {
+        expect(getElementFromTemplate(highlightFilterElements(TEST_PHRASE, "")))
+            .toMatchInlineSnapshot(`
+            <div>
+              Prefix Highlight Suffix
+            </div>
+        `);
+    });
+
+    it("should highlight more that one element", () => {
+        expect(getElementFromTemplate(highlightFilterElements(TEST_PHRASE, "gh")))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+              
+              Prefix Hi
+              
+              <span
+                class="highlight"
+              >
+                gh
+              </span>
+              
+              li
+              
+              <span
+                class="highlight"
+              >
+                gh
+              </span>
+              
+              t Suffix
+            </div>
+        `);
+    });
+
+    it("highlights each matching search terms", () => {
+        expect(getElementFromTemplate(highlightFilterElements(TEST_PHRASE, "suffix highlight")))
+            .toMatchInlineSnapshot(`
+            <div>
+              
+              
+              Prefix 
+              
+              <span
+                class="highlight"
+              >
+                Highlight
+              </span>
+              
+               
+              
+              <span
+                class="highlight"
+              >
+                Suffix
+              </span>
+            </div>
+        `);
+    });
+
+    it("should escape regexp search terms", () => {
+        expect(
+            getElementFromTemplate(
+                highlightFilterElements("Prefix (Highlight) Suffix", "(Highlight)")
+            )
+        ).toMatchInlineSnapshot(`
+            <div>
+              
+              
+              Prefix 
+              
+              <span
+                class="highlight"
+              >
+                (Highlight)
+              </span>
+              
+               Suffix
+            </div>
+        `);
+    });
+});
+
+function getElementFromTemplate(template: UpdateFunctionWithMethods<unknown>): HTMLElement {
+    const element = document.createElement("div");
+    template(document.body, element);
+
+    return element;
+}

programming_language: TypeScript
filename: highlight-filter-template.ts
change_type: ModificationType.ADD
diff: @@ -0,0 +1,46 @@
+/**
+ * Copyright (c) Enalean, 2023-Present. All Rights Reserved.
+ *
+ * This file is a part of Tuleap.
+ *
+ * Tuleap is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Tuleap is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import type { UpdateFunctionWithMethods } from "hybrids";
+import { html } from "hybrids";
+import { Classifier } from "./highlight/Classifier";
+import { HighlightedText } from "./highlight/HighlightedText";
+export function highlightFilterElements(
+    content: string | number | undefined,
+    search: string | number | undefined
+): UpdateFunctionWithMethods<unknown> {
+    const text_content = content?.toString();
+    if (
+        text_content === "" ||
+        text_content === undefined ||
+        search === "" ||
+        search === undefined
+    ) {
+        return html`${text_content}`;
+    }
+
+    const classifier = Classifier(search.toString());
+    const templates = classifier.classify(text_content).map((highlighted_text) => {
+        if (!HighlightedText.isHighlight(highlighted_text)) {
+            return html`${highlighted_text.content}`;
+        }
+        return html`<span class="highlight">${highlighted_text.content}</span>`;
+    });
+    return html`${templates}`;
+}

programming_language: TypeScript
filename: highlight-filter.js
change_type: ModificationType.MODIFY
diff: @@ -5,9 +5,7 @@
  * @license MIT
  */
 
-import { isNumber } from "angular";
-import { Classifier } from "./highlight/Classifier";
-import { HighlightedText } from "./highlight/HighlightedText";
+import { highlightFilterElements } from "./highlight-filter-template";
 
 export default TuleapHighlightFilter;
 
@@ -19,22 +17,17 @@ TuleapHighlightFilter.$inject = [];
  * @returns HTML-encoded string
  */
 function TuleapHighlightFilter() {
-    function isTextSearchable(text, search) {
-        return text && (search || isNumber(search));
+    function getHTMLStringFromTemplate(template) {
+        const element = document.createElement("div");
+        template({}, element);
+
+        return element.innerHTML;
     }
 
     return function (text, search) {
-        if (!isTextSearchable(text, search)) {
-            return text ? text.toString() : text;
+        if (text === null) {
+            return null;
         }
-
-        const classifier = Classifier(String(search));
-        const parts = classifier.classify(String(text)).map((highlighted_text) => {
-            if (!HighlightedText.isHighlight(highlighted_text)) {
-                return highlighted_text.content;
-            }
-            return `<span class="highlight">${highlighted_text.content}</span>`;
-        });
-        return parts.join("");
+        return getHTMLStringFromTemplate(highlightFilterElements(text, search));
     };
 }

programming_language: TypeScript
filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -1,5 +1,4 @@
 import angular from "angular";
-import ngSanitize from "angular-sanitize";
 import "../themes/main.scss";
 
 import HighlightFilter from "./highlight-filter.js";
@@ -11,7 +10,7 @@ import cardComputedFieldDirective from "./card-computed-field/card-computed-fiel
 import cardTextFieldDirective from "./card-text-field/card-text-field-directive.js";
 
 export default angular
-    .module("card-fields", [ngSanitize])
+    .module("card-fields", [])
     .service("CardFieldsService", CardFieldsService)
     .directive("cardFields", CardFieldsDirective)
     .directive("tuleapSimpleField", tuleapSimpleFieldDirective)

programming_language: TypeScript
filename: vite.config.ts
change_type: ModificationType.MODIFY
diff: @@ -27,12 +27,11 @@ export default vite.defineLibConfig({
             name: "PluginAgileDashboardCardFields",
         },
         rollupOptions: {
-            external: ["angular", "angular-sanitize", "moment"],
+            external: ["angular", "hybrids"],
             output: {
                 globals: {
                     angular: "angular",
-                    "angular-sanitize": "angular-sanitize",
-                    moment: "moment",
+                    hybrids: "hybrids",
                 },
             },
         },

programming_language: Scala