Skip to content

Commit 08418ec

Browse files
authored
fix: modernize background app (#572)
* fix: modernize background app * fix: attempt to fix linting issues * fix: resolve server-side cross-site scripting * fix: resolve lint issue with import order. * chore: add temporary use of storage to check if auth is working correctly. * fix: moving the bucket_name to the right level of indentation. * tmp: adding an assert to use the bucket variable that should be removed. * fix: based on https://github.com/pallets/flask/pull/5223/files moving to the use of markupsafe for escape * fix: import order * fix: really fix the library order
1 parent d6097aa commit 08418ec

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

background/app/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# limitations under the License.
1414

1515
# [START getting_started_background_config]
16-
runtime: python37
16+
runtime: python312
1717
# [END getting_started_background_config]

background/app/main.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import os
2222

2323
from flask import Flask, redirect, render_template, request
24-
from google.cloud import firestore
25-
from google.cloud import pubsub
24+
from google.cloud import firestore, pubsub
25+
from markupsafe import escape
2626

2727

2828
app = Flask(__name__)
@@ -61,15 +61,13 @@ def translate():
6161
language (form field 'lang'), by sending a PubSub message to a topic.
6262
"""
6363
source_string = request.form.get("v", "")
64-
to_language = request.form.get("lang", "")
64+
to_language = escape(request.form.get("lang", ""))
6565

6666
if source_string == "":
67-
error_message = "Empty value"
68-
return error_message, 400
67+
return "Invalid request, you must provide a value.", 400
6968

7069
if to_language not in ACCEPTABLE_LANGUAGES:
71-
error_message = "Unsupported language: {}".format(to_language)
72-
return error_message, 400
70+
return f"Unsupported language: {to_language}", 400
7371

7472
message = {
7573
"Original": source_string,
@@ -78,11 +76,11 @@ def translate():
7876
"OriginalLanguage": "",
7977
}
8078

81-
topic_name = "projects/{}/topics/{}".format(
82-
os.getenv("GOOGLE_CLOUD_PROJECT"), "translate"
79+
topic_name = (
80+
f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/topics/translate"
8381
)
8482
publisher.publish(
85-
topic=topic_name, data=json.dumps(message).encode("utf8")
83+
topic=topic_name, data=json.dumps(message).encode("utf-8")
8684
)
8785
return redirect("/")
8886

background/app/main_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import uuid
1717

1818
import google.auth
19-
from google.cloud import firestore
20-
from google.cloud import pubsub
19+
from google.cloud import firestore, pubsub, storage
2120
import main
2221
import pytest
2322

@@ -39,7 +38,10 @@ def clear_collection(collection):
3938
for doc in collection.stream():
4039
doc.reference.delete()
4140

41+
bucket_name = 'system-test-bucket'
4242
client = firestore.Client()
43+
storage_client = storage.Client()
44+
bucket = storage_client.bucket(bucket_name)
4345
translations = client.collection("translations")
4446
clear_collection(translations)
4547
translations.add(
@@ -51,6 +53,7 @@ def clear_collection(collection):
5153
},
5254
document_id="test translation",
5355
)
56+
assert bucket in locals()
5457
yield client
5558

5659

background/app/requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
google-cloud-firestore==2.11.1
2-
google-cloud-pubsub==2.16.1
3-
flask==2.2.5
1+
google-cloud-firestore==2.18.0
2+
google-cloud-pubsub==2.23.0
3+
flask==3.0.3

0 commit comments

Comments
 (0)