Skip to content

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

Merged
clubanderson merged 1 commit into
mainfrom
quality/test-sanitize-prompt
Jun 29, 2026
Merged

[quality] test: unit tests for sanitizeForPrompt (prompt injection defense) and staleCacheEvents#19713
clubanderson merged 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.

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 5975455
🔍 Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/6a42a8ed78c6cd0008c97d90
😎 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
@clubanderson clubanderson force-pushed the quality/test-sanitize-prompt branch from dfbd63c to bc2b11a Compare June 29, 2026 16:51
@kubestellar-prow kubestellar-prow Bot added dco-signoff: yes Indicates the PR's author has signed the DCO. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jun 29, 2026
…fense) and staleCacheEvents

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
@clubanderson clubanderson force-pushed the quality/test-sanitize-prompt branch from bc2b11a to 5975455 Compare June 29, 2026 17:18
@clubanderson clubanderson merged commit 39540f7 into main Jun 29, 2026
28 of 35 checks passed
@kubestellar-prow kubestellar-prow Bot deleted the quality/test-sanitize-prompt branch June 29, 2026 17:29
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution! Your PR has been merged.

Check out what's new:

Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey

@github-actions

Copy link
Copy Markdown
Contributor

✅ Post-Merge Verification: passed

Commit: 39540f74f8f91933105a3036459bbcd814ca0cb0
Specs run: smoke.spec.ts
Report: https://github.com/kubestellar/console/actions/runs/28390702168

@github-actions

Copy link
Copy Markdown
Contributor

Post-merge build verification passed

Both Go and frontend builds compiled successfully against merge commit 39540f74f8f91933105a3036459bbcd814ca0cb0.

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

Labels

dco-signoff: yes Indicates the PR's author has signed the DCO. hold Blocked — do not touch 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