Catch issues with bad MMSC URIs, add validation

Fixes #2202
// FREEBIE
This commit is contained in:
Jake McGinty 2014-12-16 15:55:41 -08:00
parent 44a4570870
commit e650349bb7
3 changed files with 80 additions and 22 deletions

View file

@ -502,6 +502,9 @@
<string name="MmsPreferencesFragment__manual_mms_settings_are_required">Manual MMS settings are required for your phone.</string>
<string name="MmsPreferencesFragment__enabled">Enabled</string>
<string name="MmsPreferencesFragment__disabled">Disabled</string>
<string name="MmsPreferencesFragment__not_set">Not set</string>
<string name="MmsPreferencesFragment__invalid_uri">The text entered was not a valid URI</string>
<string name="MmsPreferencesFragment__invalid_host">The text entered was not a valid host</string>
<!-- prompt_passphrase_activity -->
<string name="prompt_passphrase_activity__unlock">Unlock</string>

View file

@ -20,12 +20,17 @@ import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.support.v4.preference.PreferenceFragment;
import android.text.TextUtils;
import android.widget.Toast;
import org.thoughtcrime.securesms.mms.OutgoingMmsConnection;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.net.URI;
import java.net.URISyntaxException;
public class MmsPreferencesFragment extends PreferenceFragment {
@ -47,22 +52,15 @@ public class MmsPreferencesFragment extends PreferenceFragment {
} else {
addPreferencesFromResource(R.xml.preferences_manual_mms);
}
this.findPreference(TextSecurePreferences.MMSC_HOST_PREF).setOnPreferenceChangeListener(new ValidUriVerificationListener());
this.findPreference(TextSecurePreferences.MMSC_PROXY_HOST_PREF).setOnPreferenceChangeListener(new ValidHostnameVerificationListener());
this.findPreference(TextSecurePreferences.MMSC_PROXY_PORT_PREF).setOnPreferenceChangeListener(new EditTextVerificationListener());
this.findPreference(TextSecurePreferences.MMSC_USERNAME_PREF).setOnPreferenceChangeListener(new EditTextVerificationListener());
this.findPreference(TextSecurePreferences.MMSC_PASSWORD_PREF).setOnPreferenceChangeListener(new EditTextVerificationListener());
}
private void initializeEditTextSummary(final EditTextPreference preference) {
if (preference.getText() == null) {
preference.setSummary("Not set");
} else {
preference.setSummary(preference.getText());
}
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
preference.setSummary(newValue == null ? "Not set" : ((String) newValue));
return true;
}
});
preference.setSummary(TextUtils.isEmpty(preference.getText()) ? getString(R.string.MmsPreferencesFragment__not_set) : preference.getText());
}
private void initializeEditTextSummaries() {
@ -89,4 +87,57 @@ public class MmsPreferencesFragment extends PreferenceFragment {
return context.getString(TextSecurePreferences.isUseLocalApnsEnabled(context) ? enabledResId : disabledResId);
}
private class EditTextVerificationListener implements OnPreferenceChangeListener {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String newString = (String)newValue;
if (isValid(newString)) {
preference.setSummary(TextUtils.isEmpty(newString) ? getString(R.string.MmsPreferencesFragment__not_set) : newString);
return true;
} else {
Toast.makeText(getActivity(), getErrorMessage(), Toast.LENGTH_LONG).show();
return false;
}
}
protected boolean isValid(String newString) { return true; }
protected int getErrorMessage() { return 0; }
}
private class ValidUriVerificationListener extends EditTextVerificationListener {
@Override
protected boolean isValid(String newString) {
if (TextUtils.isEmpty(newString)) return true;
try {
new URI(newString);
return true;
} catch (URISyntaxException mue) {
return false;
}
}
@Override
protected int getErrorMessage() {
return R.string.MmsPreferencesFragment__invalid_uri;
}
}
private class ValidHostnameVerificationListener extends EditTextVerificationListener {
@Override
protected boolean isValid(String newString) {
if (TextUtils.isEmpty(newString)) return true;
try {
URI uri = new URI(null, newString, null, null);
return true;
} catch (URISyntaxException mue) {
return false;
}
}
@Override
protected int getErrorMessage() {
return R.string.MmsPreferencesFragment__invalid_host;
}
}
}

View file

@ -47,16 +47,20 @@ public class OutgoingMmsConnection extends MmsConnection {
protected HttpUriRequest constructRequest(boolean useProxy)
throws IOException
{
HttpPostHC4 request = new HttpPostHC4(apn.getMmsc());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
request.addHeader("Content-Type", "application/vnd.wap.mms-message");
request.setEntity(new ByteArrayEntityHC4(mms));
if (useProxy) {
HttpHost proxy = new HttpHost(apn.getProxy(), apn.getPort());
request.setConfig(RequestConfig.custom().setProxy(proxy).build());
try {
HttpPostHC4 request = new HttpPostHC4(apn.getMmsc());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
request.addHeader("Content-Type", "application/vnd.wap.mms-message");
request.setEntity(new ByteArrayEntityHC4(mms));
if (useProxy) {
HttpHost proxy = new HttpHost(apn.getProxy(), apn.getPort());
request.setConfig(RequestConfig.custom().setProxy(proxy).build());
}
return request;
} catch (IllegalArgumentException iae) {
throw new IOException(iae);
}
return request;
}
public void sendNotificationReceived(boolean usingMmsRadio, boolean useProxyIfAvailable)