Prevent invalid user agent header values from being set

Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2022-12-15 17:49:48 -05:00
parent e9087583a0
commit 0572287693
3 changed files with 22 additions and 10 deletions

View file

@ -54,6 +54,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import okhttp3.Headers
import rikka.sui.Sui
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
@ -274,6 +275,14 @@ class SettingsAdvancedController : SettingsController() {
titleRes = R.string.user_agent_string
onChange {
it as String
try {
// OkHttp checks for valid values internally
Headers.Builder().add("User-Agent", it)
} catch (_: IllegalArgumentException) {
context.toast(R.string.error_user_agent_string_invalid)
return@onChange false
}
activity?.toast(R.string.requires_app_restart)
true
}

View file

@ -40,20 +40,22 @@ class EditTextResetPreference @JvmOverloads constructor(
textView?.append(sharedPreferences?.getString(key, defValue))
this.setView(view)
this.setNeutralButton(R.string.reset) { _, _ ->
sharedPreferences?.edit { remove(key) }
callChangeListener(defValue)
notifyChanged()
if (callChangeListener(defValue)) {
sharedPreferences?.edit { remove(key) }
notifyChanged()
}
}
this.setPositiveButton(android.R.string.ok) { _, _ ->
sharedPreferences?.edit {
if (textView.text.isNullOrBlank()) {
remove(key)
} else {
putString(key, textView.text.toString())
if (callChangeListener(textView.text.toString())) {
sharedPreferences?.edit {
if (textView.text.isNullOrBlank()) {
remove(key)
} else {
putString(key, textView.text.toString())
}
}
notifyChanged()
}
callChangeListener(textView.text.toString())
notifyChanged()
}
a.recycle()
}

View file

@ -808,6 +808,7 @@
<string name="network">Network</string>
<string name="doh">DNS over HTTPS</string>
<string name="user_agent_string">Default user agent string</string>
<string name="error_user_agent_string_invalid">Invalid user agent string</string>
<string name="disabled">Disabled</string>
<string name="cloudflare" translatable="false">Cloudflare</string>
<string name="google" translatable="false">Google</string>