# Code Injection - CWE-94

Languages frequently have means for including external code directly.

* Import clauses: import code from a library, which in reality is a file somewhere in a list of folders.
* Eval/include/input clauses: include code directly from a text string.

<pre class="language-php"><code class="lang-php">$MessageFile = "cwe-94/messages.out";
if ($_GET["action"] == "NewMessage") {
<strong>    $name = $_GET["name"];
</strong>    $message = $_GET["message"];
    $handle = fopen($MessageFile, "a+");
    fwrite($handle, "&#x3C;b>$name&#x3C;/b> says '$message'&#x3C;hr>\n");
    fclose($handle); echo "Message Saved!&#x3C;p>\n";
} else if ($_GET["action"] == "ViewMessages") {
    include($MessageFile);
}
</code></pre>

```python
from flask import Flask, render_template_string, request
app = Flask(__name__, static_url_path='/static')

@app.route("/")
def home():
    user = request.args.get('user') or None
    template = '<html><head><title>SSTI demo app</title></head><body>'
    
    if user == None:
        template = template + '''
        <h1>Login Form</h1><form>
        <input name="user" value="Username"><br>
        <input type="submit" value="Log In">
        </form>'''.format(user)
    else:
        template = template + '''
        <h1>Hi {}</h1>
        Welcome to the vulnerable app!'''.format(user)
        
    return render_template_string(template)

if __name__ == '__main__':
    app.run()
```
