Skip to content

fix(timing): escape double quotes and backslashes in Server-Timing descriptions - #5478

Merged
yusukebe merged 5 commits into
honojs:mainfrom
MustafaKemal0146:fix/timing-escape-desc-quotes
Nov 4, 2026
Merged

yusukebe merged 5 commits into
honojs:mainfrom
MustafaKemal0146:fix/timing-escape-desc-quotes

Conversation

@MustafaKemal0146

@MustafaKemal0146 MustafaKemal0146 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

The author should do the following, if applicable

  • Add tests
  • Run tests
  • pnpm run check:fix to format and lint the code
  • Add TSDoc/JSDoc to document the code

Description

In src/middleware/timing/timing.ts, metric descriptions (description and valueDescription) were interpolated into desc="..." without escaping quotes or backslashes.

When descriptions contain double quotes (e.g. setMetric(c, 'db', 12.3, 'Query "users"')), the resulting header has invalid syntax:

Server-Timing: db;dur=12.3;desc="Query "users""

According to RFC 9110 Section 5.6.4 and W3C Server Timing Section 2.1, desc is serialized as a quoted-string; inner " and \ characters must be backslash-escaped.

This change escapes backslashes and double quotes in description and valueDescription:

const metric = description
  ? `${name};dur=${dur};desc="${description.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`
  : `${name};dur=${dur}`

This fixes quoted-string escaping per RFC 9110 and matches the quote-escaping approach in Hono's bearer-auth middleware.

Test Plan

Added unit tests in src/middleware/timing/index.test.ts verifying expected header outputs:

  • Numeric metric description: 'db;dur=12.3;desc="Query \\"users\\""'
  • Value-less metric description: 'cache;desc="hit \\"fast\\""'
  • Description with backslashes: 'fs;dur=5.0;desc="path\\\\to\\\\\\"file\\""'
  • Custom totalDescription: 'desc="Total \\"App\\" Time"'

Verification:

  • pnpm exec vitest run src/middleware/timing (20/20 passed)
  • pnpm run check:fix (clean)

Disclosure: Implemented with AI assistance under human review.

Copilot AI lite review requested due to automatic review settings September 28, 2026 20:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved review comments remain, and regression coverage is included.

Review effort: Lite
Findings: None

What changed in this PR

Fixes Server-Timing description serialization by escaping backslashes and double quotes.

Changes:

  • Escapes metric descriptions before header serialization.
  • Adds regression tests for quotes, backslashes, and total descriptions.
File Summary
src/​middleware/​timing/​timing.ts Escapes quoted-string characters in metric descriptions.
src/​middleware/​timing/​index.test.ts Adds serialization tests for escaped descriptions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.19%. Comparing base (e44d0a5) to head (5649ecb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5478   +/-   ##
=======================================
  Coverage   80.18%   80.19%           
=======================================
  Files         150      150           
  Lines       10570    10573    +3     
  Branches     2354     2355    +1     
=======================================
+ Hits         8476     8479    +3     
  Misses       2094     2094           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
yusukebe merged commit 28e7498 into honojs:main Nov 4, 2026
19 checks passed
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.

3 participants