Skip to content

Commit ec887f0

Browse files
[+] Restrict filetype
[+] Filetype error message CactuseSecurity#2696
1 parent 0282e19 commit ec887f0

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

roles/database/files/sql/idempotent/fworch-texts.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,9 @@ INSERT INTO txt VALUES ('E5422', 'English', 'Entry does not contain all required
31133113
INSERT INTO txt VALUES ('E5423', 'German', 'IP-Adresse/IP-Bereich ist fehlerhaft');
31143114
INSERT INTO txt VALUES ('E5423', 'English', 'IP Address/IP Range malformed');
31153115

3116+
INSERT INTO txt VALUES ('E5430', 'German', 'Hochgeladener Dateityp ist nicht erlaubt');
3117+
INSERT INTO txt VALUES ('E5430', 'English', 'Uploaded Filetype is not allowed');
3118+
31163119
INSERT INTO txt VALUES ('E6001', 'German', 'Der Re-Login war nicht erfolgreich. Haben Sie ein falsches Passwort eingegeben? Schauen Sie für Details bitte in die Logs.');
31173120
INSERT INTO txt VALUES ('E6001', 'English', 'Re-login failed. Did you enter a wrong password? See log for details.');
31183121

roles/ui/files/FWO.UI/Pages/Settings/SettingsModelling.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<div class="form-group row mt-4" data-toggle="tooltip" title="@(userConfig.PureLine("H9055"))">
8484
<label class="col-form-label col-sm-4">@userConfig.GetText("import_app_server"):</label>
8585
<div class="row col-sm-6">
86-
<FileUpload SupportedFileFormats=".csv" AuthorizedRoles="@Roles.Admin" TUploadResult="ErrorBaseModel" UploadCase="FileUploadCase.CustomLogoUpload"></FileUpload>
86+
<FileUpload SupportedFileFormats=".csv" AuthorizedRoles="@Roles.Admin" TUploadResult="CSVFileUploadErrorModel" UploadCase="FileUploadCase.ImportAppServerFromCSV" OnAfterImportResults="OnAfterImportResults" OnError="OnAddAppServerError" OnImportSuccess="OnAppServerImportSuccess"></FileUpload>
8787
</div>
8888
</div>
8989
<hr />

roles/ui/files/FWO.UI/Services/FileUploadService.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,26 @@ public class FileUploadService
2424
private readonly ModellingNamingConvention NamingConvention = new();
2525
private readonly List<AppServerType> AppServerTypes = [];
2626
private string ImportSource = "";
27+
private readonly string AllowedFileFormats;
2728

28-
public FileUploadService(ApiConnection apiConnection, UserConfig userConfig)
29+
public FileUploadService(ApiConnection apiConnection, UserConfig userConfig, string allowedFileFormats)
2930
{
3031
UserConfig = userConfig;
3132
ApiConnection = apiConnection;
3233
NamingConvention = JsonSerializer.Deserialize<ModellingNamingConvention>(userConfig.ModNamingConvention) ?? new();
3334
AppServerTypes = JsonSerializer.Deserialize<List<AppServerType>>(UserConfig.ModAppServerTypes) ?? [];
35+
AllowedFileFormats = allowedFileFormats;
3436
}
3537

3638
public async Task ReadFileToBytes(InputFileChangeEventArgs args)
3739
{
40+
string fileExtension = Path.GetExtension(args.File.Name);
41+
42+
if(!AllowedFileFormats.Contains(fileExtension))
43+
{
44+
throw new ArgumentException(UserConfig.GetText("E5430"));
45+
}
46+
3847
using MemoryStream ms = new();
3948
await args.File.OpenReadStream().CopyToAsync(ms);
4049
UploadedData = ms.ToArray();

roles/ui/files/FWO.UI/Shared/FileUpload.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<label class="btn btn-sm @(UploadDisabled ? "btn-primary" : "btn-success")" for="fileUpload">
5050
@(ModellingHandlerBase.DisplayButton(userConfig, "select_file", Icons.Add, "select_file"))
5151
</label>
52-
<InputFile id="fileUpload" hidden accept="@SupportedFileFormats" OnChange="@SingleUpload" />
52+
<InputFile id="fileUpload" hidden accept="@SupportedFileFormats" OnChange="@(async () => await UploadCustomLogo())" />
5353
@if(InputFileChangeEventArgs is not null && !string.IsNullOrEmpty(InputFileChangeEventArgs.File.Name))
5454
{
5555
<label class="d-inline">@InputFileChangeEventArgs.File.Name</label>
@@ -139,7 +139,7 @@
139139
return;
140140

141141
Loading = true;
142-
FileUploadService fileUploadService = new(apiConnection, userConfig);
142+
FileUploadService fileUploadService = new(apiConnection, userConfig, SupportedFileFormats);
143143

144144
try
145145
{
@@ -169,15 +169,15 @@
169169

170170
private async Task UploadCustomLogo()
171171
{
172-
if(UploadDisabled || InputFileChangeEventArgs is null)
172+
if(InputFileChangeEventArgs is null)
173173
return;
174174

175175
if(InputFileChangeEventArgs.File is null)
176176
return;
177177

178178
Loading = true;
179179

180-
FileUploadService fileUploadService = new(apiConnection, userConfig);
180+
FileUploadService fileUploadService = new(apiConnection, userConfig, SupportedFileFormats);
181181

182182
try
183183
{

0 commit comments

Comments
 (0)