Browse Source

Escapes html special chars and makes invitation field non-editable.

j8
yanas 11 years ago
parent
commit
8ebe2d9468
3 changed files with 63 additions and 31 deletions
  1. 39
    27
      app.js
  2. 6
    4
      chat.js
  3. 18
    0
      util.js

+ 39
- 27
app.js View File

83
             roomnode = path.substr(1).toLowerCase();
83
             roomnode = path.substr(1).toLowerCase();
84
         } else {
84
         } else {
85
             roomnode = Math.random().toString(36).substr(2, 20);
85
             roomnode = Math.random().toString(36).substr(2, 20);
86
-            window.history.pushState('VideoChat', 'Room: ' + roomnode, window.location.pathname + roomnode);
86
+            window.history.pushState('VideoChat',
87
+                    'Room: ' + roomnode, window.location.pathname + roomnode);
87
         }
88
         }
88
     }
89
     }
89
 
90
 
165
     var remotes = document.getElementById('remoteVideos');
166
     var remotes = document.getElementById('remoteVideos');
166
 
167
 
167
     if (data.peerjid) {
168
     if (data.peerjid) {
168
-        container  = document.getElementById('participant_' + Strophe.getResourceFromJid(data.peerjid));
169
+        container  = document.getElementById(
170
+                'participant_' + Strophe.getResourceFromJid(data.peerjid));
169
         if (!container) {
171
         if (!container) {
170
             console.warn('no container for', data.peerjid);
172
             console.warn('no container for', data.peerjid);
171
             // create for now...
173
             // create for now...
172
             // FIXME: should be removed
174
             // FIXME: should be removed
173
-            container = addRemoteVideoContainer('participant_' + Strophe.getResourceFromJid(data.peerjid));
175
+            container = addRemoteVideoContainer(
176
+                    'participant_' + Strophe.getResourceFromJid(data.peerjid));
174
         } else {
177
         } else {
175
             //console.log('found container for', data.peerjid);
178
             //console.log('found container for', data.peerjid);
176
         }
179
         }
588
                 });
591
                 });
589
 
592
 
590
     $('#presentation>iframe').attr('id', preziPlayer.options.preziId);
593
     $('#presentation>iframe').attr('id', preziPlayer.options.preziId);
