🚨 CSS Injection on GitHub: What Happened and How to Stay Safe 🚨
Tags: github, css
Published: 2024-06-08 17:59:36

## 🚨 CSS Injection on GitHub: What Happened and How to Stay Safe 🚨

Hey Dev.to community! 🌟

I wanted to bring your attention to a recent security issue involving CSS injection on GitHub. This incident has been a hot topic and underscores the importance of staying vigilant with web security. Let's break down what happened, the implications, and how we can protect our projects from such vulnerabilities.

### What Happened? 🕵️‍♂️

Recently, a CSS injection vulnerability was discovered on GitHub profiles. This type of attack allows malicious users to inject unauthorized CSS code into web pages, potentially altering their appearance and behavior in harmful ways. Steve Matindi wrote an insightful article on Medium titled ["CSS Injection on GitHub Profiles: From Unicode Exploits to New Bypass Techniques"](https://stevemats.medium.com/css-injection-on-github-profiles-from-unicode-exploits-to-new-bypass-techniques-f73f343f05d8), where he explained how these exploits work and detailed both old and new methods attackers have used.

### The Original Exploit: Unicode Command Injection

Initially, attackers found a way to inject CSS styles using LaTeX math mode with the `\unicode` command. Here’s an example of the code used:

```latex
$$\ce{$\unicode[goombafont; color:red; pointer-events: none; z-index: 100; position: fixed; top: 0; left: 0; height: 100vh; object-fit: cover; background-size: cover; width: 100vw; opacity: 1.0; background: url('https://github.com/stevemats/stong/blob/master/Rotating_cube_SMIL.svg?raw=true');]{x0000}$}$$
```

This code injected styles that altered the appearance of a profile page by placing an image as the background and applying various CSS properties.

### GitHub’s Response: Blocking the Unicode Command

To counter this exploit, GitHub blocked the `\unicode` command in LaTeX math mode. Users trying to use this command now see an error message: "The following macros are not allowed: \unicode."

### A New Bypass Technique: HTML Character Encoding

Despite GitHub's efforts, attackers found a new way to bypass the filter using HTML character encoding. Here’s an example of the updated exploit:

```latex
$$\ce{$&#x5C;unicode[goombafont; color:red; pointer-events: none; z-index: -10; position: fixed; top: 0; left: 0; height: 100vh; object-fit: cover; background-size: cover; width: 130vw; opacity: 0.5; background: url('https://user-images.githubusercontent.com/30528167/92789817-e4b53d80-f3b3-11ea-96a4-dad3ea09d237.png?raw=true');]{x0000}$}$$
```

#### How It Works

1. **HTML Character Encoding**: The sequence `&#x5C;` is the HTML entity for the backslash (`\`). This encoding helps avoid directly writing the `\unicode` command, which GitHub’s filter blocks.
2. **LaTeX Math Mode**: The code remains enclosed within LaTeX math mode markers (`$$...$$`), allowing LaTeX commands.
3. **Chemical Equation (`\ce`) Environment**: The `\ce` command acts as a wrapper, potentially disguising the intent of the code.
4. **Unicode Command Injection**: When decoded, the `&#x5C;` entity is interpreted as a backslash, executing the `\unicode` command with the specified CSS properties.

### Why This Bypass Works

- **Encoding Trick**: By using `&#x5C;`, the command is hidden from the filter that blocks `\unicode`.
- **Command Interpretation**: Once rendered, the encoded character is interpreted correctly, executing the command.

### GitHub's Continued Efforts

GitHub is actively addressing this new method by further tightening their filters and enhancing their security measures. These steps are crucial for several reasons:

- **Security Risks**: Arbitrary CSS injection poses significant security vulnerabilities, including phishing risks.
- **Site Integrity**: CSS injection can disrupt the site's consistent appearance and functionality.
- **Abuse Prevention**: Restrictions are necessary to prevent malicious actors from exploiting CSS injection for harmful activities.
- **User Experience**: Excessive styles can lead to poor user experiences, making profiles unusable or visually overwhelming.

### Protecting Your Projects 🚧

As developers, we must be proactive in safeguarding our projects against CSS injection and other security threats. Here are some best practices:

1. **Sanitize and Validate User Inputs**: Always ensure user inputs are sanitized and validated before processing or rendering them.
2. **Implement Content Security Policies (CSP)**: Use CSP headers to restrict the sources from which CSS and other resources can be loaded.
3. **Conduct Regular Security Audits**: Regularly audit your code and conduct security reviews to identify and fix vulnerabilities.
4. **Stay Informed**: Keep up with the latest security trends and updates.

### Resources 📚

To dive deeper into CSS injection and prevention techniques, check out these resources:
- [Security Boulevard: CSS Injection Prevention Guide](https://securityboulevard.com/2024/06/a-complete-guide-on-css-injection-prevention-examples-steps-included/)
- [OWASP: Cross-Site Scripting (XSS)](https://owasp.org/www-community/attacks/xss/)
- [MDN Web Docs: Content Security Policy (CSP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)

### Conclusion

Security vulnerabilities like CSS injection are a stark reminder of the ever-present threats in web development. By adopting best practices and staying vigilant, we can protect our projects and users from malicious attacks. Let’s all stay safe and keep coding securely! 💪✨

Feel free to share your thoughts and additional tips in the comments. Happy coding! 👩‍💻👨‍💻

---

*References:*
- [Steve Matindi's Medium Article](https://stevemats.medium.com/css-injection-on-github-profiles-from-unicode-exploits-to-new-bypass-techniques-f73f343f05d8)
- [Security Boulevard](https://securityboulevard.com/2024/06/a-complete-guide-on-css-injection-prevention-examples-steps-included/)
- [GitHub Blog](https://github.blog/2024/06/fixing-security-vulnerabilities-with-ai/)

---

I hope this helps! If you have any further details or specific questions about the vulnerability, feel free to ask. 😊


---
Tweets


{% twitter https://twitter.com/cloud11665/status/1799136093071163396 %}

{% twitter https://twitter.com/gf_256/status/1799197013629645101 %}