im trying to learn slim and setting up cors, csrf, and using it as a api for my react…
- User
im trying to learn slim and setting up cors, csrf, and using it as a api for my react app. I've ran into issues with cors and csrf... hoping you can help. Heres what I learnt about cors and csrf and after this post ill share my index.php for my app? ok?
Cross-Origin Resource Sharing (CORS) is a security feature implemented in web browsers that allows or restricts web pages from making requests to a domain different from the one that served the web page.
It is a mechanism that enables controlled access to resources located outside of a given domain.
CORS is essential for enabling secure communication between different web applications while preventing malicious cross-origin requests.
By specifying certain headers, servers can indicate which origins are permitted to access their resources, thus maintaining a balance between usability and security.
A good flowchart for implementing CORS support: CORS Server Flowchart
You can read the specification here: https://fetch.spec.whatwg.org/#cors-protocol The simple solution
For simple CORS requests, the server only needs to add the following header to its response:
Access-Control-Allow-Origin: , ...
The following code should enable lazy CORS.
request, args) { return $response; });
- Assistant
- User
- Assistant
- User
rewrite/refactor my index.php and how it should be.
- Assistant
- User
- Assistant
- User
not entirely sure what im looking for:
GET http://localhost:8000/api/csrf-token Transferred0 B (0 B size) Referrer Policystrict-origin-when-cross-origin DNS ResolutionSystem
Accept / Accept-Encoding gzip, deflate, br, zstd Accept-Language en-US,en;q=0.5 Connection keep-alive DNT 1 Host localhost:8000 Origin http://localhost:3000 Priority u=4 Referer http://localhost:3000/ Sec-Fetch-Dest empty Sec-Fetch-Mode cors Sec-Fetch-Site same-site Sec-GPC 1 User-Agent Mozilla/5.0
- Assistant
- User
GET http://localhost:8000/api/csrf-token Transferred0 B (0 B size) Referrer Policystrict-origin-when-cross-origin DNS ResolutionSystem
Accept / Accept-Encoding gzip, deflate, br, zstd Accept-Language en-US,en;q=0.5 Connection keep-alive DNT 1 Host localhost:8000 Origin http://localhost:3000 Priority u=4 Referer http://localhost:3000/ Sec-Fetch-Dest empty Sec-Fetch-Mode cors Sec-Fetch-Site same-site Sec-GPC 1 User-Agent Mozilla/5.0
- Assistant
- User
i disabled:
//$app->add('csrf');// TODO Comment out the CSRF middleware for testing
and the form worked.. but i dont understand.. i know that it means the csrf did not.
- Assistant
- User
POST http://localhost:8000/api/send-email Status 200 OK VersionHTTP/1.1 Transferred541 B (57 B size) Referrer Policystrict-origin-when-cross-origin Request PriorityHighest DNS ResolutionSystem
Access-Control-Allow-Headers Content-Type, Authorization Access-Control-Allow-Methods GET, POST, OPTIONS Access-Control-Allow-Origin http://localhost:3000 Cache-Control no-store, no-cache, must-revalidate Connection close Content-Type application/json Date Tue, 11 Mar 2025 02:50:31 GMT Expires Thu, 19 Nov 1981 08:52:00 GMT Host localhost:8000 Pragma no-cache Set-Cookie PHPSESSID=uamj42bn8ria79vecjflrjl1mh; path=/ X-Powered-By PHP/8.2.26
Accept / Accept-Encoding gzip, deflate, br, zstd Accept-Language en-US,en;q=0.5 Connection keep-alive Content-Length 59 Content-Type application/json DNT 1 Host localhost:8000 Origin http://localhost:3000 Priority u=0 Referer http://localhost:3000/ Sec-Fetch-Dest empty Sec-Fetch-Mode cors Sec-Fetch-Site same-site Sec-GPC 1 undefined undefined User-Agent Mozilla/5.0
- Assistant