Skip to content

Commit a5b751f

Browse files
chrisd8088dscho
andcommitted
script/windows-installer: check Git program path
When installing and uninstalling Git LFS, the Git LFS program configures its "clean" and "smudge" Git filters, and to do this it executes Git so as to change the Git global configuration (or, optionally, the system-wide, local user, or worktree-specific configuration). The Git program executed is the first one found using the PATH environment variable (and, on Windows, the PATHEXT environment variable). Therefore, when installing Git LFS as an administrator with elevated privileges, final responsibility lies with the administrator to ensure there are no compromised executables in their system PATH. For instance, on Linux the "secure_path" configuration value might be set in /etc/sudoers before running the command "sudo git lfs install --system". We can, however, attempt to assist the administrator on Windows where we provide a dedicated installer and also anticipate that Git will be installed under a common set of directories. For that reason we update our Inno Setup installer script so that if it detects that the Git program found with the relevant PATH and PATHEXT environment variables (either the user or system ones, depending on the user's role) is not within either of the "C:\Program Files" or "C:\Program Files (x86)" directories, then a warning is displayed and the user prompted to decide whether to continue. And for convenience, we now report a failure message if no Git program is found, which avoids subsequent errors during the installation or uninstallation steps for any user. Note, though, that if a Windows administrator runs the "git-lfs.exe install" command manually, the checks we are adding to the Inno Setup script will not be performed, and the situation then is no different than a macOS or Linux user running "sudo git-lfs install" without confidence that the system PATH and installed Git binary are already secure. Co-authored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
1 parent e06653d commit a5b751f

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

script/windows-installer/inno-setup-git-lfs-installer.iss

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ WizardSmallImageFile=git-lfs-logo.bmp
5959
Name: "english"; MessagesFile: "compiler:Default.isl"
6060

6161
[Files]
62-
Source: {#PathToX86Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsX86
63-
Source: {#PathToX64Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsX64
64-
Source: {#PathToARM64Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsARM64
62+
Source: {#PathToX86Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsX86 and GitFoundInPath
63+
Source: {#PathToX64Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsX64 and GitFoundInPath
64+
Source: {#PathToARM64Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsARM64 and GitFoundInPath
6565

6666
[Registry]
6767
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{app}"; Check: IsAdminLoggedOn and NeedsAddPath('{app}')
@@ -101,6 +101,56 @@ begin
101101
Result := Pos(';' + UpperCase(ParamExpanded) + '\;', ';' + UpperCase(OrigPath) + ';') = 0;
102102
end;
103103
104+
// Verify that a Git executable is found in the PATH, and if it does not
105+
// reside in either 'C:\Program Files' or 'C:\Program Files (x86)', warn
106+
// the user in case it is not the Git installation they expected.
107+
function GitFoundInPath(): boolean;
108+
var
109+
PFiles32,PFiles64: string;
110+
PathEnv,Path: string;
111+
PathExt,Ext: string;
112+
i,j: integer;
113+
begin
114+
Result := False;
115+
PFiles32 := ExpandConstant('{commonpf32}\')
116+
PFiles64 := ExpandConstant('{commonpf64}\')
117+
118+
PathEnv := GetEnv('PATH') + ';';
119+
repeat
120+
i := Pos(';', PathEnv);
121+
Path := Copy(PathEnv, 1, i-1) + '\git';
122+
PathEnv := Copy(PathEnv, i+1, Length(PathEnv)-i);
123+
124+
PathExt := GetEnv('PATHEXT') + ';';
125+
repeat
126+
j := Pos(';', PathExt);
127+
Ext := Copy(PathExt, 1, j-1);
128+
PathExt := Copy(PathExt, j+1, Length(PathExt)-j);
129+
130+
if FileExists(Path + Ext) then begin
131+
if (Pos(PFiles32, Path) = 1) or (Pos(PFiles64, Path) = 1) then begin
132+
Result := True;
133+
Exit;
134+
end;
135+
Log('Warning: Found Git in unexpected location: "' + Path + Ext + '"');
136+
Result := (SuppressibleMsgBox(
137+
'An executable Git program was found in an unexpected location outside of Program Files:' + #13+#10 +
138+
' "' + Path + Ext + '"' + #13+#10 +
139+
'If this looks dubious, Git LFS should not be registered using it.' + #13+#10 + #13+#10 +
140+
'Do you want to register Git LFS using this Git program?',
141+
mbConfirmation, MB_YESNO, IDNO) = IDYES);
142+
if Result then
143+
Log('Using Git found at: "' + Path + Ext + '"')
144+
else
145+
Log('Refusing to use Git found at: "' + Path + Ext + '"');
146+
Exit;
147+
end;
148+
until Result or (PathExt = '');
149+
until Result or (PathEnv = '');
150+
SuppressibleMsgBox(
151+
'Could not find Git; can not proceed.', mbError, MB_OK, IDOK);
152+
end;
153+
104154
// Runs the lfs initialization.
105155
procedure InstallGitLFS();
106156
var
@@ -122,10 +172,14 @@ function InitializeUninstall(): Boolean;
122172
var
123173
ResultCode: integer;
124174
begin
125-
Exec(
126-
ExpandConstant('{cmd}'),
127-
ExpandConstant('/C ""{app}\git-lfs.exe" uninstall"'),
128-
'', SW_HIDE, ewWaitUntilTerminated, ResultCode
129-
);
130-
Result := True;
175+
Result := False;
176+
177+
if GitFoundInPath() then begin
178+
Exec(
179+
ExpandConstant('{cmd}'),
180+
ExpandConstant('/C ""{app}\git-lfs.exe" uninstall"'),
181+
'', SW_HIDE, ewWaitUntilTerminated, ResultCode
182+
);
183+
Result := True;
184+
end;
131185
end;

0 commit comments

Comments
 (0)