Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Add CSRF check to more websockets
Browse files Browse the repository at this point in the history
This adds the token to more websockets that were
missing it.

Change-Id: I633ce28ec9602d33a79d613f94582ba0ff265368
Signed-off-by: James Feist <[email protected]>
  • Loading branch information
feistjj authored and gtmills committed Apr 30, 2020
1 parent 052a282 commit 4a16a02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 6 additions & 4 deletions app/common/directives/app-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ window.angular && (function(angular) {
'template': require('./app-header.html'),
'scope': {'path': '='},
'controller': [
'$rootScope', '$scope', 'dataService', 'userModel', '$location',
'$route',
'$rootScope', '$cookies', '$scope', 'dataService', 'userModel',
'$location', '$route',
function(
$rootScope, $scope, dataService, userModel, $location, $route) {
$rootScope, $cookies, $scope, dataService, userModel, $location,
$route) {
$scope.dataService = dataService;
$scope.username = '';

try {
// Create a secure websocket with URL as /subscribe
// TODO: Need to put in a generic APIUtils to avoid duplicate
// controller
var token = $cookies.get('XSRF-TOKEN');
var ws = new WebSocket(
'wss://' + dataService.server_id + '/subscribe');
'wss://' + dataService.server_id + '/subscribe', [token]);
} catch (error) {
console.log('WebSocket', error);
}
Expand Down
15 changes: 10 additions & 5 deletions app/server-control/controllers/virtual-media-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ window.angular && (function(angular) {
'use strict';

angular.module('app.serverControl').controller('virtualMediaController', [
'$scope', 'APIUtils', 'toastService', 'dataService', 'nbdServerService',
function($scope, APIUtils, toastService, dataService, nbdServerService) {
'$scope', '$cookies', 'APIUtils', 'toastService', 'dataService',
'nbdServerService',
function(
$scope, $cookies, APIUtils, toastService, dataService,
nbdServerService) {
$scope.devices = [];

// Only one Virtual Media WebSocket device is currently available.
Expand All @@ -31,7 +34,9 @@ window.angular && (function(angular) {
var file = $scope.devices[index].file;
var id = $scope.devices[index].id;
var host = dataService.getHost().replace('https://', '');
var server = new NBDServer('wss://' + host + '/vm/0/' + id, file, id);
var token = $cookies.get('XSRF-TOKEN');
var server =
new NBDServer('wss://' + host + '/vm/0/' + id, token, file, id);
$scope.devices[index].nbdServer = server;
nbdServerService.addConnection(id, server, file);
server.start();
Expand Down Expand Up @@ -97,7 +102,7 @@ const NBD_STATE_WAIT_CFLAGS = 3;
const NBD_STATE_WAIT_OPTION = 4;
const NBD_STATE_TRANSMISSION = 5;

function NBDServer(endpoint, file, id) {
function NBDServer(endpoint, token, file, id) {
this.file = file;
this.id = id;
this.endpoint = endpoint;
Expand All @@ -106,7 +111,7 @@ function NBDServer(endpoint, file, id) {
this.msgbuf = null;

this.start = function() {
this.ws = new WebSocket(this.endpoint);
this.ws = new WebSocket(this.endpoint, [token]);
this.state = NBD_STATE_OPEN;
this.ws.binaryType = 'arraybuffer';
this.ws.onmessage = this._on_ws_message.bind(this);
Expand Down

0 comments on commit 4a16a02

Please sign in to comment.