File tree Expand file tree Collapse file tree 2 files changed +24
-6
lines changed
Modules/Promasy.Modules.Files/Services Expand file tree Collapse file tree 2 files changed +24
-6
lines changed Original file line number Diff line number Diff line change 11using Promasy . Application . Interfaces ;
2+ using Promasy . Core . Exceptions ;
23
34namespace Promasy . Modules . Files . Services ;
45
@@ -8,17 +9,18 @@ internal class FileStorage : IFileStorage
89
910 public Task < byte [ ] > ReadFileAsync ( string fileName )
1011 {
11- var path = Path . Combine ( Directory . GetCurrentDirectory ( ) , ReportsPath , fileName ) ;
12- if ( ! File . Exists ( path ) )
13- {
14- return Task . FromResult ( Array . Empty < byte > ( ) ) ;
15- }
12+ Ensure . FileNameSafety ( fileName ) ;
1613
17- return File . ReadAllBytesAsync ( path ) ;
14+ var path = Path . Combine ( Directory . GetCurrentDirectory ( ) , ReportsPath , fileName ) ;
15+ return File . Exists ( path )
16+ ? File . ReadAllBytesAsync ( path )
17+ : Task . FromResult ( Array . Empty < byte > ( ) ) ;
1818 }
1919
2020 public string GetPathForFile ( string fileName )
2121 {
22+ Ensure . FileNameSafety ( fileName ) ;
23+
2224 if ( ! Directory . Exists ( Path . Combine ( Directory . GetCurrentDirectory ( ) , ReportsPath ) ) )
2325 {
2426 Directory . CreateDirectory ( Path . Combine ( Directory . GetCurrentDirectory ( ) , ReportsPath ) ) ;
Original file line number Diff line number Diff line change 1+ using System ;
2+
3+ namespace Promasy . Core . Exceptions ;
4+
5+ public static class Ensure
6+ {
7+ public static void FileNameSafety ( string fileName )
8+ {
9+ ArgumentException . ThrowIfNullOrWhiteSpace ( fileName ) ;
10+
11+ if ( fileName . Contains ( ".." ) || fileName . Contains ( '/' ) || fileName . Contains ( '\\ ' ) )
12+ {
13+ throw new ArgumentException ( "Invalid file name" ) ;
14+ }
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments