Skip to content

Reflected Cross-site Scripting in yiisoft/yii2 Debug mode

Moderate severity GitHub Reviewed Published May 30, 2024 in yiisoft/yii2 • Updated Jul 8, 2024

Package

composer yiisoft/yii2 (Composer)

Affected versions

>= 2.0.43, < 2.0.49.4

Patched versions

2.0.49.4

Description

During the internal penetration testing of our product based on Yii2, we discovered an XSS vulnerability within the framework itself. This issue is relevant for the latest version of Yii2 (2.0.49.3).

Conditions for vulnerability reproduction

  • The framework is in debug mode (YII_DEBUG set to true).
  • The php.ini setting zend.exception_ignore_args is set to Off (default value).
  • An attacker induces an exception in the application, leading to a stack trace page being displayed.

Vulnerability description

The issue lies in the mechanism for displaying function argument values in the stack trace. The vulnerability manifests when an argument's value exceeds 32 characters. For convenience, argument values exceeding this limit are truncated and displayed with an added "...". The full argument value becomes visible when hovering over it with the mouse, as it is displayed in the title attribute of a span tag. However, the use of a double quote (") allows an attacker to break out of the title attribute's value context and inject their own attributes into the span tag, including malicious JavaScript code through event handlers such as onmousemove.

Demonstration example:
http://31.184.254.143/about/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%22%20onmousemove=alert(1)%20style=%22width:%20100000px;%20height:%20100000px;%20position:%20absolute;%20top:%20-10000px;%20left:%200;%22

Impact of the vulnerability

This vulnerability allows an attacker to execute arbitrary JavaScript code in the security context of the victim's site via a specially crafted link. This could lead to the theft of cookies (including httpOnly cookies, which are accessible on the page), content substitution, or complete takeover of user accounts.

Technical analysis and mitigation suggestion

Upon analyzing the framework's source code, it was found that data handling for the title attribute is performed in the file framework/web/ErrorHandler.php. The identified problem is related to changes made in the commit yiisoft/yii2@8cc9aeb , which led to the disabling of encoding for single and double quotes in the htmlEncode method (https://github.com/yiisoft/yii2/blob/8cc9aeb2f0b2ffe02fb54a817064e9da75512706/framework/web/ErrorHandler.php#L183) due to the addition of the ENT_NOQUOTES flag. To address this issue while preserving the functionality intended by the commit, we suggest modifying the htmlEncode method as follows:

    return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8');

This change will effectively prevent the XSS vulnerability while maintaining the targeted functionality of the previous changes.

Conclusion

Based on the above, we strongly recommend implementing the suggested changes to the project's main code as soon as possible to protect framework users from potential attacks. I am ready to provide further information or assistance, including creating a pull request if necessary.

References

@samdark samdark published to yiisoft/yii2 May 30, 2024
Published by the National Vulnerability Database May 30, 2024
Published to the GitHub Advisory Database Jun 2, 2024
Reviewed Jun 2, 2024
Last updated Jul 8, 2024

Severity

Moderate

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
High
Privileges required
None
User interaction
Required
Scope
Unchanged
Confidentiality
Low
Integrity
Low
Availability
None

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:H/PR:N/UI:R/S:U/C:L/I:L/A:N

EPSS score

0.043%
(10th percentile)

Weaknesses

CVE ID

CVE-2024-32877

GHSA ID

GHSA-qg5r-95m4-mjgj

Source code

Credits

Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.