Skip to content

fix: eliminate Prometheus Counter anti-patterns and security issues - #92

Merged
theinterneti merged 2 commits into
feature/keploy-frameworkfrom
copilot/sub-pr-26-please-work
Nov 13, 2025
Merged

theinterneti merged 2 commits into
feature/keploy-frameworkfrom
copilot/sub-pr-26-please-work

Conversation

Copilot AI commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

Addresses code review feedback from PR #26: improper Counter manipulation, missing type annotations, fragile parsing, and XSS vulnerability.

Changes

Prometheus Counter tracking (prometheus_exporter.py)

  • Replaced _value attribute access with internal state tracking
  • Added _last_request_total, _last_cost_total, _last_savings_total dictionaries
  • Counters now properly increment via .inc() with calculated deltas
# Before: accessing private implementation details
counter = self.request_total.labels(...)
current_value = getattr(counter, '_value', None)
if current_value is not None:
    increment = total - current_value.get()

# After: track state internally
key = (name, "success")
last_total = self._last_request_total.get(key, 0.0)
increment = current_total - last_total
if increment > 0:
    self.request_total.labels(...).inc(increment)
    self._last_request_total[key] = current_total

Type annotations (recorder.py)

  • Added proper types to __aexit__: type[BaseException] | None, BaseException | None, TracebackType | None
  • Removed # type: ignore[no-untyped-def] suppression

PAF parsing (paf_memory.py)

  • Replaced string split with regex: r"- \*\*([A-Z]+-\d+)\*\*:\s*(.+)"
  • Handles formatting variations and edge cases correctly

XSS prevention (test_runner.py)

  • Added html.escape() to test case names and statuses in HTML report generation
  • Prevents injection of malicious content in test reports

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…tions, regex parsing, XSS prevention

- Fix Prometheus Counter anti-pattern by tracking last values internally instead of accessing private _value attribute
- Add proper type annotations to __aexit__ with TracebackType import
- Use regex for more robust PAF markdown parsing (handles edge cases better)
- Add HTML escaping to prevent XSS vulnerabilities in test reports

Addresses feedback from PR #26 review thread

Co-authored-by: theinterneti <169108167+theinterneti@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Phase 1 workflow enhancements for observability and API testing fix: eliminate Prometheus Counter anti-patterns and security issues Nov 13, 2025
Copilot AI requested a review from theinterneti November 13, 2025 00:44
@theinterneti
theinterneti marked this pull request as ready for review November 13, 2025 17:57
Copilot AI review requested due to automatic review settings November 13, 2025 17:57
@theinterneti
theinterneti merged commit 1348d33 into feature/keploy-framework Nov 13, 2025
@theinterneti
theinterneti deleted the copilot/sub-pr-26-please-work branch November 13, 2025 17:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses code quality issues identified in review feedback, focusing on eliminating Prometheus Counter anti-patterns, improving type safety, enhancing parsing robustness, and preventing XSS vulnerabilities.

Key changes:

  • Replaced improper Prometheus Counter _value access with internal state tracking
  • Added proper type annotations to async context manager methods
  • Improved PAF parsing from string splitting to regex-based parsing
  • Added HTML escaping to prevent XSS in test report generation

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

File Description
prometheus_exporter.py Eliminated Counter anti-pattern by tracking last values internally instead of accessing private _value attribute
recorder.py Added proper type hints to __aexit__ parameters, removing type checking suppression
paf_memory.py Replaced fragile string split parsing with regex for more robust PAF entry extraction
test_runner.py Added html.escape() to prevent XSS injection in HTML test reports

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