In simple language.
CSP (Content Security Policy)
In CSP, a site will only call or load resources from the sites specified in its CSP. Even if the other site is ready to respond, the site won't call them if they are not listed in the CSP. CSP is to prevent calls to external resources This is designed to prevent a website from loading potentially malicious content from untrusted sources.
Example: If you open facebook.com and CSP is set to facebook.com, it will not call twitter.com or any other site.
Content-Security-Policy: default-src facebook.com
It can also be set in the HTML meta tag.
<meta
http-equiv="Content-Security-Policy"
content="default-src https://facebook.com"/>
If this policy is set and the site makes call to other site then browser will block it with blocked:csp error.
CORS (Cross-Origin Resource Sharing)
In CORS, a site can make calls to other sites, but the other site will only respond if the requesting site is allowed by its CORS policy. CORS is to prevent calls from external sources. This prevents a site from sharing data with untrusted origins.
Example: If you open facebook.com with no CSP set, facebook.com can make calls to twitter.com. However, twitter.com will not respond unless facebook.com is allowed by its CORS policy.
Access-Control-Allow-Origin: https://twitter.com
If this policy is set and the site make call to other site then browser will block it with CORS error.
Both CSP and CORS are enforced at the browser level.
You can still make calls using tools like cURL, but the browser will block such requests based on these policies.
Technical details:
CSP Info Source: The browser gets CSP information from the server of the site being loaded or from the HTML meta tag. If the other site is not in the CSP list, the browser blocks the call.
CORS Preflight Request: The browser makes a preflight request to check if the other site has the appropriate CORS settings. If the requesting site is not in the allowed list, the browser blocks the call.