Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Fix security issue. SHA1 has collisions, switching to SHA256

Joseph Milazzo126fb57f4d63
diff --git a/API.Tests/Services/CacheServiceTests.cs b/API.Tests/Services/CacheServiceTests.cs
index 642d86e0f..bdb2f4b62 100644
--- a/API.Tests/Services/CacheServiceTests.cs
+++ b/API.Tests/Services/CacheServiceTests.cs
@@ -48,79 +48,79 @@ namespace API.Tests.Services
// }
//string GetCachedPagePath(Volume volume, int page)
- [Fact]
- //[InlineData("", 0, "")]
- public void GetCachedPagePathTest_Should()
- {
- // TODO: Figure out how to test this
- // string archivePath = "flat file.zip";
- // int pageNum = 0;
- // string expected = "cache/1/pexels-photo-6551949.jpg";
- //
- // var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
- // var file = Path.Join(testDirectory, archivePath);
- // var volume = new Volume
- // {
- // Id = 1,
- // Files = new List<MangaFile>()
- // {
- // new()
- // {
- // Id = 1,
- // Chapter = 0,
- // FilePath = archivePath,
- // Format = MangaFormat.Archive,
- // NumberOfPages = 1,
- // }
- // },
- // Name = "1",
- // Number = 1
- // };
- //
- // var cacheService = Substitute.ForPartsOf<CacheService>();
- // cacheService.Configure().CacheDirectoryIsAccessible().Returns(true);
- // cacheService.Configure().GetVolumeCachePath(1, volume.Files.ElementAt(0)).Returns("cache/1/");
- // _directoryService.Configure().GetFilesWithExtension("cache/1/").Returns(new string[] {"pexels-photo-6551949.jpg"});
- // Assert.Equal(expected, _cacheService.GetCachedPagePath(volume, pageNum));
- //Assert.True(true);
- }
-
- [Fact]
- public void GetOrderedChaptersTest()
- {
- // var files = new List<Chapter>()
- // {
- // new()
- // {
- // Number = "1"
- // },
- // new()
- // {
- // Chapter = 2
- // },
- // new()
- // {
- // Chapter = 0
- // },
- // };
- // var expected = new List<MangaFile>()
- // {
- // new()
- // {
- // Chapter = 1
- // },
- // new()
- // {
- // Chapter = 2
- // },
- // new()
- // {
- // Chapter = 0
- // },
- // };
- // Assert.NotStrictEqual(expected, _cacheService.GetOrderedChapters(files));
- }
-
+ // [Fact]
+ // //[InlineData("", 0, "")]
+ // public void GetCachedPagePathTest_Should()
+ // {
+ // // TODO: Figure out how to test this
+ // // string archivePath = "flat file.zip";
+ // // int pageNum = 0;
+ // // string expected = "cache/1/pexels-photo-6551949.jpg";
+ // //
+ // // var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
+ // // var file = Path.Join(testDirectory, archivePath);
+ // // var volume = new Volume
+ // // {
+ // // Id = 1,
+ // // Files = new List<MangaFile>()
+ // // {
+ // // new()
+ // // {
+ // // Id = 1,
+ // // Chapter = 0,
+ // // FilePath = archivePath,
+ // // Format = MangaFormat.Archive,
+ // // NumberOfPages = 1,
+ // // }
+ // // },
+ // // Name = "1",
+ // // Number = 1
+ // // };
+ // //
+ // // var cacheService = Substitute.ForPartsOf<CacheService>();
+ // // cacheService.Configure().CacheDirectoryIsAccessible().Returns(true);
+ // // cacheService.Configure().GetVolumeCachePath(1, volume.Files.ElementAt(0)).Returns("cache/1/");
+ // // _directoryService.Configure().GetFilesWithExtension("cache/1/").Returns(new string[] {"pexels-photo-6551949.jpg"});
+ // // Assert.Equal(expected, _cacheService.GetCachedPagePath(volume, pageNum));
+ // //Assert.True(true);
+ // }
+ //
+ // [Fact]
+ // public void GetOrderedChaptersTest()
+ // {
+ // // var files = new List<Chapter>()
+ // // {
+ // // new()
+ // // {
+ // // Number = "1"
+ // // },
+ // // new()
+ // // {
+ // // Chapter = 2
+ // // },
+ // // new()
+ // // {
+ // // Chapter = 0
+ // // },
+ // // };
+ // // var expected = new List<MangaFile>()
+ // // {
+ // // new()
+ // // {
+ // // Chapter = 1
+ // // },
+ // // new()
+ // // {
+ // // Chapter = 2
+ // // },
+ // // new()
+ // // {
+ // // Chapter = 0
+ // // },
+ // // };
+ // // Assert.NotStrictEqual(expected, _cacheService.GetOrderedChapters(files));
+ // }
+ //
}
}
\ No newline at end of file
diff --git a/API/Extensions/HttpExtensions.cs b/API/Extensions/HttpExtensions.cs
index 1139e0ece..db7bcd370 100644
--- a/API/Extensions/HttpExtensions.cs
+++ b/API/Extensions/HttpExtensions.cs
@@ -21,23 +21,15 @@ namespace API.Extensions
}
/// <summary>
- /// Calculates SHA1 hash for a byte[] and sets as ETag. Ensures Cache-Control: private header is added.
+ /// Calculates SHA256 hash for a byte[] and sets as ETag. Ensures Cache-Control: private header is added.
/// </summary>
/// <param name="response"></param>
/// <param name="content">If byte[] is null or empty, will only add cache-control</param>
public static void AddCacheHeader(this HttpResponse response, byte[] content)
{
- // Calculates SHA1 Hash for byte[]
if (content == null || content.Length <= 0) return;
- using var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
+ using var sha1 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
response.Headers.Add("ETag", string.Concat(sha1.ComputeHash(content).Select(x => x.ToString("X2"))));
-
- // Not Needed with Response Caching
- // if (!response.Headers.Keys.Contains("Cache-Control"))
- // {
- // response.Headers.Add("Cache-Control", "private");
- // }
-
}
}