ยอมให้ iOS แอปใช้งาน http ได้ แบบเจาะจงโดนเมน หรือยอมให้ใช้วิธีการเข้ารหัสแบบเก่า

Wasith T. (Bai-Phai)
Nov 1 · 1 min read

การใช้งาน http จะเป็นส่ง และรับ request แบบไม่เข้ารหัส ซึ่งอาจถูกดักฟัง หรือปลอมแปลงได้ระหว่างทาง และนั่นทำให้ข้อมูลผู้ใช้งานไม่ปลอดภัย ข้อความอาจไม่น่าเชื่อถือแอปเปิลเลยไม่ได้เปิดให้เชื่อมต่อ http มาโดยปริยายแต่เราต้องระบุว่าแอปเราอยากให้ใช้ http ได้ด้วย

Icon made by Smashicons from www.flaticon.com

แต่มีหลายบริษัทการก็มีความจำเป็นต้องใช้ http เพื่อเชื่อมต่อ service บางอย่างเช่น ผู้ให้บริการโทรศัพท์เคลื่อนที่ เข้าแอปในค่ายตัวเองได้ง่ายโดยไม่ต้องยืนยันตัวตน เพราะถือว่ามี sim card อยู่กับตัวแล้ว

หรืออาจจะอยากต่อ server ของเราเองเพื่อทดลองใช้งานเล็ก ๆ น้อย หรือระบบภายในที่ไม่ยอมซื้อ certificates

สำหรับแบบธรรมดานั้น เราจะระบุในไฟล์ Info.plist ว่า

<key>NSAppTransportSecurity</key>
<dict>
<key>
NSAllowsArbitraryLoads</key>
<true/>
</dict>

ซึ่งจริง ๆ แล้วเราสามารถระบุ domain ของ host ที่จะอนุญาตให้แอปยิงแบบ http ได้ เพื่อที่ระบบของ iOS จะไม่ยอมยิงนอกเหนือจาก domain ที่ได้ระบุไว้เช่น เราจะยอมให้ใช้กับ domain someservice.co.th และ subdomains ทั้งหมดก็ระบุไปว่า

<key>NSAppTransportSecurity</key>
<dict>
<key>
NSAllowsArbitraryLoads</key>
<false/>
<key>
NSExceptionDomains</key>
<dict>
<key>
someservice.co.th</key>
<dict>
<key>
NSIncludesSubdomains</key>
<true/>
<key>
NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>

ซึ่งในชุดของ NSAppTransportSecurity มี key อื่น ๆ ให้ใช้อีกคือ

  • NSIncludesSubdomains
  • NSExceptionAllowsInsecureHTTPLoads
  • NSExceptionRequiresForwardSecrecy
  • NSExceptionMinimumTLSVersion
  • NSThirdPartyExceptionAllowsInsecureHTTPLoads
  • NSThirdPartyExceptionRequiresForwardSecrecy
  • NSThirdPartyExceptionMinimumTLSVersion

และสามารถใช้รวม ๆ กันได้เช่น

<key>NSAppTransportSecurity</key>
<dict>
<key>
NSAllowsArbitraryLoads</key>
<false/>
<key>
NSExceptionDomains</key>
<dict>
<key>
someservice.co.th</key>
<dict>
<key>
NSIncludesSubdomains</key>
<true/>
<key>
NSExceptionMinimumTLSVersion</key>
<string>
TLSv1.0</string>
</dict>
<key>
gucode.live</key>
<dict>
<key>
NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>
odds.team</key>
<dict>
<key>
NSIncludesSubdomains</key>
<true/>
<key>
NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>

แต่ทางที่ดีควรจะยกมาตรฐานให้ได้สูงสุดตามที่แอปเปิลแนะนำมากกว่า

สวัสดีครับ

กูโค้ด

ใครไม่โค้ด กูโค้ด

Wasith T. (Bai-Phai)

Written by

ตบมือเป็นกำลังใจให้ผมด้วยนะครับ 😘

กูโค้ด

ใครไม่โค้ด กูโค้ด

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade