Skip to content

Commit 41ee983

Browse files
Duncanwpfleger96
andcommitted
fix(media): count all t tags by field name in Strict cardinality
In Strict mode, count every tag whose field name is 't' before validating its content. The previous implementation counted only valid-valued matching tags, so a proof with both ["t"] (valueless) and ["t","upload"] (valid) silently ignored the malformed instance and admitted a two-tag proof as exactly one. NIP-FI.md:658-666 requires Strict to reject malformed, empty, duplicate, or conflicting instances as evidence_rejected. The fix counts first, gates on cardinality (>1 → DuplicateTag("t")), then validates content (empty/valueless/wrong-verb → InvalidAuthVerb). Permissive keeps the origin/main found_t semantics unchanged. Updated tests: the two malformed-alone Strict tests now expect InvalidAuthVerb (counted as one, content check fires) instead of the stale MissingTag. Added four new regressions: valueless+valid combo, empty-string+valid combo (both reject in Strict), and valueless+valid x combo (confirms x_count increments unconditionally → DuplicateTag("x")). Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 75e9bef commit 41ee983

1 file changed

Lines changed: 161 additions & 25 deletions

File tree

‎crates/buzz-media/src/auth.rs‎

Lines changed: 161 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ pub enum BlossomStrictness {
4545
/// Checks in order:
4646
/// 1. Schnorr signature
4747
/// 2. kind == 24242 and non-empty content
48-
/// 3. `t` tag matches `verb` — exactly one in `Strict`, at-least-one in `Permissive`
48+
/// 3. `t` tag matches `verb`:
49+
/// - `Strict`: count every tag named `t` by field name; require exactly
50+
/// one occurrence; require its content to be non-empty and equal to
51+
/// `verb` (NIP-FI.md:658-666 — malformed/empty/duplicate instances MUST
52+
/// be rejected as `evidence_rejected`).
53+
/// - `Permissive`: at least one valid-valued `t` tag equal to `verb`;
54+
/// valueless/empty tags are ignored (origin/main `found_t` semantics).
4955
/// 4. `expiration` tag present, strictly future, and within the freshness window
5056
/// 5. `created_at` bounded: not more than 5s in the future; not older than the
5157
/// mode-selected window (60s `Strict`, 3600s `Permissive`)
@@ -96,24 +102,37 @@ pub fn verify_blossom_auth_event_for_verb(
96102
let kind = tag.kind().to_string();
97103
match kind.as_str() {
98104
"t" => {
99-
// A `t` tag only counts if it has non-empty content equal to
100-
// the requested verb. A valueless/empty tag cannot satisfy
101-
// verb binding and is ignored for cardinality purposes — it is
102-
// structurally malformed but not an explicit rejection. This
103-
// matches origin/main's `found_t` semantics.
104-
if let Some(v) = tag.content() {
105-
if v.is_empty() {
106-
// Empty string value: does not satisfy the requirement.
107-
} else if v != verb.as_str() {
108-
return Err(MediaError::InvalidAuthVerb);
109-
} else {
110-
t_count = t_count.saturating_add(1);
111-
if strict && t_count > 1 {
112-
return Err(MediaError::DuplicateTag("t"));
105+
if strict {
106+
// Strict (NIP-FI): count EVERY tag named "t" by field name,
107+
// regardless of its content. NIP-FI.md:658-666 requires
108+
// that malformed, empty, duplicate, or conflicting instances
109+
// be rejected as `evidence_rejected`. We count first, gate
110+
// on cardinality, then validate content.
111+
t_count = t_count.saturating_add(1);
112+
if t_count > 1 {
113+
return Err(MediaError::DuplicateTag("t"));
114+
}
115+
// Exactly one t tag: its content must be non-empty and
116+
// equal to the requested verb.
117+
match tag.content() {
118+
Some(v) if !v.is_empty() && v == verb.as_str() => {}
119+
_ => return Err(MediaError::InvalidAuthVerb),
120+
}
121+
} else {
122+
// Permissive: only valid-valued matching tags count
123+
// (origin/main's `found_t` semantics). Valueless/empty
124+
// tags are ignored for cardinality; wrong-verb tags reject.
125+
if let Some(v) = tag.content() {
126+
if v.is_empty() {
127+
// Empty string value: does not satisfy the requirement.
128+
} else if v != verb.as_str() {
129+
return Err(MediaError::InvalidAuthVerb);
130+
} else {
131+
t_count = t_count.saturating_add(1);
113132
}
114133
}
134+
// No content: tag is ignored (not counted, not rejected).
115135
}
116-
// No content: tag is ignored (not counted, not rejected).
117136
}
118137
"expiration" => {
119138
exp_count = exp_count.saturating_add(1);
@@ -1141,15 +1160,15 @@ mod tests {
11411160

11421161
// ── Finding 3: valueless / empty-string t tag must not satisfy verb binding ──
11431162

1144-
/// A `t` tag with no content (`["t"]`) cannot satisfy the verb requirement.
1145-
/// It is ignored for cardinality purposes; `t_count` stays 0 → MissingTag.
1163+
/// A `t` tag with no content (`["t"]`) in Strict mode: counted as one occurrence,
1164+
/// content check fires → `InvalidAuthVerb` (malformed instance, NIP-FI §cardinality).
11461165
#[test]
11471166
fn test_valueless_t_tag_is_not_counted_strict() {
11481167
let keys = Keys::generate();
11491168
let sha256 = "a".repeat(64);
11501169
let now = Timestamp::now().as_secs();
11511170
let exp_str = (now + 55).to_string();
1152-
// ["t"] with no second element — no content, should not satisfy t requirement
1171+
// ["t"] with no second element — no content, must be rejected
11531172
let tags = vec![
11541173
Tag::parse(["t"]).unwrap(),
11551174
Tag::parse(["x", &sha256]).unwrap(),
@@ -1168,9 +1187,9 @@ mod tests {
11681187
Some("relay.example"),
11691188
BlossomStrictness::Strict
11701189
),
1171-
Err(MediaError::MissingTag("t"))
1190+
Err(MediaError::InvalidAuthVerb)
11721191
),
1173-
"valueless t tag must not satisfy t requirement in Strict mode"
1192+
"valueless t tag must be rejected in Strict mode (counted once, content invalid)"
11741193
);
11751194
}
11761195

@@ -1203,8 +1222,8 @@ mod tests {
12031222
);
12041223
}
12051224

1206-
/// A `t` tag with an empty-string value (`["t", ""]`) cannot satisfy the
1207-
/// verb requirement — empty content does not equal any verb.
1225+
/// A `t` tag with an empty-string value (`["t", ""]`) in Strict mode: counted
1226+
/// as one occurrence, content check fires → `InvalidAuthVerb`.
12081227
#[test]
12091228
fn test_empty_string_t_tag_is_not_counted_strict() {
12101229
let keys = Keys::generate();
@@ -1229,9 +1248,9 @@ mod tests {
12291248
Some("relay.example"),
12301249
BlossomStrictness::Strict
12311250
),
1232-
Err(MediaError::MissingTag("t"))
1251+
Err(MediaError::InvalidAuthVerb)
12331252
),
1234-
"empty-string t tag must not satisfy t requirement in Strict mode"
1253+
"empty-string t tag must be rejected in Strict mode (counted once, content invalid)"
12351254
);
12361255
}
12371256

@@ -1265,4 +1284,121 @@ mod tests {
12651284
"valueless x tag must not satisfy hash requirement"
12661285
);
12671286
}
1287+
1288+
// ── Finding 3 R2: mixed malformed+valid t combos must reject in Strict ────
1289+
1290+
/// `["t"]` (valueless) + `["t","upload"]` in Strict: the malformed tag is counted
1291+
/// and content-validated on first encounter; the exact rejection error depends on
1292+
/// tag ordering but any error is correct — no combination may be admitted.
1293+
/// (If malformed comes first: `InvalidAuthVerb`; if valid first: `DuplicateTag("t")`.)
1294+
#[test]
1295+
fn test_strict_rejects_valueless_plus_valid_t_combo() {
1296+
let keys = Keys::generate();
1297+
let sha256 = "a".repeat(64);
1298+
let now = Timestamp::now().as_secs();
1299+
let exp_str = (now + 55).to_string();
1300+
let tags = vec![
1301+
Tag::parse(["t"]).unwrap(), // valueless — counted in Strict
1302+
Tag::parse(["t", "upload"]).unwrap(), // valid — but two t tags total
1303+
Tag::parse(["x", &sha256]).unwrap(),
1304+
Tag::parse(["expiration", &exp_str]).unwrap(),
1305+
Tag::parse(["server", "relay.example"]).unwrap(),
1306+
];
1307+
let event = EventBuilder::new(Kind::from(24242), "Upload bypass attempt")
1308+
.tags(tags)
1309+
.sign_with_keys(&keys)
1310+
.unwrap();
1311+
let result = verify_blossom_upload_auth(
1312+
&event,
1313+
&sha256,
1314+
Some("relay.example"),
1315+
BlossomStrictness::Strict,
1316+
);
1317+
assert!(
1318+
result.is_err(),
1319+
"valueless+valid t combo must be rejected in Strict mode, got Ok"
1320+
);
1321+
// The first tag is valueless → InvalidAuthVerb fires before the second tag
1322+
// is seen; if ordering were reversed it would be DuplicateTag("t"). Both are
1323+
// valid evidence_rejected-class outcomes — what matters is admission is denied.
1324+
assert!(
1325+
matches!(
1326+
result,
1327+
Err(MediaError::InvalidAuthVerb) | Err(MediaError::DuplicateTag("t"))
1328+
),
1329+
"expected InvalidAuthVerb or DuplicateTag(t), got unexpected error variant"
1330+
);
1331+
}
1332+
1333+
/// `["t",""]` (empty-string) + `["t","upload"]` in Strict: same reasoning —
1334+
/// any error is correct; admission must be denied.
1335+
#[test]
1336+
fn test_strict_rejects_empty_plus_valid_t_combo() {
1337+
let keys = Keys::generate();
1338+
let sha256 = "a".repeat(64);
1339+
let now = Timestamp::now().as_secs();
1340+
let exp_str = (now + 55).to_string();
1341+
let tags = vec![
1342+
Tag::parse(["t", ""]).unwrap(), // empty-string — counted in Strict
1343+
Tag::parse(["t", "upload"]).unwrap(), // valid — but two t tags total
1344+
Tag::parse(["x", &sha256]).unwrap(),
1345+
Tag::parse(["expiration", &exp_str]).unwrap(),
1346+
Tag::parse(["server", "relay.example"]).unwrap(),
1347+
];
1348+
let event = EventBuilder::new(Kind::from(24242), "Upload bypass attempt")
1349+
.tags(tags)
1350+
.sign_with_keys(&keys)
1351+
.unwrap();
1352+
let result = verify_blossom_upload_auth(
1353+
&event,
1354+
&sha256,
1355+
Some("relay.example"),
1356+
BlossomStrictness::Strict,
1357+
);
1358+
assert!(
1359+
result.is_err(),
1360+
"empty-string+valid t combo must be rejected in Strict mode, got Ok"
1361+
);
1362+
assert!(
1363+
matches!(
1364+
result,
1365+
Err(MediaError::InvalidAuthVerb) | Err(MediaError::DuplicateTag("t"))
1366+
),
1367+
"expected InvalidAuthVerb or DuplicateTag(t), got unexpected error variant"
1368+
);
1369+
}
1370+
1371+
/// `["x"]` (valueless) + `["x", sha256]` (valid) in Strict: `x_count` increments
1372+
/// unconditionally for both occurrences → `DuplicateTag("x")`. This confirms
1373+
/// the x arm already handles mixed malformed+valid correctly.
1374+
#[test]
1375+
fn test_strict_rejects_valueless_plus_valid_x_combo() {
1376+
let keys = Keys::generate();
1377+
let sha256 = "a".repeat(64);
1378+
let now = Timestamp::now().as_secs();
1379+
let exp_str = (now + 55).to_string();
1380+
let tags = vec![
1381+
Tag::parse(["t", "upload"]).unwrap(),
1382+
Tag::parse(["x"]).unwrap(), // valueless — counted unconditionally
1383+
Tag::parse(["x", &sha256]).unwrap(), // valid — but makes x_count = 2
1384+
Tag::parse(["expiration", &exp_str]).unwrap(),
1385+
Tag::parse(["server", "relay.example"]).unwrap(),
1386+
];
1387+
let event = EventBuilder::new(Kind::from(24242), "Upload bypass attempt")
1388+
.tags(tags)
1389+
.sign_with_keys(&keys)
1390+
.unwrap();
1391+
assert!(
1392+
matches!(
1393+
verify_blossom_upload_auth(
1394+
&event,
1395+
&sha256,
1396+
Some("relay.example"),
1397+
BlossomStrictness::Strict
1398+
),
1399+
Err(MediaError::DuplicateTag("x"))
1400+
),
1401+
"valueless+valid x combo must be rejected as DuplicateTag in Strict mode"
1402+
);
1403+
}
12681404
}

0 commit comments

Comments
 (0)