Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
More strict detection for IsInstalledApp / CurrentlyInstalledVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Apr 7, 2022
1 parent dcbdbdb commit fc07728
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
35 changes: 24 additions & 11 deletions src/Squirrel/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class UpdateManager : IUpdateManager
public string AppDirectory => Path.Combine(_localAppDataDirectoryOverride ?? GetLocalAppDataDirectory(), AppId);

/// <summary>True if the current executable is inside the target <see cref="AppDirectory"/>.</summary>
public bool IsInstalledApp => isUpdateExeAvailable() ? Utility.IsFileInDirectory(SquirrelRuntimeInfo.EntryExePath, AppDirectory) : false;
public bool IsInstalledApp => CurrentlyInstalledVersion() != null;

/// <summary>The directory packages and temp files are stored in.</summary>
protected string PackagesDirectory => Utility.PackageDirectoryForAppDir(AppDirectory);
Expand Down Expand Up @@ -184,21 +184,34 @@ public void RemoveShortcutsForExecutable(string exeName, ShortcutLocation locati
/// <inheritdoc/>
public SemanticVersion CurrentlyInstalledVersion(string executable = null)
{
string appDir;
try {
executable = executable ?? SquirrelRuntimeInfo.EntryExePath;

if (!Utility.IsFileInDirectory(executable, AppDirectory))
return null;

var appDirName = executable.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.FirstOrDefault(x => x.StartsWith("app-", StringComparison.OrdinalIgnoreCase));

if (appDirName == null) return null;
return new SemanticVersion(appDirName.Substring(4));
appDir = AppDirectory;
} catch (InvalidOperationException) {
// app is not installed, see getUpdateExe()
return null;
}

executable = Path.GetFullPath(executable ?? SquirrelRuntimeInfo.EntryExePath);

// check if the application to check is in the correct application directory
if (!Utility.IsFileInDirectory(executable, appDir))
return null;

// check if Update.exe exists in the expected relative location
var baseDir = Path.GetDirectoryName(executable);
if (!File.Exists(Path.Combine(baseDir, "..\\Update.exe")))
return null;

var exePathWithoutAppDir = executable.Substring(appDir.Length);
var appDirName = exePathWithoutAppDir.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
.FirstOrDefault(x => x.StartsWith("app-", StringComparison.OrdinalIgnoreCase));

// check if we are inside an 'app-{ver}' directory and extract version
if (appDirName == null)
return null;

return new SemanticVersion(appDirName.Substring(4));
}

/// <inheritdoc/>
Expand Down
23 changes: 13 additions & 10 deletions test/UpdateManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,20 @@ public async Task WhenUrlResultsInWebExceptionWeShouldThrow()
}
}

[Theory]
[InlineData("C:\\Foo\\Bar\\Test.exe", default(string))]
[InlineData("%LocalAppData%\\theApp\\app-1.0.0.1\\Test.exe", "1.0.0.1")]
[InlineData("%LocalAppData%\\aDifferentApp\\app-1.0.0.1\\Test.exe", default(string))]
public void CurrentlyInstalledVersionTests(string input, string expectedVersion)
[Fact]
public void IsInstalledHandlesInvalidDirectoryStructure()
{
input = Environment.ExpandEnvironmentVariables(input);
var expected = expectedVersion != null ? new SemanticVersion(expectedVersion) : default(SemanticVersion);

using (var fixture = new UpdateManager("http://lol", "theApp")) {
Assert.Equal(expected, fixture.CurrentlyInstalledVersion(input));
using (Utility.WithTempDirectory(out var tempDir)) {
Directory.CreateDirectory(Path.Combine(tempDir, "theApp"));
Directory.CreateDirectory(Path.Combine(tempDir, "theApp", "app-1.0.1"));
Directory.CreateDirectory(Path.Combine(tempDir, "theApp", "wrongDir"));
File.WriteAllText(Path.Combine(tempDir, "theApp", "Update.exe"), "1");
using (var fixture = new UpdateManager("http://lol", "theApp", tempDir)) {
Assert.Null(fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "app.exe")));
Assert.Null(fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "theApp", "app.exe")));
Assert.Null(fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "theApp", "wrongDir", "app.exe")));
Assert.Equal(new SemanticVersion(1, 0, 1, 9), fixture.CurrentlyInstalledVersion(Path.Combine(tempDir, "theApp", "app-1.0.1.9", "app.exe")));
}
}
}

Expand Down

0 comments on commit fc07728

Please sign in to comment.