Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»CodeSOD: A Stack of XML

    CodeSOD: A Stack of XML

    June 27, 2024

    Alice works with an XML-based RPC system. They send requests, it does a thing, and returns back a response, all surrounded in XML. The service sending the requests, however, doesn’t use a well-established parsing library. The result is that, well, the code ends up here.

    First, though, a bit about the data they expect to parse. It’s verbose, but fairly straightforward:

    <methodResponse>
    <params>
    <param>
    <value>
    <struct>
    <member>
    <name>foo</name>
    <value>
    <string>abcde</string>
    </value>
    </member>
    <member>
    <name>bar</name>
    <value>
    <int>12345</int>
    </value>
    </member>
    </struct>
    </value>
    </param>
    </params>
    </methodResponse>

    The tree is awkward, but the response contains params, where each param may contain a struct, the struct may contain members, which have a name and a value, where the value contains the actual data, wrapped in a type.

    But there’s one thing missing here: the service which sends this response recently changed its API, and for some fields, includes an empty <data/> tag, instead of a value. This particular client doesn’t care about data fields, and should be able to safely ignore them if they exist.

    Now, you might think, “well, that should be easy then,” and you’d be right if you were just using a generic XML parser. They didn’t do that. They hand-rolled a parser that is specific to this data format. So let’s see what happened:

    void XmlRpcResponseParser::OnStartElement(const XML_Char *pszName, const XML_Char **papszAttrs)
    {
    const string element = pszName;
    // Sanity
    if (_states.empty()) {
    if (element != “methodResponse” && !_get_method_response) {
    throw “Invalid top-level element <“ + element + “> expecting <methodResponse>”;
    }

    _got_method_response = true;
    _states.push(&response);
    }

    if (element == “fault”) {
    // response is faulty: just extract error’s code and description
    is_fault = true;
    } else if (!is_fault && element == “struct”) {
    top()->type = XML_RPC_STRUCT;
    } else if (!is_fault && element == “array”) {
    top()->type = XML_RPC_ARRAY;
    } else if (!is_fault && (element == “data” || element == “value”)) {
    if (top()->type == XML_RPC_ARRAY) {
    top()->xml_array.push_back(Entry());
    _states.push(&top()->xml_array.back()); // <———— #1
    }
    }

    _cdata.clear();
    }

    Here, they track a stack, so they can keep track of where they are in parsing. Oh, except they don’t. They only push onto the stack when they encounter a data or a value element. When the response only contained values, this worked fine. So for years, it sat like this.

    But when they added data tags, it stopped working, specifically because of how they handle closing tags:

    void XmlRpcResponseParser::OnEndElement(const XML_Char *pszName)
    {
    const string element = pszName;

    if (element == “int” || element == “i4”) {
    top()->integer = atoi(chomp(_cdata).c_str());
    top()->type = XML_RPC_INTEGER;
    } else if (element == “string”) {
    top()->str = _cdata;
    top()->type = XML_RPC_STRING;
    } else if (element == “boolean”) {
    const string value = chomp(_cdata);
    top()->boolean = (value == “1” || value == “true”);
    top()->type = XML_RPC_BOOLEAN;
    } else if (!is_fault && element == “name”) {
    _states.push(&top()->xml_struct[_cdata]);
    } else if (!is_fault && element == “value”) {
    if (!_states.empty()) {
    _states.pop(); // <————- #2
    }
    }

    _cdata.clear();
    }

    Note here that they only pop when they encounter a value element. Which means when they encounter a data element, they push the stack, but never pop it, which gets the whole tree desynced and breaks parsing.

    Since this was discovered, most of the service calls have been migrated to use JSON instead of XML. That “solves” the problem, given that the XML parser is broken. But the XML parser is still used for some calls, and the result is that the service being invoked is constrained in how it’s allowed to change its API- it can’t add data fields to certain responses, because this client will break. Everyone hates this, and someday, the XML endpoints will go away. Someday.

    [Advertisement]
    BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleEarly Prime Day deals on the best PC upgrade parts are already tempting with huge discounts — here’s what I’d buy
    Next Article Apache Spark – unified analytics engine for large-scale data processing

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2024-47893 – VMware GPU Firmware Memory Disclosure

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Diablo 4 is collaborating with Berserk, bringing Kentaro Miura’s legendary manga series to the world of Sanctuary

    News & Updates

    AI-Powered Accessibility Testing: Benefits, and Best Practices

    Development

    Ransomware Attack Hits Union County, Exposing Residents’ Personal Data

    Development

    CVE-2025-3921 – PeproDev Ultimate Profile Solutions WordPress Unauthenticated Data Modification Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    MediaTek Patches Multiple Vulnerabilities Affecting Tablets, Smartphones & TV Chipsets

    May 5, 2025

    MediaTek Patches Multiple Vulnerabilities Affecting Tablets, Smartphones & TV Chipsets

    MediaTek has released critical security patches addressing six significant vulnerabilities affecting a wide range of devices powered by their chipsets.
    The vulnerabilities, disclosed in the company’s …
    Read more

    Published Date:
    May 05, 2025 (1 hour, 55 minutes ago)

    Vulnerabilities has been mentioned in this article.

    CVE-2025-20671

    CVE-2025-20670

    CVE-2025-20668

    CVE-2025-20667

    CVE-2025-20666

    CVE-2025-20665

    How to Build a Component Library in next with Storybook

    December 30, 2024

    Microsoft PC Manager for Windows 11 is now showing Microsoft 365 ads

    April 30, 2025

    Apache ActiveMQ Vulnerability Allows Remote Attackers to Execute Arbitrary Code

    May 1, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.