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 People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
210
The browser strikes back
jonoalderson
0
1.2k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
My Coaching Mixtape
mlcsv
0
150
Evolving SEO for Evolving Search Engines
ryanjones
0
220
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Site-Speed That Sticks
csswizardry
13
1.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Technical Leadership for Architectural Decision Making
baasie
3
410
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
190
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
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!