Skip to content

[quality] test: unit tests for sanitizeForPrompt (prompt injection defense) and staleCacheEvents#19713

Open
clubanderson wants to merge 1 commit into
mainfrom
quality/test-sanitize-prompt
Open

[quality] test: unit tests for sanitizeForPrompt (prompt injection defense) and staleCacheEvents#19713
clubanderson wants to merge 1 commit into
mainfrom
quality/test-sanitize-prompt

Conversation

@clubanderson

Copy link
Copy Markdown
Collaborator

Test Improvement

Adds unit tests for two previously untested lib modules:

sanitizeForPrompt.test.ts — 28 test cases (critical: prompt injection defense)

  • Basic sanitization: plain text passthrough, whitespace trim, empty/blank input
  • Angle bracket removal: literal <>, unicode \u003c/\u003e, hex \x3c/\x3e, leading zeros
  • HTML entity encoding: &&amp;, "&quot;, '&#39;
  • Length capping: default 500 limit, custom max, trim-before-cap ordering
  • Prompt injection defense: HTML/XML tag stripping, unicode-escaped injection, combined attack vectors, length-bomb attempts

staleCacheEvents.test.ts — 5 test cases

  • Event dispatching with full detail verification
  • Subscribe/unsubscribe lifecycle
  • Multiple independent subscriber support

Why this matters

sanitizeForPrompt is the primary prompt injection defense — it sanitizes all user-supplied text before interpolation into AI prompts across the mission-control and AI chat features. It had zero test coverage despite being security-critical.


Filed by quality agent (hold-gated mode). Human review required.

…ents

Adds test coverage for two previously untested lib modules:

sanitizeForPrompt.test.ts (28 test cases):
- Basic sanitization: plain text, trim, empty input
- Angle bracket removal: literal <>, unicode \u003c/\u003e,
  hex \x3c/\x3e, leading zeros
- HTML entity encoding: &, quotes
- Length capping: default 500, custom, trim-before-cap
- Prompt injection defense: HTML tags, unicode injection,
  combined vectors, length-bomb attempts

staleCacheEvents.test.ts (5 test cases):
- dispatchStaleCacheCleanupEvent: event dispatching, detail fields
- subscribeToStaleCacheCleanupEvents: subscribe/unsubscribe,
  multiple independent subscribers

Signed-off-by: Quality Agent <quality-agent@kubestellar.io>
Copilot AI review requested due to automatic review settings June 26, 2026 10:05
@kubestellar-prow kubestellar-prow Bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Jun 26, 2026
@kubestellar-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign eeshaansa for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@clubanderson clubanderson added hold Blocked — do not touch quality testing and removed dco-signoff: yes Indicates the PR's author has signed the DCO. labels Jun 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

👋 Hey @clubanderson — thanks for opening this PR!

🤖 This project is developed exclusively using AI coding assistants.

Please do not attempt to code anything for this project manually.
All contributions should be authored using an AI coding tool such as:

This ensures consistency in code style, architecture patterns, test coverage,
and commit quality across the entire codebase.


This is an automated message.

@github-actions

Copy link
Copy Markdown
Contributor

🐝 Hi @clubanderson! I'm kubestellar-hive[bot], an automation bot for this repo.

Trusted users — org members and contributors with write access — can mention @kubestellar-hive in a comment to trigger repo automation.
On issues, that mention queues an automated fix attempt. On pull requests, it records extra context for existing automation.
This is not an interactive Q&A bot, so mentions should be treated as requests for automation rather than a conversation.

Automation may take a moment to start, and follow-up happens through workflow activity rather than chat replies.

@kubestellar-prow kubestellar-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 26, 2026
@netlify

netlify Bot commented Jun 26, 2026

Copy link
Copy Markdown

Deploy Preview for kubestellarconsole ready!

Name Link
🔨 Latest commit dfbd63c
🔍 Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/6a3e4ed16fc7e10008eaf230
😎 Deploy Preview https://deploy-preview-19713.console-deploy-preview.kubestellar.io
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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 adds Vitest unit coverage for two web/src/lib utilities: sanitizeForPrompt (prompt-injection hardening) and staleCacheEvents (observability event dispatch/subscription used during cache cleanup).

Changes:

  • Add unit tests covering sanitization behaviors (escaping/encoding, trimming, and max-length capping) for sanitizeForPrompt.
  • Add unit tests validating kc:stale-cache-cleanup event dispatching and subscribe/unsubscribe behavior for staleCacheEvents.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
web/src/lib/tests/sanitizeForPrompt.test.ts Adds unit tests for prompt-input sanitization behaviors and attack-vector handling.
web/src/lib/tests/staleCacheEvents.test.ts Adds unit tests for stale-cache cleanup event dispatch and subscription lifecycle.

@@ -0,0 +1,100 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
Comment on lines +8 to +17
const makeDetail = (
overrides: Partial<StaleCacheCleanupEventDetail> = {},
): StaleCacheCleanupEventDetail => ({
staleKeysFound: 5,
staleKeysRemoved: 3,
oldestStaleAgeMs: 86400000,
cleanupDurationMs: 42,
timestamp: Date.now(),
...overrides,
})
Comment on lines +1 to +4
import { describe, it, expect } from 'vitest'
import { sanitizeForPrompt } from '../sanitizeForPrompt'

describe('sanitizeForPrompt', () => {
@@ -0,0 +1,100 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
@clubanderson

Copy link
Copy Markdown
Collaborator Author

Closing — superseded by #19719 which provides more comprehensive coverage (55+ test cases vs 28, includes K8s scenarios and boundary conditions).

@kubestellar-prow kubestellar-prow Bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 26, 2026
@kubestellar-prow

Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hold Blocked — do not touch needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. quality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. testing tier/1-lightweight

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants