0

I am trying to make a CLI Python App that uses the Spotify API and I have implemented the Authorization Code Flow (called like that in their docs) which from my understanding is basically:

  1. create a url for the user to copy paste into their Web Browser

  2. After user logins, it redirects to a specified url (Like http://localhost:8000) and it has the code written at the end of the url.

  3. user copy pastes that code which is the auth code into my App and we have the auth and refresh token all set

So what I don't like is that manual labor so I created a basic webserver that listens to the redirect url and grabs the code automatically. It handles ONLY one request using a specified method for that.

from http.server import BaseHTTPRequestHandler, HTTPServer
class WebServer():
    authorization_code = None
def __init__(self, hostName, serverPort):
    self.hostName = hostName
    self.serverPort = serverPort
    self.server = HTTPServer((self.hostName, self.serverPort), CallbackHandler)

def get_auth_code(self):
    self.server.handle_request()

class CallbackHandler(BaseHTTPRequestHandler):
def do_GET(self):
    WebServer.authorization_code = self.path.split('?')[1].split('=')[1]
    self.send_response(200)
    self.send_header('Content-type', 'text/html')
    self.end_headers()
    self.wfile.write(b'<html><body><h1>Authorization Successful!</h1></body>    
    </html>')

My question comes in to parts:

  1. Is this kind of thing "safe"? like the webserver is not exposed to the internet it's only local so there is absolutely no way for an attacker to get in... right? (Not paranoid just curious about hacking)

  2. I wanna understand security in a more general way. Suppose I have an http Web Server at a port that is port forwarded to the open internet for anyone to see. the Web Server does something veery simple, like returning hello world at every Get request or hand out a NON-sensitive file. Does this kind of scenario has any other vulnerability? like a hacker gaining access to my computer/network? doing any "magic" tricks that I don't get? I imagine that absolute worst case scenario is a hacker just getting my non-sensitive file... Like I've read a lot about SQL injection attacks and HTTP server in python following symbolic links etc etc... but in a simple scenario like this where I don't care about the actual data being transferred neither my script does anything advanced exploitable, is the rest of my PC/files safe ?

Ony10
  • 3
  • 3
  • better to use already existing web framework which handle these cases than reinventing the whole web framework – sahasrara62 Jun 11 '23 at 17:03
  • "Does this kind of scenario has any other vulnerability? like a hacker gaining access to my computer/network? " - yes. Web servers are programs, programs have bugs and exploits. People use automated tools to scan web servers for known bugs and exploits to infect the machines running the servers. – Dave S Jun 11 '23 at 17:14
  • You can automate this by using the python module Flask, like in [this answer](https://stackoverflow.com/a/75292843/7111585). – Ximzend Jun 11 '23 at 17:28
  • Well the purpose of my projects is learning about network infrastructure, servers etc etc... so I want to be as barebones as possible. Also, I can see the Flask version of it is the same as mine... is the Flask Web Server more secure than HtppServer? – Ony10 Jun 12 '23 at 10:11

0 Answers0