591
-                 
594
+
592
     preziPlayer.on(PreziPlayer.EVENT_STATUS, function(event) {
595
     preziPlayer.on(PreziPlayer.EVENT_STATUS, function(event) {
593
         console.log("prezi status", event.value);
596
         console.log("prezi status", event.value);
594
         if (event.value == PreziPlayer.STATUS_CONTENT_READY) {
597
         if (event.value == PreziPlayer.STATUS_CONTENT_READY) {
897
                      if(v)
900
                      if(v)
898
                      {
901
                      {
899
                         var lockKey = document.getElementById('lockKey');
902
                         var lockKey = document.getElementById('lockKey');
900
-                     
903
+
901
                         if (lockKey.value)
904
                         if (lockKey.value)
902
                         {
905
                         {
903
-                            setSharedKey(lockKey.value);
906
+                            setSharedKey(Util.escapeHtml(lockKey.value));
904
                             lockRoom(true);
907
                             lockRoom(true);
905
                         }
908
                         }
906
                      }
909
                      }
913
  * Opens the invite link dialog.
916
  * Opens the invite link dialog.
914
  */
917
  */
915
 function openLinkDialog() {
918
 function openLinkDialog() {
916
-    $.prompt('<input id="inviteLinkRef" type="text" value="' + roomUrl + '" onclick="this.select();">',
919
+    $.prompt('<input id="inviteLinkRef" type="text" value="'
920
+            + encodeURI(roomUrl) + '" onclick="this.select();" readonly>',
917
              {
921
              {
918
              title: "Share this link with everyone you want to invite",
922
              title: "Share this link with everyone you want to invite",
919
              persistent: false,
923
              persistent: false,
949
 
953
 
950
                         if ($('#requireNicknames').is(":checked"))
954
                         if ($('#requireNicknames').is(":checked"))
951
                         {
955
                         {
952
-                            // it is checked                        
956
+                            // it is checked
953
                         }
957
                         }
954
              /*
958
              /*
955
                         var lockKey = document.getElementById('lockKey');
959
                         var lockKey = document.getElementById('lockKey');
986
         });
990
         });
987
     }
991
     }
988
     else if (preziPlayer != null) {
992
     else if (preziPlayer != null) {
989
-        $.prompt("Another participant is already sharing a Prezi. This conference allows only one Prezi at a time.",
993
+        $.prompt("Another participant is already sharing a Prezi." +
994
+                "This conference allows only one Prezi at a time.",
990
                  {
995
                  {
991
                  title: "Share a Prezi",
996
                  title: "Share a Prezi",
992
                  buttons: { "Ok": true},
997
                  buttons: { "Ok": true},
1012
 
1017
 
1013
                     if (preziUrl.value)
1018
                     if (preziUrl.value)
1014
                     {
1019
                     {
1015
-                        if (preziUrl.value.indexOf('http://prezi.com/') != 0
1016
-                            && preziUrl.value.indexOf('https://prezi.com/') != 0)
1020
+                        var urlValue
1021
+                            = encodeURI(Util.escapeHtml(preziUrl.value));
1022
+
1023
+                        if (urlValue.indexOf('http://prezi.com/') != 0
1024
+                            && urlValue.indexOf('https://prezi.com/') != 0)
1017
                         {
1025
                         {
1018
                             $.prompt.goToState('state1');
1026
                             $.prompt.goToState('state1');
1019
                             return false;
1027
                             return false;
1020
                         }
1028
                         }
1021
                         else {
1029
                         else {
1022
-                            var presIdTmp = preziUrl.value.substring(preziUrl.value.indexOf("prezi.com/") + 10);
1023
-                            if (presIdTmp.indexOf('/') < 2) {
1030
+                            var presIdTmp = urlValue.substring(urlValue.indexOf("prezi.com/") + 10);
1031
+                            if (!Util.isAlphanumeric(presIdTmp)
1032
+                                    || presIdTmp.indexOf('/') < 2) {
1024
                                 $.prompt.goToState('state1');
1033
                                 $.prompt.goToState('state1');
1025
                                 return false;
1034
                                 return false;
1026
                             }
1035
                             }
1027
                             else {
1036
                             else {
1028
-                                connection.emuc.addPreziToPresence(preziUrl.value, 0);
1037
+                                connection.emuc.addPreziToPresence(urlValue, 0);
1029
                                 connection.emuc.sendPresence();
1038
                                 connection.emuc.sendPresence();
1030
                                 $.prompt.close();
1039
                                 $.prompt.close();
1031
                             }
1040
                             }
1053
         };
1062
         };
1054
 
1063
 
1055
         var myPrompt = jQuery.prompt(openPreziState);
1064
         var myPrompt = jQuery.prompt(openPreziState);
1056
-        
1065
+
1057
         myPrompt.on('impromptu:loaded', function(e) {
1066
         myPrompt.on('impromptu:loaded', function(e) {
1058
                     document.getElementById('preziUrl').focus();
1067
                     document.getElementById('preziUrl').focus();
1059
                     });
1068
                     });
1071
         connection.emuc.lockRoom(sharedKey);
1080
         connection.emuc.lockRoom(sharedKey);
1072
     else
1081
     else
1073
         connection.emuc.lockRoom('');
1082
         connection.emuc.lockRoom('');
1074
-    
1083
+
1075
     updateLockButton();
1084
     updateLockButton();
1076
 }
1085
 }
1077
 
1086
 
1202
  * Shows the display name for the given video.
1211
  * Shows the display name for the given video.
1203
  */
1212
  */
1204
 function showDisplayName(videoSpanId, displayName) {
1213
 function showDisplayName(videoSpanId, displayName) {
1214
+    var escDisplayName = Util.escapeHtml(displayName);
1215
+
1205
     var nameSpan = $('#' + videoSpanId + '>span.displayname');
1216
     var nameSpan = $('#' + videoSpanId + '>span.displayname');
1206
 
1217
 
1207
     // If we already have a display name for this video.
1218
     // If we already have a display name for this video.
1209
         var nameSpanElement = nameSpan.get(0);
1220
         var nameSpanElement = nameSpan.get(0);
1210
 
1221
 
1211
         if (nameSpanElement.id == 'localDisplayName'
1222
         if (nameSpanElement.id == 'localDisplayName'
1212
-            && $('#localDisplayName').html() != displayName)
1213
-            $('#localDisplayName').html(displayName);
1223
+            && $('#localDisplayName').html() != escDisplayName)
1224
+            $('#localDisplayName').html(escDisplayName);
1214
         else
1225
         else
1215
-            $('#' + videoSpanId + '_name').html(displayName);
1226
+            $('#' + videoSpanId + '_name').html(escDisplayName);
1216
     }
1227
     }
1217
     else {
1228
     else {
1218
         var editButton = null;
1229
         var editButton = null;
1230
+
1219
         if (videoSpanId == 'localVideoContainer') {
1231
         if (videoSpanId == 'localVideoContainer') {
1220
             editButton = createEditDisplayNameButton();
1232
             editButton = createEditDisplayNameButton();
1221
         }
1233
         }
1222
-
1223
-        if (displayName.length) {
1234
+        if (escDisplayName.length) {
1224
             nameSpan = document.createElement('span');
1235
             nameSpan = document.createElement('span');
1225
             nameSpan.className = 'displayname';
1236
             nameSpan.className = 'displayname';
1226
-            nameSpan.innerHTML = displayName;
1237
+            nameSpan.innerHTML = escDisplayName;
1227
             $('#' + videoSpanId)[0].appendChild(nameSpan);
1238
             $('#' + videoSpanId)[0].appendChild(nameSpan);
1228
         }
1239
         }
1229
 
1240
 
1233
         else {
1244
         else {
1234
             nameSpan.id = 'localDisplayName';
1245
             nameSpan.id = 'localDisplayName';
1235
             $('#' + videoSpanId)[0].appendChild(editButton);
1246
             $('#' + videoSpanId)[0].appendChild(editButton);
1236
-            
1247
+
1237
             var editableText = document.createElement('input');
1248
             var editableText = document.createElement('input');
1238
             editableText.className = 'displayname';
1249
             editableText.className = 'displayname';
1239
             editableText.id = 'editDisplayName';
1250
             editableText.id = 'editDisplayName';
1240
 
1251
 
1241
-            if (displayName.length)
1242
-                editableText.value = displayName.substring(0, displayName.indexOf(' (me)'));
1252
+            if (escDisplayName.length)
1253
+                editableText.value
1254
+                    = escDisplayName.substring(0, escDisplayName.indexOf(' (me)'));
1243
 
1255
 
1244
             editableText.setAttribute('style', 'display:none;');
1256
             editableText.setAttribute('style', 'display:none;');
1245
             editableText.setAttribute('placeholder', 'ex. Jane Pink');
1257
             editableText.setAttribute('placeholder', 'ex. Jane Pink');
1254
 
1266
 
1255
                 var inputDisplayNameHandler = function(name) {
1267
                 var inputDisplayNameHandler = function(name) {
1256
                     if (nickname != name) {
1268
                     if (nickname != name) {
1257
-                        nickname = name;
1269
+                        nickname = Util.escapeHtml(name);
1258
                         window.localStorage.displayname = nickname;
1270
                         window.localStorage.displayname = nickname;
1259
                         connection.emuc.addDisplayNameToPresence(nickname);
1271
                         connection.emuc.addDisplayNameToPresence(nickname);
1260
                         connection.emuc.sendPresence();
1272
                         connection.emuc.sendPresence();
1263
                     }
1275
                     }
1264
 
1276
 
1265
                     if (!$('#localDisplayName').is(":visible")) {
1277
                     if (!$('#localDisplayName').is(":visible")) {
1266
-                        $('#localDisplayName').html(name + " (me)");
1278
+                        $('#localDisplayName').html(nickname + " (me)");
1267
                         $('#localDisplayName').show();
1279
                         $('#localDisplayName').show();
1268
                         $('#editDisplayName').hide();
1280
                         $('#editDisplayName').hide();
1269
                     }
1281
                     }

+ 6
- 4
chat.js View File

19
         $('#nickinput').keydown(function(event) {
19
         $('#nickinput').keydown(function(event) {
20
             if (event.keyCode == 13) {
20
             if (event.keyCode == 13) {
21
                 event.preventDefault();
21
                 event.preventDefault();
22
-                var val = this.value;
22
+                var val = Util.escapeHtml(this.value);
23
                 this.value = '';
23
                 this.value = '';
24
                 if (!nickname) {
24
                 if (!nickname) {
25
                     nickname = val;
25
                     nickname = val;
38
         $('#usermsg').keydown(function(event) {
38
         $('#usermsg').keydown(function(event) {
39
             if (event.keyCode == 13) {
39
             if (event.keyCode == 13) {
40
                 event.preventDefault();
40
                 event.preventDefault();
41
-                var message = this.value;
41
+                var message = Util.escapeHtml(this.value);
42
                 $('#usermsg').val('').trigger('autosize.resize');
42
                 $('#usermsg').val('').trigger('autosize.resize');
43
                 this.focus();
43
                 this.focus();
44
                 connection.emuc.sendMessage(message, nickname);
44
                 connection.emuc.sendMessage(message, nickname);
78
         }
78
         }
79
 
79
 
80
         //replace links and smileys
80
         //replace links and smileys
81
-        message = processReplacements(message);
81
+        var escMessage = Util.escapeHtml(message);
82
+        var escDisplayName = Util.escapeHtml(displayName);
83
+        message = processReplacements(escMessage);
82
 
84
 
83
         $('#chatconversation').append('<div class="' + divClassName + '"><b>'
85
         $('#chatconversation').append('<div class="' + divClassName + '"><b>'
84
-                                        + displayName + ': </b>'
86
+                                        + escDisplayName + ': </b>'
85
                                         + message + '</div>');
87
                                         + message + '</div>');
86
         $('#chatconversation').animate(
88
         $('#chatconversation').animate(
87
                 { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
89
                 { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);

+ 18
- 0
util.js View File

39
         document.getElementById(id).play();
39
         document.getElementById(id).play();
40
     };
40
     };
41
 
41
 
42
+    /**
43
+     * Escapes the given text.
44
+     */
45
+    my.escapeHtml = function(unsafeText) {
46
+        return $('<div/>').text(unsafeText).html();
47
+    };
48
+
49
+    /**
50
+     * Indicates if the given string is an alphanumeric string.
51
+     * Note that some special characters are also allowed (-, _ , /) for the
52
+     * purpose of checking URIs. (FIXME: This should maybe moved to another not
53
+     * so generic method in the future.)
54
+     */
55
+    my.isAlphanumeric = function(unsafeText) {
56
+        var regex = /^[a-z0-9-_\/]+$/i;
57
+        return regex.test(unsafeText);
58
+    };
59
+
42
     return my;
60
     return my;
43
 }(Util || {}));
61
 }(Util || {}));

Loading…
Cancel
Save