From 8ec080177715e6a3307c800f84e209ddfaa83053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salvador=20D=C3=ADaz=20Fau?= Date: Mon, 23 Sep 2019 11:42:20 +0200 Subject: [PATCH] CustomAbsolutePath canonicalizes relative and absolute paths now. Added CustomPathIsURL and CustomPathIsUNC functions Added checks in CustomPathCanonicalize to avoid buffer overruns and unsupported paths. --- source/uCEFMiscFunctions.pas | 55 ++++++++++++++++++++++++++++++++---- update_CEF4Delphi.json | 2 +- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/source/uCEFMiscFunctions.pas b/source/uCEFMiscFunctions.pas index 1d4214ce..3df31065 100644 --- a/source/uCEFMiscFunctions.pas +++ b/source/uCEFMiscFunctions.pas @@ -128,6 +128,10 @@ function PathIsRelativeUnicode(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAP function GetGlobalMemoryStatusEx(var Buffer: TMyMemoryStatusEx): BOOL; stdcall; external Kernel32DLL name 'GlobalMemoryStatusEx'; function PathCanonicalizeAnsi(pszBuf: LPSTR; pszPath: LPCSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathCanonicalizeA'; function PathCanonicalizeUnicode(pszBuf: LPWSTR; pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathCanonicalizeW'; +function PathIsUNCAnsi(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsUNCA'; +function PathIsUNCUnicode(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsUNCW'; +function PathIsURLAnsi(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsURLA'; +function PathIsURLUnicode(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsURLW'; {$IFNDEF DELPHI12_UP} {$IFDEF WIN64} @@ -142,6 +146,8 @@ function PathCanonicalizeUnicode(pszBuf: LPWSTR; pszPath: LPCWSTR): BOOL; stdcal function CustomPathIsRelative(const aPath : string) : boolean; function CustomPathCanonicalize(const aOriginalPath : string; var aCanonicalPath : string) : boolean; function CustomAbsolutePath(const aPath : string; aMustExist : boolean = False) : string; +function CustomPathIsURL(const aPath : string) : boolean; +function CustomPathIsUNC(const aPath : string) : boolean; function GetModulePath : string; function CefIsCertStatusError(Status : TCefCertStatus) : boolean; @@ -1419,6 +1425,32 @@ begin {$ENDIF} end; +function CustomPathIsURL(const aPath : string) : boolean; +begin + {$IFDEF MSWINDOWS} + {$IFDEF DELPHI12_UP} + Result := PathIsURLUnicode(PChar(aPath + #0)); + {$ELSE} + Result := PathIsURLAnsi(PChar(aPath + #0)); + {$ENDIF} + {$ELSE} + Result := False; + {$ENDIF} +end; + +function CustomPathIsUNC(const aPath : string) : boolean; +begin + {$IFDEF MSWINDOWS} + {$IFDEF DELPHI12_UP} + Result := PathIsUNCUnicode(PChar(aPath + #0)); + {$ELSE} + Result := PathIsUNCAnsi(PChar(aPath + #0)); + {$ENDIF} + {$ELSE} + Result := False; + {$ENDIF} +end; + function CustomPathCanonicalize(const aOriginalPath : string; var aCanonicalPath : string) : boolean; var TempBuffer: array [0..pred(MAX_PATH)] of Char; @@ -1426,17 +1458,22 @@ begin Result := False; aCanonicalPath := ''; + if (length(aOriginalPath) > MAX_PATH) or + (Copy(aOriginalPath, 1, 4) = '\\?\') or + CustomPathIsUNC(aOriginalPath) then + exit; + FillChar(TempBuffer, MAX_PATH * SizeOf(Char), 0); {$IFDEF MSWINDOWS} {$IFDEF DELPHI12_UP} - if PathCanonicalizeUnicode(@TempBuffer[0], PChar(aOriginalPath)) then + if PathCanonicalizeUnicode(@TempBuffer[0], PChar(aOriginalPath + #0)) then begin aCanonicalPath := TempBuffer; Result := True; end; {$ELSE} - if PathCanonicalizeAnsi(@TempBuffer[0], PChar(aOriginalPath)) then + if PathCanonicalizeAnsi(@TempBuffer[0], PChar(aOriginalPath + #0)) then begin aCanonicalPath := TempBuffer; Result := True; @@ -1451,10 +1488,16 @@ var begin if (length(aPath) > 0) then begin - if not(CustomPathIsRelative(aPath) and CustomPathCanonicalize(GetModulePath + aPath, TempPath)) then - TempPath := aPath; - - if aMustExist and not(DirectoryExists(TempPath)) then + if CustomPathIsRelative(aPath) then + begin + if not(CustomPathCanonicalize(GetModulePath + aPath, TempPath)) then + TempPath := aPath; + end + else + if not(CustomPathCanonicalize(aPath, TempPath)) then + TempPath := aPath; + + if aMustExist and not(DirectoryExists(TempPath)) then Result := '' else Result := TempPath; diff --git a/update_CEF4Delphi.json b/update_CEF4Delphi.json index 61b21546..a5ea8f74 100644 --- a/update_CEF4Delphi.json +++ b/update_CEF4Delphi.json @@ -2,7 +2,7 @@ "UpdateLazPackages" : [ { "ForceNotify" : true, - "InternalVersion" : 31, + "InternalVersion" : 32, "Name" : "cef4delphi_lazarus.lpk", "Version" : "76.1.13.0" }