2

I am trying to write a bash script to update set Max TLS version to 1.3 in Chrome. This is what my script currently looks like:

#!/bin/bash -x
consoleuser=`ls -la /dev/console | cut -d " " -f4`
string='browser.*max@2'
echo "Current user: ${consoleuser}"
echo "Closing Google Chrome.."
sleep 2
pkill Chrome
cd /Users/$consoleuser/Library/Application\ Support/Google/Chrome
echo "Current directory: " && pwd
if [ ! -f "/Users/"$consoleuser"/Library/Application Support/Google/Chrome/Local State" ]; then
     echo "Local State file not found. Relaunch Chrome to create the file"
else
    while true; do
        if grep -q "$string" "/Users/"$consoleuser"/Library/Application Support/Google/Chrome/Local State"; then
            echo "Max TLS set to 1.3"
            break
        else
            sed -i '.bak' -e 's|\(\"browser\"\):{\(\".*origin\":\"\"\)}|\1:{\"enabled_labs_experiments\":[\"ssl-version-max@2\"],\2}|' /Users/$consoleuser/Library/Application\ Support/Google/Chrome/Local\ State
            if ! grep -q "$string" "/Users/"$consoleuser"/Library/Application Support/Google/Chrome/Local State"; then
                echo "TLS configuration not found. Retrying.."
            fi
        fi
    done
fi
sleep 2
exit

The goal is to append

"enabled_labs_experiments":["ssl-version-max@2"]

to this

{"browser":{"last_redirect_origin":""}

to make it looks like this

{"browser":{"enabled_labs_experiments":["ssl-version-max@2"],"last_redirect_origin":""}

The reason to put sed in a loop is that it needs to run sed at least twice on some machines to make actual changes to the file. However, for some reason, this script runs on some machines and fails to make changes on others. Not really sure what I am doing wrong here.

Any insights/help/directions/articles to make this script work or to achieve a similar result will be highly appreciated!

For reference: @1=TLS1.2 and @2=TLS1.3

Thank you!

EDIT

After reading comments below, I learned that python can be used to edit JSON files as jq is not native to Macs. However, I have no idea for where to start. I would really appreciate it if anyone could help me with this or point me in right direction.

LuciDroid
  • 55
  • 1
  • 7
  • 1
    That file is one long line (100K bytes on my machine). Maybe `sed` can't handle such a long line? It might be better to use a tool like `jq` to manipulate JSON, instead of `sed`. – Barmar Jun 07 '17 at 17:27
  • BTW, you don't need to escape double quote inside the `s|||` command. – Barmar Jun 07 '17 at 17:29
  • The problem with using jq is that it's not natively available on Macs and installing it is not an option as the script will be deployed on multiple systems. – LuciDroid Jun 07 '17 at 17:39
  • I'm not sure what you mean by "not natively available on Macs" but there's a OS X 64bit download link on [their website](https://stedolan.github.io/jq/) – Aaron May 24 '19 at 09:17

0 Answers0