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

Wasith T. (Bai-Phai)
กูโค้ด
Published in
1 min readNov 1, 2019

การใช้งาน 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)
กูโค้ด

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