fix(review): close client load-TOCTOU + canonicalize If-Match parse (Codex High/Medium)
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m39s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m30s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m39s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m30s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Codex independent review of #263 surfaced two defects the fork review missed: - High — client load TOCTOU: BlockEditor read root metadata (getBlock) and items+ETag (getBlockItemsWithMeta) concurrently, so a concurrent write landing between them (with the items read resolving last) left a stale root paired with a current ETag → the save silently overwrote the concurrent change with no 412. Fix: read items+ETag FIRST, then the root metadata, so the captured ETag is never newer than the root version and any inconsistency fails safe (save 412s → conflict dialog → reload). - Medium — `ParseIfMatch` accepted non-canonical strong tags ("03", "+3", " 3 ") as version 3. An ETag is opaque; only the exact emitted form is valid. Fix: canonical decimal only (`NumberStyles.None` + no leading zeros) → else 400. Tests: new `ConcurrencyHeadersTests` (canonical parse + padded/signed/whitespace/weak/ unquoted/list/overflow/empty → malformed); `ApiResultsTests` gains the 412 mapping case. Existing BlocksScreen tests still green (load reordering is behavior-preserving for the non-concurrent path). Refs #253 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,13 @@ public class ApiResultsTests
|
||||
result.ShouldBeOfType<NotFoundObjectResult>().StatusCode.ShouldBe(404);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToErrorResult_Should_Map_PreconditionFailedError_To_412()
|
||||
{
|
||||
IActionResult result = new PreconditionFailedError("stale").ToErrorResult();
|
||||
result.ShouldBeOfType<ObjectResult>().StatusCode.ShouldBe(412);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToErrorResult_Should_Return_ProblemDetails_For_NotFoundError()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using ErsatzTV.Extensions;
|
||||
using LanguageExt;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NUnit.Framework;
|
||||
using Shouldly;
|
||||
using static LanguageExt.Prelude;
|
||||
|
||||
namespace ErsatzTV.Tests.Extensions;
|
||||
|
||||
[TestFixture]
|
||||
public class ConcurrencyHeadersTests
|
||||
{
|
||||
private static IfMatchCondition Parse(string? ifMatch)
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
if (ifMatch is not null)
|
||||
{
|
||||
context.Request.Headers.IfMatch = ifMatch;
|
||||
}
|
||||
|
||||
return ConcurrencyHeaders.ParseIfMatch(context.Request);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Absent_Header_Is_Absent()
|
||||
{
|
||||
IfMatchCondition result = Parse(null);
|
||||
result.Kind.ShouldBe(IfMatchKind.Absent);
|
||||
result.ExpectedVersion.ShouldBe(Option<int>.None);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Wildcard_Is_Any_And_Forces_Write()
|
||||
{
|
||||
IfMatchCondition result = Parse("*");
|
||||
result.Kind.ShouldBe(IfMatchKind.Any);
|
||||
result.ExpectedVersion.ShouldBe(Option<int>.None);
|
||||
}
|
||||
|
||||
[TestCase("\"0\"", 0)]
|
||||
[TestCase("\"3\"", 3)]
|
||||
[TestCase("\"2147483647\"", int.MaxValue)]
|
||||
public void Canonical_Strong_Tag_Parses_To_Version(string header, int expected)
|
||||
{
|
||||
IfMatchCondition result = Parse(header);
|
||||
result.Kind.ShouldBe(IfMatchKind.Version);
|
||||
result.ExpectedVersion.ShouldBe(Some(expected));
|
||||
}
|
||||
|
||||
// An ETag is opaque: only the exact canonical form we emit is accepted. Padded / signed /
|
||||
// whitespaced / weak / unquoted / overflowing / list values are all rejected as malformed (→ 400),
|
||||
// never silently coerced to a version that would match a stale write.
|
||||
[TestCase("\"03\"")] // leading zero
|
||||
[TestCase("\"+3\"")] // explicit sign
|
||||
[TestCase("\"-3\"")] // negative
|
||||
[TestCase("\" 3 \"")] // surrounding whitespace
|
||||
[TestCase("\"3.0\"")] // non-integer
|
||||
[TestCase("\"\"")] // empty tag
|
||||
[TestCase("3")] // unquoted
|
||||
[TestCase("W/\"3\"")] // weak tag
|
||||
[TestCase("\"3\", \"5\"")] // tag list
|
||||
[TestCase("\"99999999999999999999\"")] // overflows int
|
||||
[TestCase("garbage")]
|
||||
public void Non_Canonical_Values_Are_Malformed(string header)
|
||||
{
|
||||
IfMatchCondition result = Parse(header);
|
||||
result.Kind.ShouldBe(IfMatchKind.Malformed);
|
||||
result.ExpectedVersion.ShouldBe(Option<int>.None);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using LanguageExt;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
@@ -48,11 +49,18 @@ public static class ConcurrencyHeaders
|
||||
}
|
||||
|
||||
// Strong entity-tag of a decimal version, e.g. "3". Weak tags (W/"…") are not honored:
|
||||
// this contract's ETags are always strong.
|
||||
if (value.Length >= 2 && value[0] == '"' && value[^1] == '"' &&
|
||||
int.TryParse(value[1..^1], out int version))
|
||||
// this contract's ETags are always strong. An ETag is an opaque token, so only the exact
|
||||
// canonical form we emit is accepted — a non-negative decimal with no sign, surrounding
|
||||
// whitespace, or leading zeros (`NumberStyles.None` + the leading-zero guard reject "+3",
|
||||
// " 3 ", and "03", which must NOT be treated as equal to the emitted "3").
|
||||
if (value.Length >= 2 && value[0] == '"' && value[^1] == '"')
|
||||
{
|
||||
return new IfMatchCondition(IfMatchKind.Version, version);
|
||||
string inner = value[1..^1];
|
||||
if (inner.Length > 0 && (inner.Length == 1 || inner[0] != '0') &&
|
||||
int.TryParse(inner, NumberStyles.None, CultureInfo.InvariantCulture, out int version))
|
||||
{
|
||||
return new IfMatchCondition(IfMatchKind.Version, version);
|
||||
}
|
||||
}
|
||||
|
||||
return new IfMatchCondition(IfMatchKind.Malformed, 0);
|
||||
|
||||
@@ -863,8 +863,19 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
|
||||
Promise.all([getBlock(blockId), getBlockItemsWithMeta(blockId), getWatermarks(), getGraphicsElements()])
|
||||
.then(([blockData, itemsMeta, watermarkData, graphicsData]) => {
|
||||
// Read items + ETag FIRST, then the root metadata. The ETag must be no newer than every piece of
|
||||
// versioned data the draft is built from (issue #253): if the root were read first and a concurrent
|
||||
// write landed before the items read, the draft would pair a stale root with a current ETag and the
|
||||
// save would silently overwrite that write with no 412. Reading items first makes the ETag ≤ the
|
||||
// root version, so any inconsistency fails safe (the save 412s → conflict dialog → reload).
|
||||
void (async () => {
|
||||
try {
|
||||
const itemsMeta = await getBlockItemsWithMeta(blockId);
|
||||
const [blockData, watermarkData, graphicsData] = await Promise.all([
|
||||
getBlock(blockId),
|
||||
getWatermarks(),
|
||||
getGraphicsElements()
|
||||
]);
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
@@ -879,12 +890,12 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
stopScheduling: blockData.stopScheduling,
|
||||
items: itemsMeta.data.map(itemFromResponse)
|
||||
});
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
} catch (error) {
|
||||
if (active) {
|
||||
setLoadError(messageFromBlockError(error, 'Unable to load block'));
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
|
||||
Reference in New Issue
Block a user