Skip to content

Improve filename sanitization in export with length limits and reserved name handling #78

Open

Description

@adamveld12

Problem

The current filename sanitization in export-assessment.ts is too basic and could cause issues on different operating systems:

Location: packages/client/src/lib/export-assessment.ts:60

const sanitizedLabel = positionLabel.replace(/[^a-zA-Z0-9_-]/g, '_');

Issues:

  1. No length limits (some filesystems have 255 character limits)
  2. Doesn't handle Windows reserved names (CON, PRN, AUX, NUL, COM1-9, LPT1-9)
  3. Doesn't handle Unix reserved names (., ..)
  4. No handling of duplicate filenames in the same ZIP
  5. Could produce empty filenames if input is all special characters

Impact:

  • Files could fail to extract on Windows if using reserved names
  • Very long position names could cause filesystem errors
  • Duplicate position names would overwrite files in ZIP

Example Cases

  • Position name "CON" → Should become "CON_file.jpg" (not "CON.jpg" which is invalid on Windows)
  • Position name "Front/Side View!!!" → Should become "Front_Side_View.jpg" (sanitized + length checked)
  • Position name "..." → Should become "_dot.jpg" (handle Unix reserved)
  • Position name with 300 chars → Should truncate to 200 chars max

Related Code

Lines that need updating:

  • Line 60: Main sanitization
  • Line 72: Photo filename generation
  • Line 86: Notes filename generation

Activity

  1. added
    Bug
    Enhancement
    on Oct 29, 2025
  2. adamveld12 commented on Oct 29, 2025

    @adamveld12
    ContributorAuthor

    Implementation Plan

    Changes to packages/client/src/lib/export-assessment.ts

    1. Add new sanitization function (before generateAssessmentZip):
    /**
     * Sanitize filename for cross-platform compatibility
     * Handles length limits, reserved names, and invalid characters
     */
    function sanitizeFilename(name: string, maxLength = 200): string {
      // Reserved Windows filenames
      const RESERVED_NAMES = new Set([
        'CON', 'PRN', 'AUX', 'NUL',
        'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9',
        'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9',
      ]);
      
      // Remove/replace invalid characters
      let sanitized = name.replace(/[^a-zA-Z0-9_-]/g, '_');
      
      // Remove leading/trailing underscores and multiple consecutive underscores
      sanitized = sanitized.replace(/^_+|_+$/g, '').replace(/_+/g, '_');
      
      // Handle reserved Windows names
      const upperName = sanitized.toUpperCase();
      if (RESERVED_NAMES.has(upperName)) {
        sanitized = `${sanitized}_file`;
      }
      
      // Prevent Unix reserved names
      if (sanitized === '.' || sanitized === '..') {
        sanitized = '_dot';
      }
      
      // Enforce length limit
      if (sanitized.length > maxLength) {
        sanitized = sanitized.substring(0, maxLength);
      }
      
      // Ensure non-empty
      if (sanitized.length === 0) {
        sanitized = 'unnamed';
      }
      
      return sanitized;
    }
    1. Update line 60 - Replace basic sanitization:
    // OLD:
    const sanitizedLabel = positionLabel.replace(/[^a-zA-Z0-9_-]/g, '_');
    
    // NEW:
    const sanitizedLabel = sanitizeFilename(positionLabel);
    1. Add duplicate filename tracking - After line 40 (zip initialization):
    // Track used filenames to prevent duplicates
    const usedFilenames = new Set<string>();
    1. Update line 72 - Handle duplicate filenames:
    // OLD:
    const photoFilename = `${sanitizedLabel}.${extension}`;
    
    // NEW:
    let photoFilename = `${sanitizedLabel}.${extension}`;
    let counter = 1;
    while (usedFilenames.has(photoFilename)) {
      photoFilename = `${sanitizedLabel}_${counter}.${extension}`;
      counter++;
    }
    usedFilenames.add(photoFilename);
    1. Update line 86 - Use same pattern for notes:
    // OLD:
    const notesFilename = `${sanitizedLabel}_notes.txt`;
    
    // NEW:
    let notesFilename = `${sanitizedLabel}_notes.txt`;
    let notesCounter = 1;
    while (usedFilenames.has(notesFilename)) {
      notesFilename = `${sanitizedLabel}_notes_${notesCounter}.txt`;
      notesCounter++;
    }
    usedFilenames.add(notesFilename);

    Verification

    • Run pnpm turbo run lint-fix --filter="@snapscope/client"
    • Run pnpm turbo run build --filter="@snapscope/client"
    • Test with edge cases:
      • Position name "CON"
      • Position name with special chars
      • Very long position name (>200 chars)
      • Duplicate position names

    Commit Message

    fix: Add robust filename sanitization with length limits and reserved names
    
    - Add sanitizeFilename() utility function
    - Handle Windows reserved names (CON, PRN, AUX, etc.)
    - Handle Unix reserved names (., ..)
    - Enforce 200 character length limit
    - Prevent duplicate filenames in ZIP with counter suffix
    - Strip invalid characters and normalize underscores
    
    Fixes #78
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEnhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions