Patterns and anti-patterns in software development

Philippe de Ryck

Swiss Cyber Storm 2021 · Day 1 · Arena

Overview

In his Swiss Cyber Storm talk, "Patterns and anti-patterns in software development," Philippe de Ryck addresses a pervasive challenge in application security: developers, despite good intentions, frequently fail to implement secure coding practices. De Ryck, a seasoned trainer in secure coding, argues that the current approach of burdening developers with an ever-growing list of security guidelines is ineffective and unsustainable. His core message is that instead of expecting individual developers to be experts in every security nuance, security teams should provide secure-by-default abstractions and tools that simplify secure development and reduce cognitive load.

Watch on YouTube · Slides

Visual summary for Patterns and anti-patterns in software development by Philippe de Ryck
Visual summary for Patterns and anti-patterns in software development by Philippe de Ryck

Key moments

  1. 1:27 Developers fail at secure coding despite good intentions
  2. 8:00 JSON Web Token 'none' vulnerability explained
  3. 11:58 Pattern: Encapsulate security behavior in libraries
  4. 14:30 React's dangerouslySetInnerHTML: a dangerous feature
  5. 21:30 Achieving code hygiene with safe components
  6. 26:00 How to get security into developers' toolbox

Patterns and anti-patterns in software development

Speakers: Philippe de Ryck

Conference: Swiss Cyber Storm

YouTube: https://www.youtube.com/watch?v=IZSMK2tOAoQ

Overview

In his Swiss Cyber Storm talk, "Patterns and anti-patterns in software development," Philippe de Ryck addresses a pervasive challenge in application security: developers, despite good intentions, frequently fail to implement secure coding practices. De Ryck, a seasoned trainer in secure coding, argues that the current approach of burdening developers with an ever-growing list of security guidelines is ineffective and unsustainable. His core message is that instead of expecting individual developers to be experts in every security nuance, security teams should provide secure-by-default abstractions and tools that simplify secure development and reduce cognitive load.

The talk delves into specific examples from JSON Web Token (JWT) handling and Cross-Site Scripting (XSS) prevention in React applications, highlighting how complex APIs and insufficient documentation lead to critical vulnerabilities. De Ryck advocates for a shift in strategy, emphasizing the importance of encapsulating security logic within reusable, vetted libraries and components. This approach not only streamlines development but also enables effective code hygiene through automated scanning, ultimately fostering a more secure software supply chain by making the secure path the easiest path.

Background

▶ Watch: Developers fail at secure coding despite good intentions (1:27)

The landscape of modern software development is fraught with complexity. Developers are constantly tasked with adhering to a multitude of guidelines that extend far beyond core functionality, encompassing performance optimization, accessibility standards, user interface design, and, crucially, security. Philippe de Ryck observes that this overwhelming volume of requirements often leads to developer fatigue and, paradoxically, a stagnation in security improvements. Despite attending trainings and being aware of security risks, developers often find themselves unable to consistently apply secure coding practices due to the sheer detail and nuance involved in each domain.

De Ryck differentiates this scenario from what he terms "absec hell," where organizations have no security program or awareness whatsoever. Instead, his focus is on environments where developers are trying to do the right thing but are inadvertently led astray by complex APIs, ambiguous documentation, and a lack of practical, actionable secure patterns. This problem is exacerbated by the reliance on third-party libraries and frameworks, which, while accelerating development, also introduce potential pitfalls if not used with a deep understanding of their security implications. The talk aims to dissect these "good intentions" that pave the road to security vulnerabilities, offering concrete strategies to empower developers and security teams alike.

Key Findings

▶ Watch: Pattern: Encapsulate security behavior in libraries (11:58)

