Skip to content

Commit

Permalink
PLAT-12109 add bugsnag integity header (#797)
Browse files Browse the repository at this point in the history
* header ready for testing

* env update
  • Loading branch information
richardelms authored May 10, 2024
1 parent 1ce7100 commit 6f1cb44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 0 additions & 2 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@

BeforeAll do
$api_key = 'a35a2a72bd230ac0aa0f52715bbdc6aa'
Maze.config.enforce_bugsnag_integrity = false

if Maze.config.os&.downcase == 'macos'
# The default macOS Crash Reporter "#{app_name} quit unexpectedly" alert grabs focus which can cause tests to flake.
# This option, which appears to have been introduced in macOS 10.11, displays a notification instead of the alert.
Expand Down
17 changes: 17 additions & 0 deletions src/BugsnagUnity/Delivery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using BugsnagUnity.Payload;
using UnityEngine;
using UnityEngine.Networking;
using System.Security.Cryptography;

namespace BugsnagUnity
{
Expand Down Expand Up @@ -187,6 +188,8 @@ IEnumerator PushToServer(IPayload payload)
{
req.SetRequestHeader("Content-Type", "application/json");
req.SetRequestHeader("Bugsnag-Sent-At", DateTimeOffset.Now.ToString("o", CultureInfo.InvariantCulture));
req.SetRequestHeader("Bugsnag-Integrity", "sha1 " + Hash(body));

foreach (var header in payload.Headers)
{
req.SetRequestHeader(header.Key, header.Value);
Expand Down Expand Up @@ -278,6 +281,20 @@ private byte[] PrepareEventBodySimple(IPayload payload)
return serialisedPayload;
}

private string Hash(byte[] input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(input);
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
}

private bool TruncateBreadcrumbs(Dictionary<string, object> @event, int bytesToRemove)
{
var breadcrumbsList = (@event[EVENT_KEY_BREADCRUMBS] as Dictionary<string, object>[]).ToList();
Expand Down

0 comments on commit 6f1cb44

Please sign in to comment.