Skip to content

Commit 5919d90

Browse files
committed
Merge remote-tracking branch 'upstream/master' into ios-networking-indicator
2 parents 3dbd07c + 27e9598 commit 5919d90

File tree

13 files changed

+116
-216
lines changed

13 files changed

+116
-216
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 2.0.0
4+
5+
- Feature #103: implement HTTP SSL cert modes
6+
7+
- :warning: **Breaking Change**: Removed AngularJS (v1) integration service
8+
- :warning: **Breaking Change**: Removed "enableSSLPinning" and "acceptAllCerts", use "setSSLCertMode" instead
9+
310
## 1.11.1
411

512
- Fixed #92: headers not deserialized on platform "browser"

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,6 @@ This plugin registers a global object located at `cordova.plugin.http`.
4141

4242
Check the [Ionic docs](https://ionicframework.com/docs/native/http/) for how to use this plugin with Ionic-native.
4343

44-
### With AngularJS (Deprecated)
45-
46-
:warning: *This feature is deprecated and will be removed anytime soon.* :warning:
47-
48-
This plugin creates a cordovaHTTP service inside of a cordovaHTTP module. You must load the module when you create your app's module.
49-
50-
```js
51-
var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'cordovaHTTP']);
52-
```
53-
54-
You can then inject the cordovaHTTP service into your controllers. The functions can then be used identically to the examples shown below except that instead of accepting success and failure callback functions, each function returns a promise. For more information on promises in AngularJS read the [AngularJS docs](http://docs.angularjs.org/api/ng/service/$q). For more info on promises in general check out this article on [html5rocks](http://www.html5rocks.com/en/tutorials/es6/promises/). Make sure that you load cordova.js or phonegap.js after AngularJS is loaded.
55-
56-
5744
## Synchronous Functions
5845

5946
### getBasicAuthHeader
@@ -141,32 +128,46 @@ cordova.plugin.http.clearCookies();
141128
## Asynchronous Functions
142129
These functions all take success and error callbacks as their last 2 arguments.
143130

144-
### enableSSLPinning
145-
Enable or disable SSL pinning. This defaults to false.
131+
### setSSLCertMode<a name="setSSLCertMode"></a>
132+
Set SSL Cert handling mode, being one of the following values:
133+
134+
* `default`: default SSL cert handling using system's CA certs
135+
* `nocheck`: disable SSL cert checking, trusting all certs (meant to be used only for testing purposes)
136+
* `pinned`: trust only provided certs
146137

147138
To use SSL pinning you must include at least one .cer SSL certificate in your app project. You can pin to your server certificate or to one of the issuing CA certificates. For ios include your certificate in the root level of your bundle (just add the .cer file to your project/target at the root level). For android include your certificate in your project's platforms/android/assets folder. In both cases all .cer files found will be loaded automatically. If you only have a .pem certificate see this [stackoverflow answer](http://stackoverflow.com/a/16583429/3182729). You want to convert it to a DER encoded certificate with a .cer extension.
148139

149140
As an alternative, you can store your .cer files in the www/certificates folder.
150141

151142
```js
152-
cordova.plugin.http.enableSSLPinning(true, function() {
143+
// enable SSL pinning
144+
cordova.plugin.http.setSSLCertMode('pinned', function() {
153145
console.log('success!');
154146
}, function() {
155147
console.log('error :(');
156148
});
157-
```
158149

159-
### acceptAllCerts
160-
Accept all SSL certificates. Or disable accepting all certificates. This defaults to false.
150+
// use system's default CA certs
151+
cordova.plugin.http.setSSLCertMode('default', function() {
152+
console.log('success!');
153+
}, function() {
154+
console.log('error :(');
155+
});
161156

162-
```js
163-
cordova.plugin.http.acceptAllCerts(true, function() {
157+
// disable SSL cert checking, only meant for testing purposes, do NOT use in production!
158+
cordova.plugin.http.setSSLCertMode('nocheck', function() {
164159
console.log('success!');
165160
}, function() {
166161
console.log('error :(');
167162
});
168163
```
169164

165+
### enableSSLPinning (obsolete)
166+
This function was removed in 2.0.0. Use ["setSSLCertMode"](#setSSLCertMode) to enable SSL pinning (mode "pinned").
167+
168+
### acceptAllCerts (obsolete)
169+
This function was removed in 2.0.0. Use ["setSSLCertMode"](#setSSLCertMode) to disable checking certs (mode "nocheck").
170+
170171
### disableRedirect
171172
If set to `true`, it won't follow redirects automatically. This defaults to false.
172173

@@ -178,8 +179,8 @@ cordova.plugin.http.disableRedirect(true, function() {
178179
});
179180
```
180181

181-
### validateDomainName
182-
This function was removed in v1.6.2. Domain name validation is disabled automatically when you enable "acceptAllCerts".
182+
### validateDomainName (obsolete)
183+
This function was removed in v1.6.2. Domain name validation is disabled automatically when you set SSL cert mode to "nocheck".
183184

184185
### removeCookies
185186
Remove all cookies associated with a given URL.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-advanced-http",
3-
"version": "1.11.1",
3+
"version": "2.0.0",
44
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
55
"scripts": {
66
"buildbrowser": "./scripts/build-test-app.sh --browser",

plugin.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<js-module src="www/messages.js" name="messages"/>
1414
<js-module src="www/local-storage-store.js" name="local-storage-store"/>
1515
<js-module src="www/cookie-handler.js" name="cookie-handler"/>
16-
<js-module src="www/angular-integration.js" name="angular-integration"/>
1716
<js-module src="www/helpers.js" name="helpers"/>
1817
<js-module src="www/advanced-http.js" name="http">
1918
<clobbers target="cordova.plugin.http"/>
@@ -82,4 +81,4 @@
8281
<runs/>
8382
</js-module>
8483
</platform>
85-
</plugin>
84+
</plugin>

src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,25 @@ else if (action.equals("delete")) {
8686
CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, timeoutInMilliseconds, callbackContext);
8787

8888
cordova.getThreadPool().execute(head);
89-
} else if (action.equals("enableSSLPinning")) {
90-
try {
91-
boolean enable = args.getBoolean(0);
92-
this.enableSSLPinning(enable);
93-
callbackContext.success();
94-
} catch(Exception e) {
95-
e.printStackTrace();
96-
callbackContext.error("There was an error setting up ssl pinning");
97-
}
98-
} else if (action.equals("acceptAllCerts")) {
99-
boolean accept = args.getBoolean(0);
89+
} else if (action.equals("setSSLCertMode")) {
90+
String mode = args.getString(0);
10091

101-
if (accept) {
102-
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_TRUSTALL);
103-
} else {
104-
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_DEFAULT);
92+
if (mode.equals("default")) {
93+
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_DEFAULT);
94+
callbackContext.success();
95+
} else if (mode.equals("nocheck")) {
96+
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_TRUSTALL);
97+
callbackContext.success();
98+
} else if (mode.equals("pinned")) {
99+
try {
100+
this.loadSSLCerts();
101+
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
102+
callbackContext.success();
103+
} catch(Exception e) {
104+
e.printStackTrace();
105+
callbackContext.error("There was an error setting up ssl pinning");
106+
}
105107
}
106-
107-
callbackContext.success();
108108
} else if (action.equals("uploadFile")) {
109109
String urlString = args.getString(0);
110110
Object params = args.get(1);
@@ -125,50 +125,44 @@ else if (action.equals("delete")) {
125125

126126
cordova.getThreadPool().execute(download);
127127
} else if (action.equals("disableRedirect")) {
128-
boolean disable = args.getBoolean(0);
129-
CordovaHttp.disableRedirect(disable);
130-
callbackContext.success();
128+
boolean disable = args.getBoolean(0);
129+
CordovaHttp.disableRedirect(disable);
130+
callbackContext.success();
131131
} else {
132132
return false;
133133
}
134134
return true;
135135
}
136136

137-
private void enableSSLPinning(boolean enable) throws GeneralSecurityException, IOException {
138-
if (enable) {
139-
AssetManager assetManager = cordova.getActivity().getAssets();
140-
String[] files = assetManager.list("");
141-
int index;
142-
ArrayList<String> cerFiles = new ArrayList<String>();
143-
for (int i = 0; i < files.length; i++) {
144-
index = files[i].lastIndexOf('.');
145-
if (index != -1) {
146-
if (files[i].substring(index).equals(".cer")) {
147-
cerFiles.add(files[i]);
148-
}
149-
}
150-
}
151-
152-
// scan the www/certificates folder for .cer files as well
153-
files = assetManager.list("www/certificates");
154-
for (int i = 0; i < files.length; i++) {
155-
index = files[i].lastIndexOf('.');
156-
if (index != -1) {
137+
private void loadSSLCerts() throws GeneralSecurityException, IOException {
138+
AssetManager assetManager = cordova.getActivity().getAssets();
139+
String[] files = assetManager.list("");
140+
int index;
141+
ArrayList<String> cerFiles = new ArrayList<String>();
142+
for (int i = 0; i < files.length; i++) {
143+
index = files[i].lastIndexOf('.');
144+
if (index != -1) {
157145
if (files[i].substring(index).equals(".cer")) {
158-
cerFiles.add("www/certificates/" + files[i]);
146+
cerFiles.add(files[i]);
159147
}
160-
}
161148
}
149+
}
162150

163-
for (int i = 0; i < cerFiles.size(); i++) {
164-
InputStream in = cordova.getActivity().getAssets().open(cerFiles.get(i));
165-
InputStream caInput = new BufferedInputStream(in);
166-
HttpRequest.addCert(caInput);
151+
// scan the www/certificates folder for .cer files as well
152+
files = assetManager.list("www/certificates");
153+
for (int i = 0; i < files.length; i++) {
154+
index = files[i].lastIndexOf('.');
155+
if (index != -1) {
156+
if (files[i].substring(index).equals(".cer")) {
157+
cerFiles.add("www/certificates/" + files[i]);
167158
}
159+
}
160+
}
168161

169-
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
170-
} else {
171-
HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_DEFAULT);
162+
for (int i = 0; i < cerFiles.size(); i++) {
163+
InputStream in = cordova.getActivity().getAssets().open(cerFiles.get(i));
164+
InputStream caInput = new BufferedInputStream(in);
165+
HttpRequest.addCert(caInput);
172166
}
173167
}
174168
}

src/ios/CordovaHttpPlugin.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
@interface CordovaHttpPlugin : CDVPlugin
66

7-
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command;
8-
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command;
7+
- (void)setSSLCertMode:(CDVInvokedUrlCommand*)command;
98
- (void)disableRedirect:(CDVInvokedUrlCommand*)command;
109
- (void)post:(CDVInvokedUrlCommand*)command;
1110
- (void)get:(CDVInvokedUrlCommand*)command;

src/ios/CordovaHttpPlugin.m

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,31 @@ - (NSMutableDictionary*)copyHeaderFields:(NSDictionary *)headerFields {
121121
return headerFieldsCopy;
122122
}
123123

124-
- (void)setTimeout:(NSTimeInterval)timeout forManager:(AFHTTPSessionManager*)manager {
125-
[manager.requestSerializer setTimeoutInterval:timeout];
126-
}
124+
- (void)setSSLCertMode:(CDVInvokedUrlCommand*)command {
125+
NSString *certMode = [command.arguments objectAtIndex:0];
127126

128-
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
129-
bool enable = [[command.arguments objectAtIndex:0] boolValue];
130-
131-
if (enable) {
132-
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
133-
} else {
127+
if ([certMode isEqualToString: @"default"]) {
128+
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
129+
securityPolicy.allowInvalidCertificates = NO;
130+
securityPolicy.validatesDomainName = YES;
131+
} else if ([certMode isEqualToString: @"nocheck"]) {
134132
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
133+
securityPolicy.allowInvalidCertificates = YES;
134+
securityPolicy.validatesDomainName = NO;
135+
} else if ([certMode isEqualToString: @"pinned"]) {
136+
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
137+
securityPolicy.allowInvalidCertificates = NO;
138+
securityPolicy.validatesDomainName = YES;
135139
}
136140

137141
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
138142
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
139143
}
140144

145+
- (void)setTimeout:(NSTimeInterval)timeout forManager:(AFHTTPSessionManager*)manager {
146+
[manager.requestSerializer setTimeoutInterval:timeout];
147+
}
148+
141149
- (void)disableRedirect:(CDVInvokedUrlCommand*)command {
142150
CDVPluginResult* pluginResult = nil;
143151
bool disable = [[command.arguments objectAtIndex:0] boolValue];
@@ -148,17 +156,6 @@ - (void)disableRedirect:(CDVInvokedUrlCommand*)command {
148156
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
149157
}
150158

151-
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command {
152-
CDVPluginResult* pluginResult = nil;
153-
bool allow = [[command.arguments objectAtIndex:0] boolValue];
154-
155-
securityPolicy.allowInvalidCertificates = allow;
156-
securityPolicy.validatesDomainName = !allow;
157-
158-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
159-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
160-
}
161-
162159
- (void)post:(CDVInvokedUrlCommand*)command {
163160
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
164161
manager.securityPolicy = securityPolicy;

0 commit comments

Comments
 (0)