Skip to content

Commit

Permalink
Merge pull request #12 from freedomjs/dborkan-secure
Browse files Browse the repository at this point in the history
Add secure method to tcp socket for firefox
  • Loading branch information
dborkan committed Aug 12, 2014
2 parents fede1e4 + 5718b1c commit 543f814
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-jshint');

grunt.registerTask('freedom-firefox', [
grunt.registerTask('build', [
'jshint:providers',
'uglify'
]);
grunt.registerTask('writeJsonDir', 'Write', writeJsonDir);
grunt.registerTask('build_test', [
'freedom-firefox',
'build',
'copy:test',
'writeJsonDir'
]);
grunt.registerTask('default', ['freedom-firefox']);
grunt.registerTask('default', ['build']);

// Write the contents of the data directory in the test extension
// into a JSON file. We have to do this because files/directories
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "freedom-for-firefox",
"description": "Embracing a distributed web",
"version": "0.4.1",
"version": "0.4.2",
"homepage": "http://freedomjs.org",
"bugs": {
"url": "http://github.com/freedomjs/freedom-for-firefox/issues",
Expand All @@ -17,7 +17,7 @@
"url": "https://github.com/freedomjs/freedom-for-firefox"
},
"devDependencies": {
"freedom": "~0.5.0",
"freedom": "~0.5.5",
"grunt": "~0.4.2",
"es5-shim": "^3.1.1",
"es6-promise": "~0.1.1",
Expand All @@ -27,7 +27,7 @@
"grunt-contrib-jshint": "~0.8.0"
},
"peerDependencies": {
"freedom": "~0.5.0"
"freedom": "~0.5.5"
},
"scripts": {}
}
8 changes: 5 additions & 3 deletions providers/client_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ ClientSocket.prototype._setupTransport = function(transport) {

};

ClientSocket.prototype.connect = function(hostname, port) {
ClientSocket.prototype.connect = function(hostname, port, startTls) {
if (typeof this.transport !== 'undefined') {
throw new Error('Socket already connected');
}

var transport = socketTransportService.createTransport([null],
0,
var socketTypes = startTls ? ['starttls'] : [null];
var numSocketTypes = startTls ? 1 : 0;
var transport = socketTransportService.createTransport(socketTypes,
numSocketTypes,
hostname,
port,
null);
Expand Down
28 changes: 27 additions & 1 deletion providers/tcp_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,36 @@ Socket_firefox.prototype.close = function(continuation) {
continuation();
};

// TODO: handle failures.
Socket_firefox.prototype.connect = function(hostname, port, continuation) {
this.clientSocket = new ClientSocket();
this.clientSocket.setOnDataListener(this._onData.bind(this));
this.clientSocket.connect(hostname, port);
this.clientSocket.connect(hostname, port, false);
this.hostname = hostname;
this.port = port;
continuation();
};

// TODO: handle failures.
Socket_firefox.prototype.secure = function(continuation) {
if (!this.hostname || !this.port || !this.clientSocket) {
continuation(undefined, {
"errcode": "SOCKET_NOT_CONNECTED",
"message": "Cannot Secure Not Connected Socket"
});
return;
}
// Create a new ClientSocket (nsISocketTransport) object for the existing
// hostname and port, using type 'starttls'. This will upgrade the existing
// connection to TLS, rather than create a new connection.
// TODO: check to make sure this doesn't result in weird race conditions if
// we have 2 pieces of code both trying to connect to the same hostname/port
// and do a starttls flow (e.g. if there are 2 instances of a GTalk social
// provider that are both trying to connect to GTalk simultaneously with
// different logins).
this.clientSocket = new ClientSocket();
this.clientSocket.setOnDataListener(this._onData.bind(this));
this.clientSocket.connect(this.hostname, this.port, true);
continuation();
};

Expand Down
46 changes: 43 additions & 3 deletions test/data/firefox_tests/tcp_socket.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe("sockets", function() {
describe("tcp sockets", function() {
var clientSocket, serverSocket;
beforeEach(function() {
serverSocket = new ServerSocket("localhost", 8081);
Expand All @@ -11,7 +11,7 @@ describe("sockets", function() {
serverSocket.disconnect();
done();
};
clientSocket.connect("localhost", 8081);
clientSocket.connect("localhost", 8081, false);
});

it("receives data", function(done) {
Expand All @@ -24,7 +24,7 @@ describe("sockets", function() {
done();
});
};
clientSocket.connect("localhost", 8081);
clientSocket.connect("localhost", 8081, false);
clientSocket.write(str2ab(stringMessage));
});

Expand All @@ -36,4 +36,44 @@ describe("sockets", function() {
}
return buf;
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
};

it("secures socket with starttls", function(done) {
var INIT_XMPP = '<stream:stream ' +
'xmlns:stream="http://etherx.jabber.org/streams" ' +
'version="1.0" xmlns="jabber:client" to="chat.facebook.com" ' +
'xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">';
var START_TLS = '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>';
var X_FACEBOOK_PLATFORM_AUTH =
'<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" ' +
'mechanism="X-FACEBOOK-PLATFORM"></auth>';

// Test that we can connect to chat.facebook.com, then upgrade to a tls
// socket and get the challenge. If we fail to upgrade the socket to tls
// facebook will not return a challenge.
var onDataCount = 0;
var dispatchEvent = function (eventType, data) {
if (eventType == 'onData') {
var xmlString = ab2str(data.data);
if (xmlString.indexOf('<challenge') >= 0) {
done();
}
++onDataCount;
if (onDataCount == 1) {
socket.write(str2ab(START_TLS), continuation);
} else if (onDataCount == 2) {
socket.secure(continuation);
socket.write(str2ab(INIT_XMPP), continuation);
} else if (onDataCount == 3) {
socket.write(str2ab(X_FACEBOOK_PLATFORM_AUTH), continuation);
}
}
};
var continuation = function() {};
var socket = new Socket_firefox(undefined, dispatchEvent, undefined);
socket.connect('chat.facebook.com', 5222, continuation);
socket.write(str2ab(INIT_XMPP), continuation);
});
});
4 changes: 3 additions & 1 deletion test/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const jasmine = require("jasmine.js");

var providers = ["firefox_providers/client_socket.js",
"firefox_providers/server_socket.js",
"firefox_providers/tcp_socket.js",
"firefox_providers/udp_socket.js"];

var tests = [
"firefox_tests/client_socket.spec.js",
"firefox_tests/udp_socket.spec.js"
"firefox_tests/udp_socket.spec.js",
"firefox_tests/tcp_socket.spec.js"
];


Expand Down

0 comments on commit 543f814

Please sign in to comment.