-
Notifications
You must be signed in to change notification settings - Fork 0
Update app.py #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,116 +1,116 @@ | ||
| import os | ||
|
|
||
| from flask import Flask | ||
| from flask_cors import CORS, cross_origin | ||
| from random import randrange | ||
| import simplejson as json | ||
| import boto3 | ||
| from multiprocessing import Pool | ||
| from multiprocessing import cpu_count | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
| cors = CORS(app, resources={r"/api/*": {"Access-Control-Allow-Origin": "*"}}) | ||
|
|
||
| cpustressfactor = os.getenv('CPUSTRESSFACTOR', 1) | ||
| memstressfactor = os.getenv('MEMSTRESSFACTOR', 1) | ||
| ddb_aws_region = os.getenv('DDB_AWS_REGION') | ||
| ddb_table_name = os.getenv('DDB_TABLE_NAME', "votingapp-restaurants") | ||
|
|
||
| ddb = boto3.resource('dynamodb', region_name=ddb_aws_region) | ||
| ddbtable = ddb.Table(ddb_table_name) | ||
|
|
||
| print("The cpustressfactor variable is set to: " + str(cpustressfactor)) | ||
| print("The memstressfactor variable is set to: " + str(memstressfactor)) | ||
| memeater=[] | ||
| memeater=[0 for i in range(10000)] | ||
|
|
||
| ## https://gist.github.com/tott/3895832 | ||
| def f(x): | ||
| for x in range(1000000 * int(cpustressfactor)): | ||
| x*x | ||
|
|
||
| def readvote(restaurant): | ||
| response = ddbtable.get_item(Key={'name': restaurant}) | ||
| # this is required to convert decimal to integer | ||
| normilized_response = json.dumps(response) | ||
| json_response = json.loads(normilized_response) | ||
| votes = json_response["Item"]["restaurantcount"] | ||
| return str(votes) | ||
|
|
||
| def updatevote(restaurant, votes): | ||
| ddbtable.update_item( | ||
| Key={ | ||
| 'name': restaurant | ||
| }, | ||
| UpdateExpression='SET restaurantcount = :value', | ||
| ExpressionAttributeValues={ | ||
| ':value': votes | ||
| }, | ||
| ReturnValues='UPDATED_NEW' | ||
| ) | ||
| return str(votes) | ||
|
|
||
| @app.route('/') | ||
| def home(): | ||
| return "<h1>Welcome to the Voting App</h1><p><b>To vote, you can call the following APIs:</b></p><p>/api/outback</p><p>/api/bucadibeppo</p><p>/api/ihop</p><p>/api/chipotle</p><b>To query the votes, you can call the following APIs:</b><p>/api/getvotes</p><p>/api/getheavyvotes (this generates artificial CPU/memory load)</p>" | ||
|
|
||
| @app.route("/api/outback") | ||
| def outback(): | ||
| string_votes = readvote("outback") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("outback", votes) | ||
| return string_new_votes | ||
|
|
||
| @app.route("/api/bucadibeppo") | ||
| def bucadibeppo(): | ||
| string_votes = readvote("bucadibeppo") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("bucadibeppo", votes) | ||
| return string_new_votes | ||
|
|
||
| @app.route("/api/ihop") | ||
| def ihop(): | ||
| string_votes = readvote("ihop") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("ihop", votes) | ||
| return string_new_votes | ||
|
|
||
| @app.route("/api/chipotle") | ||
| def chipotle(): | ||
| string_votes = readvote("chipotle") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("chipotle", votes) | ||
| return string_new_votes | ||
|
|
||
| @app.route("/api/getvotes") | ||
| def getvotes(): | ||
| string_outback = readvote("outback") | ||
| string_ihop = readvote("ihop") | ||
| string_bucadibeppo = readvote("bucadibeppo") | ||
| string_chipotle = readvote("chipotle") | ||
| string_votes = '[{"name": "outback", "value": ' + string_outback + '},' + '{"name": "bucadibeppo", "value": ' + string_bucadibeppo + '},' + '{"name": "ihop", "value": ' + string_ihop + '}, ' + '{"name": "chipotle", "value": ' + string_chipotle + '}]' | ||
| return string_votes | ||
|
|
||
| @app.route("/api/getheavyvotes") | ||
| def getheavyvotes(): | ||
| string_outback = readvote("outback") | ||
| string_ihop = readvote("ihop") | ||
| string_bucadibeppo = readvote("bucadibeppo") | ||
| string_chipotle = readvote("chipotle") | ||
| string_votes = '[{"name": "outback", "value": ' + string_outback + '},' + '{"name": "bucadibeppo", "value": ' + string_bucadibeppo + '},' + '{"name": "ihop", "value": ' + string_ihop + '}, ' + '{"name": "chipotle", "value": ' + string_chipotle + '}]' | ||
| print("You invoked the getheavyvotes API. I am eating 100MB * " + str(memstressfactor) + " at every votes request") | ||
| memeater[randrange(10000)] = bytearray(1024 * 1024 * 100 * memstressfactor, encoding='utf8') # eats 100MB * memstressfactor | ||
| print("You invoked the getheavyvotes API. I am eating some cpu * " + str(cpustressfactor) + " at every votes request") | ||
| processes = cpu_count() | ||
| pool = Pool(processes) | ||
| pool.map(f, range(processes)) | ||
| return string_votes | ||
|
|
||
| if __name__ == '__main__': | ||
| app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080))) | ||
| app.debug =True | ||
| import os | ||
| from flask import Flask | ||
| from flask_cors import CORS, cross_origin | ||
| from random import randrange | ||
| import simplejson as json | ||
| import boto3 | ||
| from multiprocessing import Pool | ||
| from multiprocessing import cpu_count | ||
| app = Flask(__name__) | ||
| cors = CORS(app, resources={r"/api/*": {"Access-Control-Allow-Origin": "*"}}) | ||
| cpustressfactor = os.getenv('CPUSTRESSFACTOR', 1) | ||
| memstressfactor = os.getenv('MEMSTRESSFACTOR', 1) | ||
| ddb_aws_region = os.getenv('DDB_AWS_REGION') | ||
| ddb_table_name = os.getenv('DDB_TABLE_NAME', "votingapp-restaurants") | ||
| ddb = boto3.resource('dynamodb', region_name=ddb_aws_region) | ||
| ddbtable = ddb.Table(ddb_table_name) | ||
| print("The cpustressfactor variable is set to: " + str(cpustressfactor)) | ||
| print("The memstressfactor variable is set to: " + str(memstressfactor)) | ||
| memeater=[] | ||
| memeater=[0 for i in range(10000)] | ||
| ## https://gist.github.com/tott/3895832 | ||
| def f(x): | ||
| for x in range(1000000 * int(cpustressfactor)): | ||
| x*x | ||
| def readvote(restaurant): | ||
| response = ddbtable.get_item(Key={'name': restaurant}) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Potential NoSQL Injection detected. Untrusted input is being used in a NoSQL database |
||
| # this is required to convert decimal to integer | ||
| normilized_response = json.dumps(response) | ||
| json_response = json.loads(normilized_response) | ||
| votes = json_response["Item"]["restaurantcount"] | ||
| return str(votes) | ||
| def updatevote(restaurant, votes): | ||
| ddbtable.update_item( | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Potential NoSQL Injection detected. Untrusted input is being used in a NoSQL database
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Potential NoSQL Injection detected. Untrusted input is being used in a NoSQL database |
||
| Key={ | ||
| 'name': restaurant | ||
| }, | ||
| UpdateExpression='SET restaurantcount = :value', | ||
| ExpressionAttributeValues={ | ||
| ':value': votes | ||
| }, | ||
| ReturnValues='UPDATED_NEW' | ||
| ) | ||
| return str(votes) | ||
| @app.route('/') | ||
| def home(): | ||
| return "<h1>Welcome to the Voting App</h1><p><b>To vote, you can call the following APIs:</b></p><p>/api/outback</p><p>/api/bucadibeppo</p><p>/api/ihop</p><p>/api/chipotle</p><b>To query the votes, you can call the following APIs:</b><p>/api/getvotes</p><p>/api/getheavyvotes (this generates artificial CPU/memory load)</p>" | ||
| @app.route("/api/outback") | ||
| def outback(): | ||
| string_votes = readvote("outback") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("outback", votes) | ||
| return string_new_votes | ||
| @app.route("/api/bucadibeppo") | ||
| def bucadibeppo(): | ||
| string_votes = readvote("bucadibeppo") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("bucadibeppo", votes) | ||
| return string_new_votes | ||
| @app.route("/api/ihop") | ||
| def ihop(): | ||
| string_votes = readvote("ihop") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("ihop", votes) | ||
| return string_new_votes | ||
| @app.route("/api/chipotle") | ||
| def chipotle(): | ||
| string_votes = readvote("chipotle") | ||
| votes = int(string_votes) | ||
| votes += 1 | ||
| string_new_votes = updatevote("chipotle", votes) | ||
| return string_new_votes | ||
| @app.route("/api/getvotes") | ||
| def getvotes(): | ||
| string_outback = readvote("outback") | ||
| string_ihop = readvote("ihop") | ||
| string_bucadibeppo = readvote("bucadibeppo") | ||
| string_chipotle = readvote("chipotle") | ||
| string_votes = '[{"name": "outback", "value": ' + string_outback + '},' + '{"name": "bucadibeppo", "value": ' + string_bucadibeppo + '},' + '{"name": "ihop", "value": ' + string_ihop + '}, ' + '{"name": "chipotle", "value": ' + string_chipotle + '}]' | ||
| return string_votes | ||
| @app.route("/api/getheavyvotes") | ||
| def getheavyvotes(): | ||
| string_outback = readvote("outback") | ||
| string_ihop = readvote("ihop") | ||
| string_bucadibeppo = readvote("bucadibeppo") | ||
| string_chipotle = readvote("chipotle") | ||
| string_votes = '[{"name": "outback", "value": ' + string_outback + '},' + '{"name": "bucadibeppo", "value": ' + string_bucadibeppo + '},' + '{"name": "ihop", "value": ' + string_ihop + '}, ' + '{"name": "chipotle", "value": ' + string_chipotle + '}]' | ||
| print("You invoked the getheavyvotes API. I am eating 100MB * " + str(memstressfactor) + " at every votes request") | ||
| memeater[randrange(10000)] = bytearray(1024 * 1024 * 100 * memstressfactor, encoding='utf8') # eats 100MB * memstressfactor | ||
| print("You invoked the getheavyvotes API. I am eating some cpu * " + str(cpustressfactor) + " at every votes request") | ||
| processes = cpu_count() | ||
| pool = Pool(processes) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Problem Fix More info
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Problem Fix More info |
||
| pool.map(f, range(processes)) | ||
| return string_votes | ||
| if __name__ == '__main__': | ||
| app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080))) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. The rule flags the use of host
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. The rule flags the use of host |
||
| app.debug =True | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Enabling an application's debug or test feature makes it easier for developers to find bugs, but it also gives attackers access to detailed information about both the system running the application and users. To make your code secure, do not enable the debug or test feature on production servers. For more information, see CWE-489.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji. Enabling an application's debug or test feature makes it easier for developers to find bugs, but it also gives attackers access to detailed information about both the system running the application and users. To make your code secure, do not enable the debug or test feature on production servers. For more information, see CWE-489. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.
Potential NoSQL Injection detected. Untrusted input is being used in a NoSQL database
operation without proper sanitization. NoSQL databases like MongoDB and DynamoDB are not
immune to injection attacks. User input should never be directly interpolated into query
objects or strings. Instead, use parameterized queries, proper sanitization techniques,
or type conversion specific to the NoSQL database you're using. For MongoDB, consider using
ObjectId() for IDs and validated operators. For DynamoDB, use
boto3.dynamodb.conditionsclasses for safe query construction. Learn more - https://cwe.mitre.org/data/definitions/943.html