Forms and Site Security Help
Hi everyone! I have most of my site set up with just HTML and CSS, I want to add a form to receive some information such as Name, Phone Number and Email Address and have that emailed to me. I have a php setup for that, but I'm worried that having a form may create a vulnerability in my site and allow it to be hacked (it's happened before). How are you all preventing injection attacks and other exploits?
46 Replies
Prepared statements
Would this work or are there vulnerabilities still?
oh, sorry, I assumed you meant SQL injection
I'm not terribly up to date on the security concerns with sending mail through the
mail()
function, but that looks like a good start at least. Hopefully someone else can weigh inthank you!
it looks ok, but you dont implement any type of rate limiting from php
you need to implement it from apache/nginx if you prefer
This might be a good read. OWASP has some great stuff on security, as that's kinda their thing :p
Also, see #How To Ask Good Questions to see how to properly format code blocks in Discord
i always forget about owasp
but seriously, i would consider not even sending an email
i would just ram all the data into the database and then send a daily email with all the contacts or something
also, you dont have any server-side validation or verificarion or any records at all
in fact, this even sounds like you didnt even request consent to use user's data, as required by the gdpr
some states are also slowly starting to have some data protection laws too, so, you should look into that
if i were you, i would even avoid sending an email from your own server, as you will have to deal with an annoying amount of work, as would try to use sendgrid or other service that has a nice free tier
thank you, I've formatted the post
I don't need gdpr where I am which is why it's not implemented. why do you say sending from my own server would be an annoying amount of work?
oh didn't consider rate limiting, thank you, would throttling like this work?
no, i can just send from a "browser" that doesnt support cookies (or disable cookies in curl)
you need to make it clear who the target audience is then, in the form
and it is annoying because of all the configurations you have to do to try to avoid spoofing
also, if some asshole decides to spam the living crap out of your server, and you send an email per request, your email box may get full and/or you get blacklisted
then how about using the database to store the session details such as:
This plus ReCaptcha should do it? or can they still be bypassed?
with recaptcha, it's possible to slow it down
but you added a possible sql injection
use prepared statements to insert stuff into the database
also, you dont need to read and send the value to the database to increment
by the way, remove the closing tag
which part do you see as a possible injection risk? none of this is coming from the form fields. not sure I understand what you meant by not needing to read and send to the database to increment, and yes, removing the closing tag for server efficiency, just keeping it there when doing snippet examples
the ip address comes from external programs
and if im not mistaken, it can be controlled in some scenarios
the ip address can be manipulated to inject sql code???
omg
i think it is possible, yes
how is anyone implementing forms on their sites!?!
painfully
seems so
with prepared statements
prepared statements have another advantage: mysql can cache the compiled query and subsequent queries are a lot faster
it's very unlikely... but the reason you would still use prepared statements are twofold:
1) Forming the habbit. It's too important to ignore and good to just always use the right way
2) Just in case there's a PHP vulnerability where
$_SERVER
can be manipulated. It'd be much nicer to know that "huh, maybe the rate limiter won't work so well" rather than "in theory my entire DB is now vulnerable"if you provide the values directly, those are new queries and wont be cached
basically, you're writing code with in the back of your mind that other parts of the stack might have vulnerabilities you didn't count on
there's a lot of stuff that is user-controlled in the
$_SERVER
superglobal
stuff you wouldnt expectpretty sure REMOTE_ADDR isn't, but it's still good practice
it's passed by apache to php
or nginx
it's a good idea to be paranoid
so, like this?
i just searched a bit and the value can be controlled if you are behind a proxy
not a lot of control, but still
now, you have a concurrency problem
fml
if i send 2 requests super quickly, i can submit twice and count as 1
you are reading the number of attempts and then storing it into the database
instead, just update in the database
let mysql do the increment for you
you can read the value again after, if you want, just so you have the updated number of attempts and then you check again
this??
you are setting the count to 1
all the time
wait, no, im wrong
only once the time limit is exceeded no?
yes
phew
also, remove the closing tag
so you think this, plus the prepared statements in my higher up example for the form itself, plus the recaptcha should be sufficient?
should slow down the spam, yes
and the risk of exploiting the server (putting files, executing, manipulating the db)?
I don't mind getting some spam, it's just I don't want my whole site deleted and replaced by a bunch of random pages (like had happened before)
well, the way that file uploads work, you WILL have files put in your server
so, you have to configure php to only accept files of up to a certain size
or as small as possible, if you dont need file uploads
huh? it's just a contact form, three fields, Name, Email and Phone. They can use the form to upload files???
yes, with a post request
thats a part of how web servers work
the file is deleted once the request finishes, but still
if you have configured to allow uploads of 100mb, and your server has 10gb of space, you just need 100 bots with intentionally slow speeds to absolutely murder your server
php is configured to 2mb, which would require 5000 bots sending 2mb at snails pace
a much more unfeasable ddos vector with that configuration vs a custom 100mb
but if I'm the only one uploading files to the server through my backend, then I could just turn uploads off like this?
at that point, the file is already in the server
😭
if your server uses apache, you might want to use a .htaccess file to set some php flags
this way, you can disable user uploads
thank you, yes it does