Jump to content

check file extension before upload


irigsoft

Recommended Posts

20 minutes ago, irigsoft said:

Yes, I use it now.

Okay. You can try this approach:

1. 

procedure TMainForm.UniFormReady(Sender: TObject);
begin
  UniFileUploadButton1.JSInterface.JSCode(#1'._checkFile = function(){'#1'.fileInputEl.dom.addEventListener("change", function(e){var accepted = ajaxRequest('#1', "checkFile", {fileName: e.target.files[0].name}, false).responseText == "true"; if (accepted == false){e.stopPropagation()}})};'#1'._checkFile();');
end;

2. 

procedure TMainForm.UniFileUploadButton1AjaxEvent(Sender: TComponent;
  EventName: string; Params: TUniStrings);
begin
  if EventName = 'checkFile' then
  begin
    // Validation logic
    if (Params.Values['fileName'] <> '') and (ExtractFileExt(Params.Values['fileName']) = '.png') then
      UniSession.SendResponse('true')
    else
      UniSession.SendResponse('false');
  end;
end;

3.

procedure TMainForm.UniFileUploadButton1Completed(Sender: TObject;
  AStream: TFileStream);
begin
  ShowMessage('complete');
  (Sender as TUniFileUploadButton).JSInterface.JSCall('_checkFile', []); // Must be added after successful upload
end;

 

Link to comment
Share on other sites

18 minutes ago, Sherzod said:

You can try this approach

Thank You , 

I read code and this logic I found:

1. Select file and create Ajax event

2. If Ajax event fires then server check file extension

 

but is there a way to check the selected file with extension "myfavoriteimage.png" is not just renamed from "virus.exe" to "myfavoriteimage.png"
I am trying to protect the server from malicious actions

Link to comment
Share on other sites

@Sherzod, I make it to work with uniFileUpload Example on Server Side, based on information here: https://en.wikipedia.org/wiki/List_of_file_signatures

FileUpload.zip

procedure UniFileUpload1Completed(Sender: TObject; AStream: TFileStream);
var
  DestName    : string;
  DestFolder  : string;
  dataDyn     : array [0..10] of byte;
  sFileExt,
  sByte       : String;

//convert Byte To String
function bintoAscii(const bin: array of byte): AnsiString;
var i: integer;
begin
  SetLength(Result, Length(bin));
  for i := 0 to Length(bin)-1 do
    Result[1+i] := AnsiChar(bin[i]);
end;

begin
  DestFolder := UniServerModule.StartPath + 'UploadFolder\';
  DestName := DestFolder + ExtractFileName(UniFileUpload1.FileName);

  AStream.Position := 0;
  AStream.Read (dataDyn,SizeOf (dataDyn));

  //convert readed Bytes to String
  sByte := bintoAscii (dataDyn);


  If  POS (#$3C#$3F#$78#$6D#$6C#$20,sByte) > 0 then begin
      //3C 3F 78 6D 6C 20
      sFileExt := '.xml';
  end;
  If  POS (#$25#$50#$44#$46#$2D,sByte) > 0  then begin
      //25 50 44 46 2D
      sFileExt := '.pdf';
  end;
  If  POS (#$89#$50#$4E#$47#$0D#$0A#$1A#$0A,sByte) > 0  then begin
      //89 50 4E 47 0D 0A 1A 0A
      IsImage := True;
      sFileExt := '.png';
  end;
  If  POS (#$42#$4D,sByte) > 0 then begin
      //42 4D
      IsImage := True;
      sFileExt := '.bmp';
  end;
  If  POS (#$FF#$D8#$FF#$E0,sByte) > 0 then begin
      //FF D8 FF E0
      IsImage := True;
      sFileExt := '.jpg';
  end;

  If  (POS (#$FF#$D8#$FF#$E0#$00#$10#$4A#$46#$49#$46#$00#$01,sByte) > 0)
  OR (POS (#$FF#$D8#$FF#$EE,sByte) > 0)
  OR (POS (#$FF#$D8#$FF#$E1 + '????' + #$45#$78#$69#$66#$00#$00,sByte) > 0)
  OR (POS (#$FF#$D8#$FF#$E0,sByte) > 0)
  then begin
      //
      IsImage := True;
      sFileExt := '.jpeg';
  end;


  If  (POS (#$66#$74#$79#$70#$69#$73#$6F#$6D,sByte) > 0) then begin
      //66 74 79 70 69 73 6F 6D
      sFileExt := '.mp4';
  end;


  //check if Declared Extention is Real Extention
  if (ExtractFileExt(UniFileUpload1.FileName) <> sFileExt) then begin
      ShowMessage ('For FILE: ' + UniFileUpload1.FileName
                  + '</br>' + ', real File Type is ' + ANSIUPPErCase (sFileExt)
                  + '</br>' + 'UPLOAD is NOT possible.'
                  );
      AStream.Position := 0;
      //AStream.Size := 0;
      AStream := nil;
  end
  //If OK, then UpLoad
  else begin
            DestFolder:=UniServerModule.StartPath+'UploadFolder\';
            DestName:=DestFolder+ExtractFileName(UniFileUpload1.FileName);
            UniLabel4.Caption:='File Name: '+UniFileUpload1.FileName;
            CopyFile(PChar(AStream.FileName), PChar(DestName), False);
            ShowMessage('File: '+ UniFileUpload1.FileName+' Uploaded to folder: '+DestFolder);
  end;
 

 

If you can help to make it work on Client Side will be nice.

Link to comment
Share on other sites

3 minutes ago, Sherzod said:

What kind of files will you accept? I mean, not to check the files to all types.

All types will be (user will choose in settings file), but if you have an example for one, I'll make it for the others.

Start with video files will be good.

Link to comment
Share on other sites

On 3/4/2023 at 6:21 PM, irigsoft said:

All types will be (user will choose in settings file), but if you have an example for one, I'll make it for the others.

Hello,

I think I found one of the possible solutions. I'll post soon.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...