开发者

Jquery $.post and PHP - Prevent the ability to use script outside of main website

开发者 https://www.devze.com 2022-12-24 09:12 出处:网络
I have a PHP script setup using Jquery $.post which would return a response or do an action within the targeted .php file within $.post.

I have a PHP script setup using Jquery $.post which would return a response or do an action within the targeted .php file within $.post.

Eg. My page has a form where you type in your Name. Once you hit the submit form button, $.post is called and sends the entered Name field value into "mywebsite.xyz/folder/ajaxscript.php"

If a user was to visit "mywebsite.xyz/folder/ajaxscript.php" directly and somehow POST the data to the script, the script would return a response / do an action, based on the submitted POST data.

The problem is, I don't want others to be able to periodically "call" an action or request a response from my website without using the website directly. Theoretically, right now you could determine what Name values my website allows without even visiting it, or you could call an action without going through the website, by simply visiting "mywebsite.xyz/folder/ajaxscript.php"

So, what measures can I take to prevent this from happening? So far my idea is to ensure that it is a $_POST and not a $_GET - so they cannot manually enter it into the browser, but they could still post data to the script...

Another measure is to apply a session key that expires, and is only valid for X amount of visits until they revisit the website. ~ Or, just have a daily "code" that changes and they'd need to grab this code from the website each day to keep their direct access to the script working (eg. I pass the daily "code" into each post request. I then check that code matches in the ajax php script.)

However, even with these meaures, they will STILL have access to the scripts so long as they know how to POST the data, and also get the new code each day. Also, having a daily code requirement will cause issues when visiting the site at midnight (12:00am) as the code will change and the script will break for someone who is on the website trying to call the script, with the invalid code being passed still.

I have attempted using .htaccess however using: order allow,deny deny from all

Prevents legitimate access,开发者_运维知识库 and I'd have to add an exception so the website's IP is allowed to access it.. which is a hassle to update I think. Although, if it's the only legitimate solution I guess I'll have to.

If I need to be more clear please let me know.


The problem you describe is similar to Cross-Site Request Forgery (CSRF or XSRF). To protect you against this you could put a cookie into the browser and have the cookie value sent in the post form too (by hidden field or just add it to $.post). On server side check both those fields, if they match the request probably came from your site.
However the problem you describe will be quite hard to protect against. Since you could easily make a script (or use Crul) to forge all kinds of requests and send to your server. I don't know how to "only allow a browser and nothing else".


Use the Session variable as you say plus...

As MyGGAN said use a value set in a cookie (CVAL1) before rendering the submit forms. If this cookie is available (JS Code Check will verify) then submit.

On the server side: If this cookie value exists and the session variable exist then the HTTP Request came from your website.

Note: If the script (form) is to presented under another domain DO NOT allow the cookie value (CVAL1) to be set.

Do not allow HTTP Requests on the Server Side Scripts if extra Http Headers Are not available (like x-requested-with: jquery). JQuery sends a request with an X-* header to the server.

Read more on Croos-Site Request Forgery as MyGGAN suggests.


I am not really sure REMOTE_ADDR would work. Isnt that supposed to be the end users IP addr?

Firstly, you could make use of $_SERVER['HTTP_REFERER'], though not always trust-able.

The only bet that a valid post came from your page would be use a captcha.


try to use HTTP_SEC

// SECURITER
if ($_SERVER[HTTP_SEC_FETCH_SITE] != "same-origin")
    die();
if ($_SERVER[HTTP_SEC_FETCH_MODE] != "cors")
    die();
if ($_SERVER[HTTP_SEC_FETCH_DEST] != "empty")
    die();
0

精彩评论

暂无评论...
验证码 换一张
取 消