diff --git a/sample-apps/flask-mongo/.env.example b/sample-apps/flask-mongo/.env.example new file mode 100644 index 000000000..35d3d4bab --- /dev/null +++ b/sample-apps/flask-mongo/.env.example @@ -0,0 +1 @@ +AIKIDO_DEBUG=true diff --git a/sample-apps/flask-mongo/Dockerfile b/sample-apps/flask-mongo/Dockerfile new file mode 100644 index 000000000..423bd623b --- /dev/null +++ b/sample-apps/flask-mongo/Dockerfile @@ -0,0 +1,21 @@ +# Use an official Python runtime as a parent image +FROM python:3 + +#Copy code base +COPY ./ /tmp + +# Set the working directory +WORKDIR /app + +# Install dependencies +RUN mv /tmp/sample-apps/flask-mongo/requirements.txt ./ +RUN pip install -r requirements.txt + +# Build and install aikido_firewall from source +WORKDIR /tmp +RUN pip install poetry +RUN make build +RUN pip install ./dist/aikido_firewall-0.1.0.tar.gz +RUN pip list + +WORKDIR /app diff --git a/sample-apps/flask-mongo/README.md b/sample-apps/flask-mongo/README.md new file mode 100644 index 000000000..935350e34 --- /dev/null +++ b/sample-apps/flask-mongo/README.md @@ -0,0 +1,9 @@ +# Flask Sample app w/ MongoDB +Run (with docker-compose installed) : +```bash +docker-compose up --build +``` + +- You'll be able to access the Flask Server at : [localhost:8080](http://localhost:8080) +- To Create a reference test dog use `http://localhost:8080/create/` +- To test the nosql injection go to `http://localhost:8080/auth/` diff --git a/sample-apps/flask-mongo/app.py b/sample-apps/flask-mongo/app.py new file mode 100644 index 000000000..c6dbca572 --- /dev/null +++ b/sample-apps/flask-mongo/app.py @@ -0,0 +1,55 @@ +import aikido_firewall # Aikido package import +aikido_firewall.protect() + +import json +from flask import Flask, render_template, request +from flask_pymongo import PyMongo +from bson import ObjectId + +app = Flask(__name__) +if __name__ == '__main__': + app.run(threaded=True) # Run threaded so we can test our agent's capabilities +app.config["MONGO_URI"] = "mongodb://admin:password@db:27017/my_database?authSource=admin" +mongo = PyMongo(app) + +@app.route("/") +def homepage(): + dogs = mongo.db.dogs.find() + return render_template('index.html', title='Homepage', dogs=dogs) + + +@app.route('/dogpage/') +def get_dogpage(dog_id): + dog = mongo.db.dogs.find_one({"_id": ObjectId(dog_id)}) + return render_template('dogpage.html', title=f'Dog', dog=dog) + +@app.route("/create", methods=['GET']) +def show_create_dog_form(): + return render_template('create_dog.html') + +@app.route("/create", methods=['POST']) +def create_dog(): + new_dog = { + 'dog_name': request.form['dog_name'], + 'pswd': request.form['pswd'] + } + res = mongo.db.dogs.insert_one(new_dog) + return f'Dog with id {res.inserted_id} created successfully' + +@app.route("/auth", methods=['GET']) +def show_auth_form(): + return render_template('auth.html') + +@app.route("/auth", methods=['POST']) +def post_auth(): + data = request.get_json() + dog_info = { + 'dog_name': data.get('dog_name'), + 'pswd': data.get('pswd') + } + dog = mongo.db.dogs.find_one(dog_info) + if dog: + dog_name = dog["dog_name"] + return f'Dog with name {dog_name} authenticated successfully' + else: + return f'Auth failed' diff --git a/sample-apps/flask-mongo/docker-compose.yml b/sample-apps/flask-mongo/docker-compose.yml new file mode 100644 index 000000000..19dc9948c --- /dev/null +++ b/sample-apps/flask-mongo/docker-compose.yml @@ -0,0 +1,40 @@ +version: '3' +services: + db: + image: mongo:latest + container_name: flask_mongo_db + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: admin + MONGO_INITDB_ROOT_PASSWORD: password + MONGO_INITDB_DATABASE: my_database + volumes: + - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro + - db_data:/data/db + networks: + - default_network + + + + backend: + build: + context: ./../../ + dockerfile: ./sample-apps/flask-mongo/Dockerfile + container_name: flask_mongo_backend + command: sh -c "flask --app app run --debug --host=0.0.0.0" + restart: always + volumes: + - .:/app + ports: + - "8080:5000" + depends_on: + - db + networks: + - default_network + +volumes: + db_data: + +networks: + default_network: + driver: bridge diff --git a/sample-apps/flask-mongo/mongo-init.js b/sample-apps/flask-mongo/mongo-init.js new file mode 100644 index 000000000..08a48d0ba --- /dev/null +++ b/sample-apps/flask-mongo/mongo-init.js @@ -0,0 +1,16 @@ +let error = true + +let res = [ + db.dogs.drop(), + db.dogs.createIndex({ dog_name: 1 }, { unique: true }), + db.dogs.createIndex({ pswd: 1 }), + db.dogs.insert({ dog_name: 'Doggo 1', pswd: "xyz" }), + db.dogs.insert({ dog_name: 'Doggo 2 (Superdog)', pswd: "admin_pass" }), +] + +printjson(res) + +if (error) { + print('Error, exiting') + quit(1) +} diff --git a/sample-apps/flask-mongo/requirements.txt b/sample-apps/flask-mongo/requirements.txt new file mode 100644 index 000000000..0ce6b3889 --- /dev/null +++ b/sample-apps/flask-mongo/requirements.txt @@ -0,0 +1,3 @@ +flask==2.3.3 +Flask-PyMongo +cryptography diff --git a/sample-apps/flask-mongo/templates/auth.html b/sample-apps/flask-mongo/templates/auth.html new file mode 100644 index 000000000..76ff1681a --- /dev/null +++ b/sample-apps/flask-mongo/templates/auth.html @@ -0,0 +1,53 @@ + + + + + + + Login + + + +

Log In

+ +
+ +
+ + +

Status:

+ + diff --git a/sample-apps/flask-mongo/templates/create_dog.html b/sample-apps/flask-mongo/templates/create_dog.html new file mode 100644 index 000000000..1a4240d6e --- /dev/null +++ b/sample-apps/flask-mongo/templates/create_dog.html @@ -0,0 +1,19 @@ + + + + + + + Create Dog + + +

Create a Dog

+
+ + + + + +
+ + diff --git a/sample-apps/flask-mongo/templates/dogpage.html b/sample-apps/flask-mongo/templates/dogpage.html new file mode 100644 index 000000000..607995a56 --- /dev/null +++ b/sample-apps/flask-mongo/templates/dogpage.html @@ -0,0 +1,25 @@ + + + + {{ title }} + + + +

{{ title }}

+

Name : {{dog.dog_name }}

+

Password (You normally don't know this) : {{ dog.pswd }}

+

Id : {{dog._id}}

+ + diff --git a/sample-apps/flask-mongo/templates/index.html b/sample-apps/flask-mongo/templates/index.html new file mode 100644 index 000000000..5b66a06e9 --- /dev/null +++ b/sample-apps/flask-mongo/templates/index.html @@ -0,0 +1,48 @@ + + + + {{ title }} + + + +

{{ title }}

+

List

+ + +