What is CORS?

  Рет қаралды 63,394

A shot of code

A shot of code

4 жыл бұрын

CORS - Cross Origin Resource Sharing allows us to relax the security applied to an API. By default the Single Origin Policy applies and a website can only make calls to its own origin. In the case though where you have a sub domain or even some third party sites that need access to you API CORS allows us to provide this to just those specific sites without having to open up the API to all origins. In this tutorial video we look at some example APIs on the web today and how they handle CORS.
swapi.co/
api.chucknorris.io/
CORS will add the the response header [access-control-allow-origins] and specify which origins are to be permitted access. This can include a wildcard, which is how the above 2 sites handle this.
Then create our own sample API and show how to add CORS headers to allow access from other Origins. The example is created in dotnet core within Visual Studio.
It should be noted that CORS and SOP is something applied by web browsers and does not take effect in other applications. So a node console app would be able to access an API even though it is not from the same origin.
P.S Just joined buy me a coffee :-)
www.buymeacoffee.com/ashotofcode

Пікірлер: 96
@mib141345
@mib141345 3 жыл бұрын
You explained in 13 mins what I spent hours reading and not comprehending. Thanks!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it helped Doran!
@mujthabahassan7614
@mujthabahassan7614 4 жыл бұрын
Thanks a lot, I was scratching my head a lot over this but you explained it briefly yet comprehensively
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Hi Mujthaba, glad it was helpful 😀
@tedisrozenfelds7630
@tedisrozenfelds7630 2 жыл бұрын
I liked that you failed couple of times and then debugged your own code. That actually showed some common mistakes that can be made and should be avoided!
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Cool thanks Tedis 😀
@joespinelli3604
@joespinelli3604 4 жыл бұрын
Awesome stuff! Thanks for being so clear and the example was very easy to follow:)
@suryakiran2207
@suryakiran2207 4 жыл бұрын
More simplified, thanks a lot for great explanation.
@wesleygomes4154
@wesleygomes4154 Жыл бұрын
man, thanks a lot for sharing this knowledge. You made this topic very clear to me now!
@tajsec498
@tajsec498 2 жыл бұрын
whole day I was struggling with this :)) your explanation was clearrr, thanksss
@Ashotofcode
@Ashotofcode 2 жыл бұрын
Glad it helped! :-)
@maspoetry1
@maspoetry1 3 жыл бұрын
i merge to the crowd, great video. thanks. I like when you solve the problems in real time, without editing.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
No problem!
@jessandgary5940
@jessandgary5940 Жыл бұрын
Finally, i get it! Thanks.
@Ashotofcode
@Ashotofcode Жыл бұрын
Glad it helped! Cheers Mark 🙂
@giorgidzidziguri610
@giorgidzidziguri610 9 ай бұрын
best tutorial out there
@Ashotofcode
@Ashotofcode 9 ай бұрын
Thanks!
@nyplace1
@nyplace1 Жыл бұрын
phenomenal explanation!
@BB855036
@BB855036 4 жыл бұрын
Great explanation. Thanks!
@Ashotofcode
@Ashotofcode 4 жыл бұрын
You're welcome!
@shivarammuthukumaraswamy7164
@shivarammuthukumaraswamy7164 3 жыл бұрын
wonderfully explained.TYSM
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it was helpful Shivaram! Cheers Mark :-)
@balapraneeth9708
@balapraneeth9708 3 жыл бұрын
Great video. Helped a lot. Thanks mate :)
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it helped :-)
@mineralisk
@mineralisk 4 жыл бұрын
Thanks for making the video
@ninjarogue
@ninjarogue 3 жыл бұрын
Thank you!!! I really appreciate the video!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it was helpful Aric :-)
@dhruvpatel6937
@dhruvpatel6937 2 ай бұрын
Very clear explanation, thank you kind sir!
@Ashotofcode
@Ashotofcode 2 ай бұрын
Welcome :-) Cheers Mark
@jagadeeshg3756
@jagadeeshg3756 3 жыл бұрын
WoW, Thank you so much!
@ameyapatil1139
@ameyapatil1139 4 жыл бұрын
Excellent ! Thanks !
@sabithapoladi5620
@sabithapoladi5620 3 жыл бұрын
very good explanation
@unknownqweasd
@unknownqweasd 3 жыл бұрын
it was very helpfull, thank you!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad it was helpful! Cheers Mark
@ttoktassynov
@ttoktassynov 3 жыл бұрын
well explained! thanks
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad you liked it Timur :-)
@ashwinisidhu
@ashwinisidhu 4 жыл бұрын
shot & easy. Thanks
@ArijeetSarkar7
@ArijeetSarkar7 3 жыл бұрын
It helped me understand what is CORS and I solved a real world problem. The problem was the origin doesn't support any headers and I was sending one. After I removed, it started working.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Excellent, glad it helped Arijeet :-)
@micahnewsum3667
@micahnewsum3667 3 жыл бұрын
Props to this guy for live coding.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks Micah :-)
@arfan8544
@arfan8544 4 жыл бұрын
Thank You very much. 🤘
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Thanks SM, glad it was useful 😀
@louisecrowe4968
@louisecrowe4968 3 жыл бұрын
Thanks great video :)
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad you liked it Louise. Cheers Mark :-)
@daminduliyanage
@daminduliyanage 3 жыл бұрын
Thank You 👍🏻👍🏻
@jig4576
@jig4576 9 ай бұрын
Awesome video
@Ashotofcode
@Ashotofcode 9 ай бұрын
Thanks! Cheers Mark 🙂
@thisurathenuka8362
@thisurathenuka8362 3 жыл бұрын
Nice explanation ❤
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Glad you liked it Thisura :-)
@erdemarslan3371
@erdemarslan3371 3 жыл бұрын
thx very clear!!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks Erdem :-) glad it was useful.
@tonyj4435
@tonyj4435 4 жыл бұрын
Thanks bro
@Ashotofcode
@Ashotofcode 4 жыл бұрын
Welcome 😎
@WolfgangPedain
@WolfgangPedain 11 ай бұрын
well done
@Ashotofcode
@Ashotofcode 11 ай бұрын
Thanks :-)
@iQCudi
@iQCudi 3 жыл бұрын
amazing
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thank you! Cheers!
@ganeshk8682
@ganeshk8682 3 жыл бұрын
Thanks..
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Welcome
@jamesscott-nicholson3808
@jamesscott-nicholson3808 3 жыл бұрын
Thanks for the video, that's cleared it up for me nicely :). If CORS is something only handled by the browser, I suppose that makes it a fairly weak piece of security. Could a browser / extension be made that simply ignores CORS or injects in the necessary header?
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks James, I'm not really up on the capabitilites of extensions, but they have full control so would think they would be able cause problems here yes. Cheers Mark
@abhijithk1397
@abhijithk1397 3 жыл бұрын
yes, you can find extensions in chrome store that disable CORS
@michi19935
@michi19935 5 ай бұрын
One question - maybe anyone knows: Why can i not log out the json response one the first .then method?
@YosepRA
@YosepRA 3 жыл бұрын
So it's all about the back-end setting up CORS headers, and the browser will try to find these headers to determine whether there's CORS violation or not.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Yep that's a good summary I'd say😀
@saqlainmushrif6453
@saqlainmushrif6453 Жыл бұрын
Can cors be exploited if some token is in URL?? (GET METHOD) Arbitrary origin is reflected in response with ACAO & ACAC but the token is in URL
@CryptoJitsu
@CryptoJitsu 3 жыл бұрын
Great vid, thank you! QUESTION: When the API does not send back the response header [access-control-allow-origins]... I'm assuming it's still sending back the data in the response body... because the decision to show or not is being done by the receiving browser. This seems insecure and dangerous and something a hacker could get around, no?
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Thanks! Good question - Yes I think you are correct in that the data will be returned - the browsers are pretty solid though so I would think safe - this takes place internally in the browser so not something you can attack with Javascript really. Cheers Mark
@CryptoJitsu
@CryptoJitsu 3 жыл бұрын
@@Ashotofcode thanks!
@anishamalynur7748
@anishamalynur7748 3 жыл бұрын
hey quick question one of the options to fix the error was "If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled." could you explain this?
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Hi Anisha, good question, this would be if you simply wanted to check that the service existed, in that it returns a success code, but without any data. I'm not sure when this would be useful, but there are cases I guess. Cheers Mark
@dartme18
@dartme18 3 жыл бұрын
kzfaq.info/get/bejne/pqqFks2fqZi8oHk.html ; different Anisha I assume :-P
@MrParanos
@MrParanos 3 жыл бұрын
Very clear and useful, yet there is still something my mind can't put hand on : in what are CORS useful ? Regarding how easy it is to go arount it... :/
@Ashotofcode
@Ashotofcode 3 жыл бұрын
CORS is actually more about relaxing the existing security, so by default only requests from your own site can be made, which is the same-origin policy. With CORS we can allow other sites to access also. So one good scenario is when our API is on a different domain to our website - in this case CORS will allows us to let the website access our API - as otherwise will be blocked by the same origin policy. Another case is simply a public API and we want to allows anyone to call it, say a weather api, by default it is restricted to just the domain it runs under, so we add CORS to relax this security and allow anyone to call it. So CORS itself is not something to get around - that is the same origin policy - which is pretty locked down in browsers. Cheers Mark
@Derekbylck
@Derekbylck 3 жыл бұрын
=> return
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Ah yes that one gets me a lot! thanks
@aravind4444
@aravind4444 3 жыл бұрын
please explain with diagram
@dartme18
@dartme18 3 жыл бұрын
RIP, SWAPI
@Ashotofcode
@Ashotofcode 3 жыл бұрын
D'oh yep it has died!
@dartme18
@dartme18 3 жыл бұрын
@@Ashotofcode Someone posted a duplicate pretty quickly after SWAPI died (that was six months ago I think?). Our company used SWAPI for interview exercises, so we were happy to see the replacement!
@Ashotofcode
@Ashotofcode 3 жыл бұрын
@@dartme18 Ah yes, swapi.dev, cool thanks!
@trumbaron
@trumbaron Жыл бұрын
Confusing for me...
@JulienReszka
@JulienReszka 3 жыл бұрын
audio is very low volume, I wish it wasn't that low
@ramiz3570
@ramiz3570 4 жыл бұрын
+
@ValentinTruta
@ValentinTruta 3 жыл бұрын
Arrow functions return by default if code is on the same line.
@Ashotofcode
@Ashotofcode 3 жыл бұрын
Nice thanks Valetin!
@addtyu6176
@addtyu6176 3 жыл бұрын
I closed the video immediately after seeing Microsoft Edge being used....
@Ashotofcode
@Ashotofcode 3 жыл бұрын
lol fair play :-)
@smashed5826
@smashed5826 3 жыл бұрын
The explanation was not deep enough, in this video you just explianed CORS is browser security policy stuff and seeing you tried it out for direct access on browser and via ajax call. It could be deeper to explain why browser needs this; what kind of attacks could be implemented if no this security policy on browser; What headers needs to be added to allow browser calls a cors resource, different browsers or same browser with different versions treat different headers to allow CORS; server side API header settings to control the access the resource in different scenarios etc.
@ronaldlogan3525
@ronaldlogan3525 3 жыл бұрын
very poorly explained, uses tools people may not be familiar with, the actual subject is almost ignored
Cross Origin Resource Sharing (Explained by Example)
23:15
Hussein Nasser
Рет қаралды 154 М.
Cross-Origin Resource Sharing (CORS) | Complete Guide
52:17
Rana Khalil
Рет қаралды 66 М.
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 6 МЛН
THEY made a RAINBOW M&M 🤩😳 LeoNata family #shorts
00:49
LeoNata Family
Рет қаралды 38 МЛН
마시멜로우로 체감되는 요즘 물가
00:20
진영민yeongmin
Рет қаралды 29 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Что такое CORS и зачем он нужен? По простому
8:15
Мир IT с Антоном Павленко
Рет қаралды 59 М.
I Hate CORS.
7:52
Theo - t3․gg
Рет қаралды 73 М.
What Is JWT and Why Should You Use JWT
14:53
Web Dev Simplified
Рет қаралды 1,1 МЛН
What is CORS? | Cross-Origin Resource Sharing | CORS Explained!
8:26
CSRF and CORS Explained
8:11
Tejas Kumar
Рет қаралды 4,2 М.
The Same Origin Policy - Hacker History
12:19
LiveOverflow
Рет қаралды 106 М.
Web Server Concepts and Examples
19:40
WebConcepts
Рет қаралды 234 М.
This Fixes CORS
13:02
Colby Fayock
Рет қаралды 1,3 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 87 М.
Очень ждал свою маму 💔
0:56
Awesome Cuts
Рет қаралды 3,3 МЛН
Este tobogán es mío 😡
0:46
Yoila y Teey
Рет қаралды 17 МЛН
Heartwarming moment as priest rescues ceremony with kindness #shorts
0:33
Fabiosa Best Lifehacks
Рет қаралды 11 МЛН
Sweet watermelon 🍉😋
0:15
LizAlex Fam
Рет қаралды 11 МЛН
БЕСТРАШНЫЙ ШКОЛЬНИК НА ВЕЛОСИПЕДЕ #shorts
0:11
Jatuh dua berturut turut
0:15
CIREBON VLOG JEH
Рет қаралды 53 МЛН