T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/quantum_nav.py:50
- Finding
- Non-Finite Sensor Input Causes the Safety Decision to Fail Open<![CDATA[ ## Vulnerability Details **File Location**: `scripts/quantum_nav.py:50-121` **Vulnerability Type**: Improper input validation and fail-open safety logic **Risk Level**: High ### Complete Vulnerable Code ```python def compute_kernel(vec_a, vec_b): """Computes similarity using the optical simulator.""" sim = OpticalQuantumSimulator(num_modes=2) kernel_sum = 0 # simplified element-wise comparison for robustness for i in range(min(len(vec_a), len(vec_b))): sim.state[0] = np.exp(1j * vec_a[i]) sim.state[1] = np.exp(1j * vec_b[i]) sim.evolve() sim.interfere() intensities = sim.measure() # Heuristic: Destructive interference at port 2 implies similarity? # Let's use a standard visibility metric. # If phases are equal, port 1 -> bright, port 2 -> dark (depending on BS phase) # We assume standard BS where equal inputs -> port 0 activates. diff = intensities[0] - intensities[1] # Derived physics: diff = 2 * sin(phi_a - phi_b) # We want Kernel = 1 when diff = 0 (identical phases) # We use a linear decay based on the interference contrast # Range of diff is [-2, 2]. similarity = 1.0 - (0.5 * np.abs(diff)) kernel_sum += similarity return kernel_sum / len(vec_a) ``` ```python def decide_action(self, sensor_input): scores = self.classify_terrain(sensor_input) best_match = max(scores, key=scores.get) confidence = scores[best_match] print("\n--- Quantum Kernel Analysis ---") for t, s in scores.items(): print(f" {t}: {s:.4f}") print(f"\nBest Match: {best_match} (Confidence: {confidence:.4f})") # SAFETY FAILSAFE if confidence < self.safety_threshold: return ">>> TRIGGERING FAILSAFE: SAFE MODE (UNCERTAIN TERRAIN) <<<" if "SAFE" in best_match: return f"Action: PROCEED (Terrain is {best_match})" else: return f"Action: AVOID / HALT (Detected {b ...[truncated 3000 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Validate the parsed vector before classification: - Require exactly three values to match the built-in signatures. - Reject empty vectors. - Reject every value for which `math.isfinite(value)` is false. - Enforce the documented input range, such as `0.0 <= value <= 1.0`. 2. Validate all calculated scores and the selected confidence: ```python if not scores or any(not math.isfinite(float(score)) for score in scores.values()): return ">>> TRIGGERING FAILSAFE: SAFE MODE (INVALID CLASSIFICATION) <<<" ``` 3. Make the safety check explicitly fail closed: ```python if not math.isfinite(float(confidence)) or confidence < self.safety_threshold: return ">>> TRIGGERING FAILSAFE: SAFE MODE (UNCERTAIN TERRAIN) <<<" ``` 4. Use exact terrain-state comparisons rather than substring matching: ```python if best_match == "SAFE_FLAT": return f"Action: PROCEED (Terrain is {best_match})" return f"Action: AVOID / HALT (Detected {best_match})" ``` 5. Treat parsing, dimensional, simulation, and scoring errors as safety events. Catch relevant exceptions at the application boundary and return safe mode or halt rather than continuing or crashing. 6. Add regression tests for `nan`, `inf`, `-inf`, empty input, vectors of incorrect length, out-of-range values, and non-finite intermediate scores. Every invalid or anomalous case should deterministically produce a halt or safe-mode decision. ]]>
