Skip to content

fix(apps/server/rest): enhance path validation and update CORS settings#77

Merged
99mini merged 7 commits intomainfrom
server-rest/fix/path-attack
Apr 26, 2025
Merged

fix(apps/server/rest): enhance path validation and update CORS settings#77
99mini merged 7 commits intomainfrom
server-rest/fix/path-attack

Conversation

@99mini
Copy link
Copy Markdown
Owner

@99mini 99mini commented Apr 26, 2025

FIX

  • Improve security by updating path validation to use isSafePath, ensuring that invalid paths are properly handled.
  • Correct the isSafePath function to allow empty target paths.
  • Simplify CORS settings for better local development compatibility.

FEAT

  • add jest
  • unit test for utils
  • test befor deploy

@99mini 99mini requested a review from Copilot April 26, 2025 13:14
@99mini 99mini self-assigned this Apr 26, 2025
@99mini 99mini added bug Something isn't working server labels Apr 26, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances security by updating path validation using a new isSafePath function and simplifies CORS settings for improved local development.

  • Replaces isRelativePath with isSafePath across file operations.
  • Updates error messages to a more generic "Invalid path" upon validation failure.
  • Simplifies CORS origins to only include local development endpoints.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
apps/server/rest/src/modules/files/utils/index.ts Introduces and documents the new isSafePath function for path validation.
apps/server/rest/src/modules/files/files.service.ts Updates validations from isRelativePath to isSafePath and adjusts error messages accordingly.
apps/server/rest/src/main.ts Simplifies CORS settings by removing production domain origins for local development.
Files not reviewed (1)
  • apps/server/rest/package.json: Language not supported
Comments suppressed due to low confidence (2)

apps/server/rest/src/modules/files/files.service.ts:67

  • [nitpick] The updated error message 'Invalid path' is generic and may not provide enough context. Consider adding more details to help diagnose the invalid path condition during development.
throw new HttpException('Invalid path', HttpStatus.BAD_REQUEST);

apps/server/rest/src/modules/files/utils/index.ts:9

  • It would be beneficial to include unit tests for isSafePath to verify its behavior with empty, relative, and edge-case paths.
export const isSafePath = (basePath: string, targetPath?: string): boolean => {

@99mini 99mini requested a review from Copilot April 26, 2025 13:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the security of path handling in the REST server by replacing the old isRelativePath function with a new isSafePath verification and simplifies CORS settings to favor local development.

  • Update and rename the path validation function from isRelativePath to isSafePath with improved logic.
  • Update FilesService to consistently use the new path check and throw standardized error messages.
  • Simplify CORS origins in main.ts and add a new Jest config for unit testing.

Reviewed Changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
apps/server/rest/src/modules/files/utils/index.ts Replaces isRelativePath with improved isSafePath validation.
apps/server/rest/src/modules/files/utils/index.test.ts Provides unit tests for the updated isSafePath function.
apps/server/rest/src/modules/files/files.service.ts Updates path validation checks to use isSafePath.
apps/server/rest/src/main.ts Simplifies CORS settings for local development.
apps/server/rest/jest.config.ts Introduces Jest configuration for testing.
Files not reviewed (2)
  • apps/server/rest/package.json: Language not supported
  • apps/server/rest/tsconfig.json: Language not supported
Comments suppressed due to low confidence (2)

apps/server/rest/src/modules/files/files.service.ts:66

  • [nitpick] The error message 'Invalid path' is generic. Consider providing a more specific error message that indicates which part of the path failed validation.
if (!isSafePath(this.basePath, path)) {

apps/server/rest/src/modules/files/utils/index.test.ts:29

  • Consider adding a test case to verify that an undefined targetPath is handled correctly by isSafePath, ensuring comprehensive test coverage for optional parameters.
const emptyPath = '';

@99mini 99mini merged commit 0cca4ea into main Apr 26, 2025
1 check passed
@99mini 99mini deleted the server-rest/fix/path-attack branch April 26, 2025 13:38
99mini added a commit that referenced this pull request Apr 27, 2025
…gs (#77)

* feat(rest/files): update path validation to use isSafePath for improved security

* fix(files/utils): correct isSafePath function to return true for empty targetPath

* fix(rest): update CORS settings to remove unnecessary origins and ensure local development compatibility

* feat: Refactor code structure for improved readability and maintainability

* chore(package): update CI and deploy scripts for correct command execution

* test(files/utils): simplify isSafePath tests by removing redundant basePath declarations

* comment(files/service): add TODO comment for modular error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants