Skip to content

Commit

Permalink
Check Runner Zip Hash on Upgrade (actions#967)
Browse files Browse the repository at this point in the history
* Check Hash if it exists on runner update
  • Loading branch information
thboop authored Feb 9, 2021
1 parent 50994bb commit 195c2db
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Runner.Listener/SelfUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography;
using GitHub.Services.WebApi;
using GitHub.Services.Common;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;

Expand Down Expand Up @@ -256,6 +258,24 @@ private async Task DownloadLatestRunner(CancellationToken token)
}

// If we got this far, we know that we've successfully downloaded the runner package
// Validate Hash Matches if it is provided
using (FileStream stream = File.OpenRead(archiveFile))
{
if (!String.IsNullOrEmpty(_targetPackage.HashValue))
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] srcHashBytes = await sha256.ComputeHashAsync(stream);
var hash = PrimitiveExtensions.ConvertToHexString(srcHashBytes);
if (hash != _targetPackage.HashValue)
{
// Hash did not match, we can't recover from this, just throw
throw new Exception($"Computed runner hash {hash} did not match expected Runner Hash {_targetPackage.HashValue} for {_targetPackage.Filename}");
}
Trace.Info($"Validated Runner Hash matches {_targetPackage.Filename} : {_targetPackage.HashValue}");
}
}
}
if (archiveFile.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
{
ZipFile.ExtractToDirectory(archiveFile, latestRunnerDirectory);
Expand Down
27 changes: 27 additions & 0 deletions src/Sdk/Common/Common/Utility/HashAlgorithmExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace GitHub.Services.Common
{
public static class HashAlgorithmExtensions
{
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm hashAlg, Stream inputStream)
{
byte[] buffer = new byte[4096];

while (true)
{
int read = await inputStream.ReadAsync(buffer, 0, buffer.Length);
if (read == 0)
break;

hashAlg.TransformBlock(buffer, 0, read, null, 0);
}

hashAlg.TransformFinalBlock(buffer, 0, 0);
return hashAlg.Hash;
}
}
}
14 changes: 14 additions & 0 deletions src/Sdk/Common/Common/Utility/PrimitiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,19 @@ public static String ConvertToHex(String base64String)
var bytes = FromBase64StringNoPadding(base64String);
return BitConverter.ToString(bytes).Replace("-", String.Empty);
}

/// <summary>
/// Converts byte array into a hex string
/// </summary>
public static String ConvertToHexString(byte[] bytes)
{
// Convert byte array to string
var sBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sBuilder.Append(bytes[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
}

0 comments on commit 195c2db

Please sign in to comment.