Skip to content

Conversation

@contolini
Copy link
Member

The filename passed to the frontend streaming API comes directly from the server and never contains anything nefarious but to play it safe, irregular characters and directory traversal attempts are now stripped out of the filename.

I also took the opportunity to get our legacy unit tests back online. About 80% fail due to outdated code references and/or other mysterious errors. I tagged all the broken ones with a // @broken comment at the top of the file and told the unit test npm script to ignore all files with that tag. I then installed Vitest for future unit tests and wrote a test for the new sanitization functionality.

See #4932

Changes

  • Sanitizes filenames before streaming submission downloads to users.
  • Sets up Vitest for new unit tests.
  • Adds a // @broken comment to the top of outdated legacy tests.
  • Adds a couple npm scripts to run both new and old unit tests.
  • Adds the npm test scripts to the GH workflow.
  • Updates docs w/r/t how unit tests work.

Testing

  1. npm unit-tests and npm unit-tests-legacy should pass.
  2. This PR should run the above scripts as part of the test.yml workflow.

Notes

  • I'm a fan of labeling npm scripts in the test:e2e, test:unit, etc. format but we can discuss that another day

Filenames come from the backend and don't contain dangerous characters
but to play it safe we strip all sketchy characters and ensure there's
no attempts at directory traversal.

See https://github.local/HMDA-Operations/hmda-devops/issues/4932
Also update unit dependencies so that they run with the current
version of React we use.
Our old unit tests were broken and using Jest. Vitest can be used for
future ones. Also adds a test file for the download stream util file.
Copy link
Contributor

@billhimmelsbach billhimmelsbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really great! Approved 🚀

import { describe, expect, it } from 'vitest'
import { sanitizeFileName } from '../downloadStream.js'

describe('sanitizeFileName', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are a champion for getting the tests working / adding new tests for this. 🏆

environment: 'jsdom',
globals: true,
include: ['src/**/__tests__/**/*.{test,spec}.{js,jsx}'],
exclude: ['oldTests/**', 'cypress/**', '**/node_modules/**']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm down for this approach of removing the old tests, then having a separate script to run them while excluding the broken tag ones. 👍

@contolini contolini merged commit a40d0b3 into master Oct 23, 2025
1 check passed
@contolini contolini deleted the 4932-directory-traversal branch October 23, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants