Commit 82ac6f3
committed
GH-11095: Prevent file writing outside output directory
- Throw exception if '..' is used within the canonical directory.
- Also prevent the absolute directory paths.
- Add tests to verify directory traversal prevention.
- Removed the NOSONAR's from class
- Utilize checkFile for file creation in:
- FileWriteingMessageHandler
- AbstractRemoteFileOutboundGateway
- Add additional test for multiple file navigation
- Add additional test for isAbsolute filepath
- Apply changes from code review
Rename checkFile to newFileInDirectoryIfValid
- Move newFileInDirectoryIfValid to FileUtils
Refactor file validation and clean up comments
- Remove try-catch blocks since checked exceptions are removed.
- Delete Sonar suppression comments to maintain clean code.
- Remove informational comments from tests.
Refactor file validation and add traversal tests
- Simplify path resolution in FileUtils.
- Throw InvalidPathException instead of MessagingException.
- Add directory traversal tests to FileWritingMessageHandlerTests.
- Add directory traversal tests to FtpTests.
- AbstractRemoteFileOutboundGateway does not have tests so,
FTPTests were used as a proxy
- FTP tests showed where a space can be inserted in front of a
directory and still be accepted.
This was remediated by stripping blanks in front of the fileName
- Update the traversal validation code to use the canonical calls to identify changes
It handles the navigation for all character variations, where the homegrown only checked for `..`.
newFileInDirectoryIfValid must return un altered File
Any File returned (after inspection) must contain the original filename
Refactor absolute path validation and tests
- Allow absolute paths in file validation when a directory is provided.
- Fix FileUtils to reject absolute paths only if directory is empty.
- Revert resultFile creation to avoid unintended path validation.
- Refactor synchronizer tests to use Mockito for simpler session mocking.
- Update FTP and synchronizer tests to align with new validation rules.
Reject absolute directory for file sync
- Absolute directory in file sync is reject regardles if parent directory is present.
Move return in FileUtils to outside of try block
- Add NOSONAR flags back to code
- Remove unnecessary afterPropertiesSet and setBeanFactory from test
- Resolve checkstyle error
Refactor file validation for empty directories
- Resolve absolute paths properly when target directories are empty.
- Remove leading whitespace stripping to maintain strict file paths.
- Add tests to verify path traversal logic for empty local directories.
resultFile in the handleRequestMessage must be validated
- Security scanner recommended that resultFile be validated for traversal.
- tempfile also needs to be validated because it can be used instead of
result file in AIL, IGNORE, REPLACE, REPLACE_IF_MODIFIED cases.
Refactor file validation and update tests
Refactor FileUtils to validate paths by comparing canonical and
absolute paths instead of checking if they start with the directory
path. This simplifies the validation logic and removes the need for
the ABSOLUTE_DIR constant.
- Remove redundant tests from AbstractRemoteFileSynchronizerTests
- Add OS-specific directory traversal tests
These tests still fail because the filter routine considers absolute navigation
a security issue
- Update FileWritingMessageHandler to use standard File instantiation
This is based on our discussion that we only needed to test one of the entries
not both
- Refactor helper methods to be static
- Set java.io.tmpdir to tmp dir when on macOS
Java tmpdir defaults to /var however on macOS this is a symlink to /private/var
Thus any test that tests for improper traversal of directories or uses code
that inspects for improper traversal will fail because of the symlink
due to how java implements its canonical resolution
- Atomically replace the destination file with the temporary file,
ensuring data integrity by overwriting any existing version in a single,
indivisible operation.
Resolves Issue: There is a Time-of-Check to Time-of-Use race condition between the
canonical path check in FileUtils.newFileInDirectoryIfValid and the
directory creation/file writing. A local attacker can replace a
valid directory with a symlink during this window, leading to an
Arbitrary File Overwrite.
Consolidate test temp dir config and update tests
- Move Mac OS X test temp dir config to root to reduce duplication.
- Split checkPathOnAbsolute FTP test for OS-specific validation.
- Add comment in FileWritingMessageHandler to clarify path checks.
Move java_tmp prop set for mac to configure method
- Remove blank lines left over from previous commit
Revert build.gradle to 7.1.x-internal
- Move property set to common configuration section
* Some code cleanup and Windows compatibility for tests
Fixes: #110951 parent 5511780 commit 82ac6f3
8 files changed
Lines changed: 226 additions & 10 deletions
File tree
- spring-integration-file/src
- main/java/org/springframework/integration/file
- remote
- gateway
- synchronizer
- support
- test/java/org/springframework/integration/file
- remote/synchronizer
- spring-integration-ftp/src/test/java/org/springframework/integration/ftp/dsl
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
358 | 363 | | |
359 | 364 | | |
360 | 365 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
524 | | - | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
525 | 527 | | |
526 | 528 | | |
527 | 529 | | |
| |||
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
1163 | 1164 | | |
1164 | 1165 | | |
1165 | 1166 | | |
1166 | | - | |
1167 | | - | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
1168 | 1170 | | |
1169 | 1171 | | |
1170 | 1172 | | |
| |||
Lines changed: 20 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
75 | 77 | | |
76 | 78 | | |
77 | 79 | | |
| 80 | + | |
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| |||
461 | 464 | | |
462 | 465 | | |
463 | 466 | | |
464 | | - | |
| 467 | + | |
465 | 468 | | |
466 | 469 | | |
467 | 470 | | |
| |||
556 | 559 | | |
557 | 560 | | |
558 | 561 | | |
559 | | - | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
560 | 570 | | |
561 | 571 | | |
562 | 572 | | |
563 | | - | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
564 | 581 | | |
565 | 582 | | |
566 | 583 | | |
| |||
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| |||
69 | 73 | | |
70 | 74 | | |
71 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
72 | 107 | | |
73 | 108 | | |
74 | 109 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| |||
691 | 693 | | |
692 | 694 | | |
693 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
694 | 709 | | |
695 | 710 | | |
696 | 711 | | |
| |||
Lines changed: 90 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
| |||
273 | 277 | | |
274 | 278 | | |
275 | 279 | | |
276 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
277 | 363 | | |
278 | 364 | | |
279 | 365 | | |
280 | | - | |
| 366 | + | |
281 | 367 | | |
282 | 368 | | |
283 | 369 | | |
| |||
297 | 383 | | |
298 | 384 | | |
299 | 385 | | |
300 | | - | |
| 386 | + | |
301 | 387 | | |
302 | 388 | | |
303 | 389 | | |
| |||
423 | 509 | | |
424 | 510 | | |
425 | 511 | | |
426 | | - | |
| 512 | + | |
427 | 513 | | |
428 | 514 | | |
429 | 515 | | |
| |||
0 commit comments