Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CORS_再入門.pdf
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
dach
February 27, 2020
1.4k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CORS_再入門.pdf
dach
February 27, 2020
More Decks by dach
See All by dach
dbt_ベストプラクティス_完全に理解した.pdf
dach
2
1.2k
プロジェクトマネージャーと炎の回避術
dach
0
980
SLO_By_Google_Cloud_Monitoring
dach
0
220
状態遷移テスト完全に理解しよう.pdf
dach
0
880
JWT完全に理解しよう-認証編-.pptx.pdf
dach
0
820
JWT完全に理解しよう-公開鍵編-.pptx.pdf
dach
0
730
チームの垣根を越境する_チーム間交換留学
dach
0
110
設計書のないサービスとの付き合い方.pptx.pdf
dach
0
210
designからWebページを作るやりかた完全に理解した.pdf
dach
1
380
Featured
See All Featured
For a Future-Friendly Web
brad_frost
183
10k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.4k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
Utilizing Notion as your number one productivity tool
mfonobong
4
320
Tell your own story through comics
letsgokoyo
1
950
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Everyday Curiosity
cassininazir
0
230
Side Projects
sachag
455
43k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
480
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
Transcript
CORS 再入門 @dach
Who is me? Job • 元SRE →まねーじめんと(実装したい) 所属 • EasyEasy運営 •
TOPGATE • チキン南蛮を支える会(仮) 最近の主な出費 • 食費、嫁の時計
None
Why speak CORS?
Former me 完全に理解したわ
The other day... @dach CORS問題を解消するために、 API 側でも改修入れました。 これで localhost で
CORS が出てたのも解消されました。
あれ? CORSって ClientServer側の話 じゃないの?
What’s CORS?
CORS (オリジン間リソース共有) 追加の HTTP ヘッダーを使用して、あるオリ ジンで動作しているウェブアプリケーションに、 異なるオリジンにある選択されたリソースへの アクセス権を与えるようブラウザーに指示する ための仕組みです。 (「MDN
web docs」より引用) https://developer.mozilla.org/ja/docs/Web/HTTP/CORS
Question 次の2つのケースで、CORS制約で怒られるケースはどれで しょう
In this case: 1 front-end Container API Contaner domain: default-dot-app
domain: api-dot-app
In this case: 2 front-end Container API Contaner port:443 network:A
port:8080 network:A localhost
Answer どちらも
なぜCase2でも怒ら れるのだろうか?
Answer ウェブアプリケーションは、自分とは異なるオリジン (ドメイン、プロトコル、ポート番 号) にあるリソースをリクエストするとき、 オリジン間 HTTP リクエストを実行します。 (「MDN web
docs」より引用)
PlayBack @dach CORS問題を解消するために、 API 側でも改修入れました。 これで localhost で CORS が出てたのも解消されました。
手を加えるところって フロントじゃないの?
CORS in detail const xhr = new XMLHttpRequest(); const url
= 'https://bar.other/resources/public-data/'; xhr.open('GET', url); xhr.onreadystatechange = someHandler; xhr.send();
CORS in detail const xhr = new XMLHttpRequest(); const url
= 'https://bar.other/resources/public-data/'; xhr.open('GET', url); xhr.onreadystatechange = someHandler; xhr.send(); 誰が呼び出したか
CORS in detail if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods:
GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; }
CORS in detail if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods:
GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; } 誰からのアクセス を許可しているか
CORS in detail if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods:
GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; } 誰からのアクセス を許可しているか 結果
CORS in detail const xhr = new XMLHttpRequest(); const url
= 'https://bar.other/resources/public-data/'; xhr.open('GET', url); xhr.onreadystatechange = someHandler; xhr.send(); 誰が呼び出したか 誰からのアクセス を許可しているか if($_SERVER['HTTP_ORIGIN'] == "http://arunranga.com") { header('Access-Control-Allow-Origin: http://arunranga.com'); header('Access-Control-Allow-Methods: GET, OPTIONS'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 1728000'); header("Content-Length: 0"); header("Content-Type: text/plain"); } else { header("HTTP/1.1 403 Access Forbidden"); header("Content-Type: text/plain"); echo "You cannot repeat this request"; }
Answer 「Access-Comntrol-Allow-Origin」で対象のリソースにアクセスできるオリジンは 誰かを指定する必要がある = API側の修正が必要だった
Conclusion
Conclusion • origin(ドメイン、プロトコル、ポート番号) • 異なるoriginにあるリソースに対してアクセスするときには 「レスポンス側」に設定が必要
Thanks!