Created
October 19, 2020 07:41
-
-
Save rnkyr/54592eecae23df76eda8b565ce29379c to your computer and use it in GitHub Desktop.
Updates Info.plist with ATS exceptions based on Config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Checks Config.plist for http urls and adds them into App Transport security of your Info.plist | |
| INFO_PLIST = "#{ENV["BUILT_PRODUCTS_DIR"]}/#{ENV["INFOPLIST_PATH"]}" | |
| CONFIG_PLIST = INFO_PLIST.sub(/.*\KInfo\.plist/, 'Config.plist') | |
| PLISTBUDDY = "/usr/libexec/PlistBuddy" | |
| lines = `#{PLISTBUDDY} #{CONFIG_PLIST} -c print | grep = | tr -d ' '`.split("\n") | |
| urls = [] | |
| lines.each do |line| | |
| value = line.match(/.*\=(.*)/).captures[0] | |
| if value.include?('http://') | |
| urls.append value | |
| end | |
| end | |
| return if urls.empty? | |
| require 'uri' | |
| def get_host_without_www(url) | |
| uri = URI.parse(url) | |
| uri = URI.parse("http://#{url}") if uri.scheme.nil? | |
| host = uri.host.downcase | |
| host.start_with?('www.') ? host[4..-1] : host | |
| end | |
| `#{PLISTBUDDY} -c "Add :NSAppTransportSecurity dict" "#{INFO_PLIST}"` | |
| `#{PLISTBUDDY} -c "Add :NSAppTransportSecurity:NSExceptionDomains dict" "#{INFO_PLIST}"` | |
| urls.each do |url| | |
| domain = get_host_without_www(url) | |
| `#{PLISTBUDDY} -c "Add :NSAppTransportSecurity:NSExceptionDomains:#{domain} dict" "#{INFO_PLIST}"` | |
| `#{PLISTBUDDY} -c "Add :NSAppTransportSecurity:NSExceptionDomains:#{domain}:NSExceptionAllowsInsecureHTTPLoads bool true" "#{INFO_PLIST}"` | |
| `#{PLISTBUDDY} -c "Add :NSAppTransportSecurity:NSExceptionDomains:#{domain}:NSIncludesSubdomains bool true" "#{INFO_PLIST}"` | |
| end | |
| puts "updated #{INFO_PLIST}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment