Update page 'Day2 Homework -Karen'

2023-04-03 17:34:00 +02:00
parent 0ca4ab7b76
commit 46bd4cdef9
+24
@@ -158,8 +158,12 @@ hijklmnop
httpd.serve_forever()
```
The reason why the HTTP server code above does not do the same thing as what was seen before is because the `MyRequestHandler` class's do_GET method sends a response with a text/plain content type, and the response body is set to "abcdefg\rhijklmnop". When the browser requests the URL http://localhost:8000, the HTTP server sends the response with the content type text/plain, and the response body containing the string "abcdefg\rhijklmnop". The browser then displays this response as plain text, and interprets the \r character as a carriage return, which causes the browser to display only "hijklmnop".
**8. Think of a way someone might try to commit a crime or hide an embezzlement scheme using the answers to the above two questions. Hint: applications usually output on web pages, but developers and system administrators usually troubleshoot in the console.**
One way someone might try to commit a crime or hide an embezzlement scheme is by manipulating the output that appears in the browser vs. what appear in the console. For instance, a malicious developer could modify the code of a web application to include a string with a \r character followed by some text that would overwrite the previously displayed text in the browser. This could be used to conceal some illegal activity or transactions that are being carried out through the web application.
**9. Think of how you would do input validation to prevent such a crime. Replace the code REPLACE ME here so the tests pass:**
```
@@ -180,5 +184,25 @@ try:
except ValueError as err:
print("✅ test2 pass!")
```
To prevent such a crime, maybe we can validate the input string to ensure that it does not contain any malicious characters such as `\r`.
```
def validate(input_str):
illegal_chars = ["\r"]
for char in input_str:
if char in illegal_chars:
raise ValueError("Crime detected!")
try:
validate("abcdefg")
print("✅ test1 pass!")
except:
print("❌ test1 fail!")
try:
validate("abcdefg\rhijklmnop")
print("❌ test2 fail!")
except ValueError as err:
print("✅ test2 pass!")
```