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: Sanitary Paths

    CodeSOD: Sanitary Paths

    July 17, 2024

    When accepting user input for things like, say, accessing the filesystem, you need to do some validation. Bad or inappropriate characters could lead to surprises that no one is going to like.

    So when Christian first spotted this C# method called SanitizePath, he didn’t think much of it. But then he looked at the implementation…

    public static string SanitizePath(string path, char replacementchar) {
    string result = “”;
    try {
    // Split path and filename
    FileInfo fi = new FileInfo(path);
    string filename = FileUtilities.ReplaceInvalidFileNameChars(fi.Name, replacementchar);
    string pathname = FileUtilities.ReplaceInvalidPathChars(fi.DirectoryName, replacementchar);
    result = Path.Combine(pathname, filename);
    } catch (Exception e) {
    Logger.Log(e);
    result = path;
    }
    return result;
    }

    We accept an input path, and attempt to open it using FileInfo. Now, the fun thing about this is that if the path contains any sort of invalid characters, it throws an ArgumentException. But let’s assume there weren’t any invalid characters.

    If there weren’t, and FileInfo was constructed successfully, we then split it by Name and DirectoryName, and replace invalid characters according to some rules not shared here. Then we combine them back together into a full path.

    Now, this isn’t a strictly useless function- what their FileUtilities class considers “invalid” may be application specific, and completely unrelated to what the filesystem allows. They may, for example, want to prevent profanity from being in a filename, a clbuttic option. Though, since it says it replaces invalid chars, I suspect it’s avoiding certain letters- it’s possible that this wants to ensure that the files remain readable on different file systems (many a C# app needs to send text files to a mainframe, even today, and they can get real picky about what characters are in those filenames).

    But let’s look at the exception path. If the filename can’t be opened because it’s invalid, we… log an error and return the input value. So SanitizePath will modify the strings if they are valid file paths, but if they’re invalid file paths, it just returns the invalid file path with no meaningful information for the caller- just a log message for an admin to check eventually.

    And as for how useful this method actually is, well… Christian turned it into a no-op, and nothing about the application’s behavior changed. It has since been removed entirely.

    [Advertisement]
    Otter – Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleCodeSOD: Prefixual
    Next Article User-friendly Segmented Control Component – Fancy Switch

    Related Posts

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4610 – WordPress WP-Members Membership Plugin Stored Cross-Site Scripting Vulnerability

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4824 – TOTOLINK A702R, A3002R, A3002RU HTTP POST Request Handler Buffer Overflow Vulnerability

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Angular Material UI Guide

    Development

    CISA Urges Agencies to Patch Critical “Array Networks” Flaw Amid Active Attacks

    Development

    Hell Gates Open

    Artificial Intelligence

    Elevación del rendimiento de la base de datos: introducción de Query Insights en MongoDB Atlas

    Databases

    Highlights

    Microsoft confirms Windows 11’s new iOS like Start menu layout for “All” apps view

    February 22, 2025

    Windows 11 24H2 and even 23H3 are getting a new Start menu layout for the…

    CVE-2025-4024 – iSourcecode Placement Management System SQL Injection Vulnerability

    April 28, 2025

    The Xbox app on Windows 11 gets a handy feature just in time for the Project Kennan handheld

    May 10, 2025

    CVE-2025-32885 – “GoTenna v1 App Message Injection Vulnerability”

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

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