Philippe de Ryck's presentation distills several critical findings regarding the current state of secure software development and proposes effective countermeasures:

  1. Developer Burden is Unsustainable: Expecting developers to master an exhaustive list of security guidelines, alongside myriad other concerns (performance, accessibility, UI/UX), leads to overwhelm and inconsistent application of security best practices. The current training-heavy model often results in developers feeling lost and unable to translate theory into practice effectively.
  2. Complex APIs are Anti-Patterns for Security: Libraries designed for common tasks, such as handling JSON Web Tokens (JWTs), often expose overly complex or ambiguous APIs. When developers are presented with multiple, subtly different functions (e.g., six parse methods), they are prone to selecting insecure options, even if the secure alternative exists within the same library. This was vividly demonstrated with the "none" algorithm vulnerability in JWT parsing.
  3. Explicit "Danger" Warnings Are Insufficient: While marking features as "dangerous" (e.g., React's dangerouslySetInnerHTML) is a step in the right direction, it doesn't magically enable secure usage. Developers still need to understand why it's dangerous, how to mitigate the risks, and what secure alternatives exist. Without clear, secure-by-default paths, such warnings often lead to developers finding insecure workarounds or misimplementing mitigations.
  4. Encapsulation of Security Logic is a Powerful Pattern: The most significant contribution proposed is the encapsulation of security behavior within internal, vetted libraries and components. Instead of developers implementing security logic directly, security teams should provide secure abstractions (e.g., a custom JWT handler, a SafeHtml React component). This removes the burden of intricate security knowledge from individual developers, centralizes security expertise, and ensures consistent application of best practices.
  5. Code Hygiene Through Automated Scanning is Essential: To enforce encapsulated security patterns and prevent the reintroduction of anti-patterns, Static Application Security Testing (SAST) tools like Semgrep are critical. By integrating these tools into the development pipeline and tailoring rules to an organization's secure libraries, teams can achieve "code hygiene" – a state where the scanner reports zero findings for legitimate, secure code, immediately flagging any deviation or accidental misuse of dangerous features.
  6. Incentivize Security by Simplifying Development: The most effective way to drive developer adoption of secure practices is to make the secure path the easiest and most efficient path. Security initiatives should aim to reduce friction and save time for developers, rather than adding to their workload. Providing secure-by-default tools and components acts as a strong incentive, transforming security from a perceived burden into a valued enabler.

Technical Deep Dive

▶ Watch: React's dangerouslySetInnerHTML: a dangerous feature (14:30)

Philippe de Ryck illustrates his points with two distinct, yet equally critical, technical examples: the insecure handling of JSON Web Tokens (JWTs) and the misuse of dangerouslySetInnerHTML in React for Cross-Site Scripting (XSS) prevention.

The first example focuses on JSON Web Tokens (JWTs), a common standard for securely transmitting information between parties as a JSON object. A JWT typically consists of three base64-encoded parts: a header (metadata), a payload (claims), and a signature. The signature is paramount, ensuring the integrity and authenticity of the token. If an application fails to validate the signature, an attacker can forge tokens, impersonating users or granting themselves elevated privileges.

De Ryck references a real-world vulnerability in Apache Pulsar, an open-source messaging system, which allowed account takeover due to improper JWT validation. Investigating the fix on GitHub revealed a subtle yet critical difference in Java code that puzzled even a security expert like de Ryck. The core issue lay within a popular Java JWT library (referred to as "jot thing" in the transcript, likely jjwt). When attempting to parse a JWT, the library presented developers with six overloaded parse methods via auto-completion. Without deep knowledge of JWT intricacies, a developer could easily select an insecure overload. The specific vulnerability exploited was the "none" algorithm bypass. This allows an attacker to construct a JWT where the header explicitly states alg: none, indicating no signature is present. If the parsing library's default or chosen parse method does not enforce signature validation, it will accept this token as valid, effectively trusting any claims made in the payload. De Ryck argues that expecting developers to navigate such complex and error-prone APIs is unrealistic and sets them up for failure. His proposed solution is to encapsulate JWT parsing and validation in an internal, security-vetted library provided by the security team, abstracting away the dangerous choices from application developers.

The second technical deep dive explores Cross-Site Scripting (XSS) in React applications. Modern JavaScript frameworks like React, Angular, and Ember generally offer robust built-in protections against XSS by automatically escaping data bound into HTML templates. This means that if a malicious string like <img src=x onerror=alert(1)> is passed to a React component, it will be rendered as literal text, not executed as code. This is a significant improvement over older libraries like jQuery, which required manual sanitization.

However, there are legitimate scenarios where applications need to render raw HTML, such as content from a rich text editor like CKEditor (e.g., user-generated blog posts with formatting). React addresses this with the property dangerouslySetInnerHTML. As its name explicitly suggests, this feature is inherently risky. When used, it bypasses React's default escaping, directly injecting the provided HTML into the DOM. If the HTML comes from an untrusted source and is not properly sanitized, it becomes a direct XSS vector. De Ryck highlights a vulnerability found in the Signal messenger desktop application, a client focused on security and privacy, which misused dangerouslySetInnerHTML, leading to an XSS flaw. This case demonstrates that even with explicit warnings and a security-conscious development team, mistakes can happen due to insufficient guidance on how to use such features securely.

The recommended mitigation involves sanitizing the HTML before rendering it with dangerouslySetInnerHTML. De Ryck specifically mentions DOMPurify, a widely recognized and robust HTML sanitization library. DOMPurify parses the HTML, removes any potentially malicious elements or attributes, and returns a safe subset of HTML. However, merely telling developers to use dangerouslySetInnerHTML with DOMPurify is still insufficient, as it requires them to understand the concepts, find the right library, and apply it correctly.

De Ryck's ultimate solution for React is to create a SafeHtml component. This custom component would internally use dangerouslySetInnerHTML but would also encapsulate the necessary DOMPurify sanitization logic. Developers would then simply use <SafeHtml html={userContent} />, abstracting away the complexity and risk. This component, provided and vetted by the security team, ensures that all raw HTML rendering in the application follows secure practices by default.

Demo / Proof of Concept

▶ Watch: Achieving code hygiene with safe components (21:30)

While the talk did not feature a live, interactive demo in the traditional sense, Philippe de Ryck effectively used code examples and static analysis tool output to illustrate the core concepts and vulnerabilities.

For the JWT discussion, de Ryck presented screenshots of a Java IDE (likely IntelliJ IDEA) showing the auto-completion suggestions for a JWT parsing library. He highlighted the problematic scenario where a developer is presented with six different parse methods, making it difficult to discern the secure option without deep knowledge of the library's internals and JWT specifications. He then explained how the "none" algorithm vulnerability would manifest if an insecure parse method were chosen, allowing an attacker to craft a token with alg: none that the server would accept without signature validation.

In the React XSS section, de Ryck demonstrated the default secure behavior of React, showing how standard data binding escapes malicious input. He then showed a code snippet utilizing dangerouslySetInnerHTML, illustrating how this explicit bypass of React's escaping mechanism would lead to an XSS vulnerability if untrusted input were directly rendered. To showcase the mitigation, he presented code integrating DOMPurify to sanitize the HTML before passing it to dangerouslySetInnerHTML.

Crucially, de Ryck extended this with a demonstration of Semgrep, a Static Application Security Testing (SAST) tool. He showed how Semgrep could detect instances of dangerouslySetInnerHTML in a React application, flagging them as potential security concerns. The insightful part of this "demo" was showing how, after implementing the secure SafeHtml component (which internally uses dangerouslySetInnerHTML with DOMPurify), Semgrep would still initially flag the internal usage. De Ryck then demonstrated how to configure Semgrep (e.g., using inline comments or custom rules) to suppress findings for the known-safe SafeHtml component. The result was a "zero findings" report from Semgrep, indicating that all other instances of dangerouslySetInnerHTML would be immediately flagged, thus establishing a powerful code hygiene mechanism. This effectively demonstrated how SAST can be leveraged not just to find bugs, but to enforce secure coding patterns and provide immediate feedback to developers when deviations occur.

Defensive Implications

▶ Watch: How to get security into developers' toolbox (26:00)

The talk offers critical defensive implications for both security teams and developers seeking to build more resilient applications:

  1. Prioritize Secure-by-Default Abstractions: Security teams should shift from providing extensive checklists and guidelines to offering secure-by-default libraries and components. Instead of expecting developers to correctly implement complex security logic (like JWT validation or HTML sanitization), provide them with pre-vetted, easy-to-use abstractions (e.g., an internal JwtService or a SafeHtml React component). This significantly reduces the cognitive load on developers and centralizes security expertise.
  2. Encapsulate "Dangerous" Features: Whenever a framework or library offers a powerful but risky feature (e.g., dangerouslySetInnerHTML), security teams should build a secure wrapper around it. This wrapper should handle all necessary sanitization, validation, or other security controls internally, exposing a safe and simple API to application developers. This strategy prevents direct misuse of the dangerous feature.
  3. Implement Robust Code Scanning (SAST): Integrate Static Application Security Testing (SAST) tools like Semgrep into the development pipeline. Configure these tools with custom rules that specifically detect the misuse of dangerous features or the incorrect implementation of security-critical logic. Crucially, suppress findings for known-safe abstractions provided by the security team. This establishes "code hygiene," where zero findings indicate a secure baseline, and any new finding immediately highlights a potential security issue or deviation from approved patterns.
  4. Test for Common Pitfalls: For critical security components like JWT handlers, developers and QA teams should include comprehensive test scenarios that specifically target known vulnerabilities, such as the "none" algorithm bypass. Throwing a "whole bunch of invalid tokens" at an endpoint should be a standard practice to ensure robust rejection mechanisms.
  5. Incentivize Security with Developer-Friendly Tools: To gain developer buy-in, security initiatives must make developers' lives easier. Offering tools and libraries that simplify complex tasks, save time, and inherently bake in security will be adopted far more readily than mandates or extensive training. As Netflix's experience demonstrated, when security tools help developers deploy faster or reduce their workload, adoption skyrockets.
  6. Foster a Culture of Collaboration, Not Blame: The talk implicitly advocates for a collaborative approach where security teams empower developers rather than just auditing or finding fault. By providing secure tools and clear guidance, security becomes an enabler of efficient development, fostering a more positive and productive relationship between security and development teams.

Key Takeaways

  • Encapsulate security behavior in libraries: Provide developers with vetted, internal libraries and components that abstract away complex and risky security logic, making the secure path the easiest path.
  • Mark dangerous features, but provide secure alternatives: Explicitly name risky features (e.g., dangerouslySetInnerHTML), but critically, offer a secure, easy-to-use alternative component or function that handles the underlying risks.
  • Implement robust code hygiene with SAST: Utilize static analysis tools like Semgrep with custom rules to enforce secure coding patterns, detect misuse of dangerous features, and achieve a "zero findings" state for legitimate, secure code.
  • Test for common security pitfalls: For critical components like JWT handlers, include specific test cases that target known vulnerabilities (e.g., "none" algorithm bypass) to ensure robust rejection of malicious inputs.
  • Reduce developer burden to scale security: Shift from overwhelming developers with guidelines to providing secure-by-default tools and abstractions that simplify their work and inherently integrate security, making it an incentive rather than a chore.
  • Focus on developer enablement: Security teams should act as enablers, offering solutions that streamline development while ensuring security, rather than merely policing code or issuing endless checklists.

About the Speaker(s)

Philippe de Ryck is a security expert from Belgium dedicated to helping developers build more secure software. He is on a mission to teach secure coding practices, working with developers through training, courses, and consulting engagements. De Ryck's expertise lies in translating complex security concepts into actionable advice and patterns that developers can effectively implement in their daily work, focusing on practical solutions to common application security challenges.

Reviews

Dr. Zero (Offensive Security Researcher) — SOLID

De Ryck is right that handing developers a 200-page security manual is a waste of time. The paved road approach works, and wrapping DOMPurify in a custom React component while tuning Semgrep to scream at anything else is exactly how you actually scale AppSec. It's not new research, but it's a competent blueprint for stopping stupid bugs.

Heather Calloway (CISO) — STRONG ACCEPT

This talk successfully argues why our developer security training budgets yield diminishing returns. By shifting the focus from education to building secure-by-default abstractions, de Ryck provides a highly practical blueprint for scaling application security. It is required viewing for anyone leading an AppSec or platform engineering team.

→ Top-rated talks at Swiss Cyber Storm 2021

All talks from Swiss Cyber Storm 2021