Skip to content

refactor(hash): dedupe edit-token hashing into shared helper - #80

Open
waterlemonnn wants to merge 1 commit into
AgentPostmortem:mainfrom
waterlemonnn:refactor/edit-token-hash-dedup
Open

refactor(hash): dedupe edit-token hashing into shared helper#80
waterlemonnn wants to merge 1 commit into
AgentPostmortem:mainfrom
waterlemonnn:refactor/edit-token-hash-dedup

Conversation

@waterlemonnn

Copy link
Copy Markdown
Contributor

Closes #31.

Edit tokens were hashed with an inline createHash("sha256").update(token).digest("hex") in six separate places (three local hashToken/getTokenHash helpers, three fully inline). Added hashEditToken() to lib/utils/hash.ts, alongside the existing hashIp/getClientIp, and pointed all six call sites at it:

  • app/api/posts/route.ts
  • app/api/status/[token]/route.ts
  • app/api/edit/[token]/route.ts
  • app/api/posts/edit/[token]/route.ts
  • app/api/admin/posts/[id]/resend-token/route.ts
  • app/edit/[token]/page.tsx

Algorithm is unchanged — same sha256(token) hex digest as before, so existing posts.edit_token_hash rows still resolve. randomBytes token generation in route.ts/resend-token/route.ts was left alone per the issue.

Added a test in lib/utils/hash.test.ts (determinism + a fixed known digest) copying the shape of the existing hashIp/getClientIp tests.

app/api/posts/route.test.ts mocks @/lib/utils/hash with an explicit object instead of importOriginal, so it needed a hashEditToken mock added too — otherwise the route's new import throws inside the mocked module.

Verify

npx tsc --noEmit   # clean
npm run lint       # clean
npm test           # 117/117 passing

Six files each reimplemented SHA-256 edit-token hashing inline or as a
local hashToken()/getTokenHash() helper. Add hashEditToken() to
lib/utils/hash.ts (same algorithm, output stays byte-identical since
existing posts.edit_token_hash rows depend on it) and switch all six
call sites to import it instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Edit-token SHA-256 hashing is duplicated inline across six files

1 participant