<!-- Copyright (c) 2026 defconxt. All rights reserved. -->
<!-- Licensed under AGPL-3.0 — see LICENSE file for details. -->
---
name: preventing-injection-attacks-in-code
description: >-
  Implement secure patterns to prevent SQL, NoSQL, OS command, and template injection.
domain: cybersecurity
subdomain: secure-coding
tags:
  - injection
  - sqli
  - command-injection
  - secure-coding
version: "1.0"
author: defconxt
license: AGPL-3.0
metadata:
  mitre-attack: ["T1190", "T1059"]
---

# Preventing Injection Attacks in Code

## Overview

Implement secure patterns to prevent SQL, NoSQL, OS command, and template injection.

## Prerequisites

| Requirement | Install |
|---|---|
| ORM/parameterized queries | DB abstraction |
| Bandit/Semgrep | Static analysis |
| Python 3.10+ | For agent tooling |

## Key Concepts

Injection inserts malicious input into interpreters. Prevention:
parameterized queries, ORMs, argument arrays, template sandboxing.

## Quick Reference

```bash
cursor.execute('SELECT * FROM users WHERE id = %s', (user_id,))
node agent.js scan --file app.py --injection-types sql,os
node agent.js fix --file app.py --finding INJECT-001
node agent.js audit --directory src/
```

## Workflow

1. Identify data paths
2. Parameterized queries
3. Use ORM
4. Argument arrays
5. Template sandbox
6. Static analysis
7. Add tests

## Detection

```yaml
title: Preventing Injection Attacks In Code Detection
id: 8fed6870-c044-4e6a-8dd7-0ea111430586
status: experimental
description: Detects suspicious activity related to preventing injection attacks in code techniques in secure coding context
logsource:
  category: application
  product: webserver
detection:
  selection:
    EventType: error
  condition: selection
level: medium
tags:
  - attack.t1190
  - attack.t1059
  - attack.initial_access
falsepositives:
  - Static analysis tools scanning source code repositories
```

## Verification

- No string concat in SQL
- Subprocess uses arg lists
- Template sandbox enabled
- Zero SAST findings
- Edge cases handled

## References

- OWASP Testing Guide — https://owasp.org/www-project-web-security-testing-guide/
- MITRE ATT&CK — https://attack.mitre.org/
- NIST SP 800-53 — https://csf.tools/reference/nist-sp-800-53/
