Skip to content

Commit 2078564

Browse files
fix(text): narrow hardening to compact text bounds
Co-authored-by: Cameron Beeley <[email protected]>
1 parent b7f479a commit 2078564

6 files changed

Lines changed: 6 additions & 86 deletions

File tree

‎src/clawsweeper-text.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ export function truncateText(value: unknown, maxLength: number): string {
1111
export function trimMiddle(text: string, maxLength: number): string {
1212
if (text.length <= maxLength) return text;
1313
const edge = Math.floor((maxLength - 120) / 2);
14-
// For maxLength < 120 the head/tail split has no room: `edge` goes negative, the slices
15-
// read from the wrong ends (and `slice(-0)` returns the whole string), and the reported
16-
// "truncated N" count exceeds text.length. Fall back to a simple head truncation.
17-
if (edge <= 0) return truncateText(text, Math.max(0, maxLength));
1814
return `${text.slice(0, edge)}\n\n... truncated ${text.length - edge * 2} chars ...\n\n${text.slice(-edge)}`;
1915
}
2016

‎src/repair/comment-router-utils.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,6 @@ export function isAllowedMutationActor(login: JsonValue, trustedBots: Iterable<s
407407
.trim()
408408
.toLowerCase();
409409
if (!actor) return false;
410-
// Security boundary: compare the submitted actor exactly. Do not use normalizeGitHubActor
411-
// here because its grouping semantics intentionally collapse repeated trailing bot suffixes.
412410
for (const trustedBot of trustedBots) {
413411
if (
414412
String(trustedBot ?? "")
@@ -421,14 +419,10 @@ export function isAllowedMutationActor(login: JsonValue, trustedBots: Iterable<s
421419
}
422420

423421
export function normalizeGitHubActor(login: JsonValue) {
424-
return (
425-
String(login ?? "")
426-
.trim()
427-
.toLowerCase()
428-
// This helper groups command requesters; mutation authorization uses the exact raw actor.
429-
// Strip ALL trailing [bot] suffixes so grouping remains idempotent.
430-
.replace(/(\[bot\])+$/i, "")
431-
);
422+
return String(login ?? "")
423+
.trim()
424+
.toLowerCase()
425+
.replace(/\[bot\]$/i, "");
432426
}
433427

434428
export function isGitHubAppIntegrationAuthError(message: JsonValue) {

‎src/repair/text-utils.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ export function slug(value: unknown, fallback = "unknown", maxLength = 120) {
2727
.toLowerCase()
2828
.replace(/[^a-z0-9_.-]+/g, "-")
2929
.replace(/^-+|-+$/g, "")
30-
.slice(0, maxLength)
31-
// Re-trim: slicing to maxLength can cut on a "-" boundary and re-introduce a trailing
32-
// dash, which made slug non-idempotent (slug(slug(x)) !== slug(x)).
33-
.replace(/-+$/g, "") || fallback
30+
.slice(0, maxLength) || fallback
3431
);
3532
}

‎test/clawsweeper-text.test.ts‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎test/repair/comment-router-utils.test.ts‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,26 +1435,12 @@ test("mutation actor guard accepts only trusted bot identities", () => {
14351435
assert.equal(normalizeGitHubActor("ClawSweeper[bot]"), "clawsweeper");
14361436
assert.equal(isAllowedMutationActor("ClawSweeper[bot]", trustedBots), true);
14371437
assert.equal(isAllowedMutationActor("clawsweeper[bot]", trustedBots), true);
1438-
assert.equal(isAllowedMutationActor("clawsweeper[bot][bot]", trustedBots), false);
14391438
assert.equal(isAllowedMutationActor("clawsweeper", trustedBots), false);
14401439
assert.equal(isAllowedMutationActor("openclaw-clawsweeper[bot]", trustedBots), true);
14411440
assert.equal(isAllowedMutationActor("steipete", trustedBots), false);
14421441
assert.equal(isAllowedMutationActor("github-actions[bot]", trustedBots), false);
14431442
});
14441443

1445-
test("normalizeGitHubActor is idempotent for repeated [bot] suffixes", () => {
1446-
// Single-suffix strip left a residual on a doubled suffix, so a second pass changed it.
1447-
assert.equal(normalizeGitHubActor("x[bot][bot]"), "x");
1448-
const once = normalizeGitHubActor("x[bot][bot]");
1449-
assert.equal(normalizeGitHubActor(once), once);
1450-
// Unregressed: a single real bot suffix still strips exactly once.
1451-
assert.equal(normalizeGitHubActor("foo[bot]"), "foo");
1452-
assert.equal(normalizeGitHubActor(" Foo[bot] "), "foo");
1453-
assert.equal(normalizeGitHubActor("x[bot][bot]tail"), "x[bot][bot]tail");
1454-
assert.equal(normalizeGitHubActor(null), "");
1455-
assert.equal(normalizeGitHubActor(123), "123");
1456-
});
1457-
14581444
test("mutation actor guard recognizes GitHub App integration auth shape", () => {
14591445
assert.equal(
14601446
isGitHubAppIntegrationAuthError("gh: Resource not accessible by integration (HTTP 403)"),

‎test/repair/text-utils.test.ts‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { compactText, slug } from "../../dist/repair/text-utils.js";
4+
import { compactText } from "../../dist/repair/text-utils.js";
55

66
test("compactText never exceeds maxLength, even for tiny caps", () => {
77
for (const n of [0, 1, 2, 3, 5, 16]) {
@@ -17,26 +17,3 @@ test("compactText never exceeds maxLength, even for tiny caps", () => {
1717
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 16), "abcdefghijklm...");
1818
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 17), "abcdef ... uvwxyz");
1919
});
20-
21-
test("slug is idempotent (a trailing dash is not re-introduced by the length slice)", () => {
22-
for (const [value, max] of [
23-
["a a", 2],
24-
["aa----bb", 3],
25-
["My Repo Name!!", 6],
26-
["...", 2],
27-
] as const) {
28-
const once = slug(value, "x", max);
29-
assert.equal(
30-
slug(once, "x", max),
31-
once,
32-
`slug not idempotent for ${JSON.stringify(value)}@${max}`,
33-
);
34-
assert.ok(
35-
!once.endsWith("-") || once === "x",
36-
`slug left a trailing dash: ${JSON.stringify(once)}`,
37-
);
38-
}
39-
// Unregressed: normal slugs are unchanged.
40-
assert.equal(slug("my-repo-name"), "my-repo-name");
41-
assert.equal(slug("a---b", "x", 3), "a");
42-
});

0 commit comments

Comments
 (0)