amuck-landowner

Python - safest way to handle user input

RTGHM

New Member
I'm looking for a safe way to handle user-input.

Ie, the system asks for a domain name to generate a file, what would be the best way to ensure no sneaky things happen. IE it isn't malformed code being run.
 

raindog308

vpsBoard Premium Member
Moderator
The right way to do it is to define what you allow, rather than to define what you don't.  Anything that doesn't match what's allowed is rejected.

For example,

  • only letters/numbers/internal hyphen,
  • a single period with a minimum number of letters before/after
  • Length of chars before and after the period should be sane (I don't know what the actual length limit for a domain is - I'm sure it's in an RFC). 
  • etc.
Anything that doesn't match this criteria should be rejected.  I'm not saying this is the exact criteria to use in this case, but the theory is that you don't want to "strip out the bad" - rather define what is good and if it doesn't match, sayonara.

I couldn't find anywhere where python defines a domain name type object you could use - but perhaps there is a module out there.
 

RTGHM

New Member
The right way to do it is to define what you allow, rather than to define what you don't.  Anything that doesn't match what's allowed is rejected.

For example,

  • only letters/numbers/internal hyphen,
  • a single period with a minimum number of letters before/after
  • Length of chars before and after the period should be sane (I don't know what the actual length limit for a domain is - I'm sure it's in an RFC). 
  • etc.
Anything that doesn't match this criteria should be rejected.  I'm not saying this is the exact criteria to use in this case, but the theory is that you don't want to "strip out the bad" - rather define what is good and if it doesn't match, sayonara.

I couldn't find anywhere where python defines a domain name type object you could use - but perhaps there is a module out there.
Well, it accepts input from a web form on the python script, then it creates a directory for the user-supplied input.

So therefore I want to ensure it stays safe. I was going to do a regex just to accept A-Z 0-9 and a period.

Would that be safe enough?
 

perennate

New Member
Verified Provider
A period? That doesn't sound good since someone can try to make a directory "." or "..", although it probably won't be an issue. Anyway you should be fine just with os.path.basename to make sure it's a single name, and os.path.exists to make sure dirctory doesn't exist yet.
 
Top
amuck-landowner