Skip to content

Commit ba8de56

Browse files
authored
fix: Disallow special characters for custom level fields (#1755)
1 parent b638b95 commit ba8de56

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

game/static/game/js/level_editor.js

+53-15
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ ocargo.LevelEditor = function(levelId) {
643643
return;
644644
}
645645

646-
const regex = /^(\w?[ ]?)*$/;
646+
const regex = /^[\w ]*$/;
647647
const validString = regex.exec(nameInput.val());
648648
if (!validString) {
649649
ocargo.Drawing.startPopup(
@@ -1685,7 +1685,7 @@ ocargo.LevelEditor = function(levelId) {
16851685
var bBox = image.getBBox();
16861686
imageWidth = bBox.width;
16871687
imageHeight = bBox.height;
1688-
1688+
16891689
var paperPosition = paper.position();
16901690
originX = x - paperPosition.left + paper.scrollLeft() - imageWidth/2;
16911691
originY = y - paperPosition.top + paper.scrollTop() - imageHeight/2;
@@ -1697,7 +1697,7 @@ ocargo.LevelEditor = function(levelId) {
16971697
}
16981698

16991699
function onDragEnd() {
1700-
1700+
17011701
if (trashcanOpen) {
17021702
cow.destroy();
17031703
unmarkOldCowSquare(controlledCoord, cow);
@@ -1714,15 +1714,15 @@ ocargo.LevelEditor = function(levelId) {
17141714
else {
17151715
var cowX = paperX;
17161716
var cowY = paperY;
1717-
1717+
17181718
if (paperWidth < paperX + imageWidth) {
17191719
cowX = paperWidth - imageWidth
17201720
}
1721-
1721+
17221722
if (paperHeight < paperY + imageHeight) {
17231723
cowY = paperHeight - imageHeight
17241724
}
1725-
1725+
17261726
image.transform('t' + cowX + ',' + cowY);
17271727
}
17281728
}
@@ -1875,12 +1875,12 @@ ocargo.LevelEditor = function(levelId) {
18751875
internalCow.destroy();
18761876
}
18771877
}
1878-
1878+
18791879
if (!trashcanOpen) {
18801880
setCowMarkingsOnMouseUp(controlledCoord, internalCow);
18811881
adjustCowGroupMinMaxFields(internalCow);
18821882
}
1883-
1883+
18841884
$(document)
18851885
.off('mousemove', handleDraggableCowDragging)
18861886
.off('mouseup mouseleave', handleDraggableCowMouseUp);
@@ -2416,7 +2416,7 @@ ocargo.LevelEditor = function(levelId) {
24162416
"><path class="blocklyPath" stroke="none" fill="#5b80a5" d="m 0,0 H 111.34375 v 30 H 0 V 20 c 0,-10 -8,8 -8,-7.5 s 8,2.5 8,-7.5 z
24172417
"></path><path class="blocklyPathLight" stroke="#8ca6c0" d="m 0.5,0.5 H 110.84375 M 110.84375,0.5 M 0.5,29.5 V 18.5 m -7.36,-0.5 q -1.52,-5.5 0,-11 m 7.36,1 V 0.5 H 1
24182418
"></path><text class="blocklyText" y="12.5" transform="translate(10,5)">pigeons</text><g transform="translate(71.34375,5)"><image height="20px" width="30px" xlink:href="/static/game/image/pigeon.svg" alt=""></image></g></g></svg>`;
2419-
2419+
24202420
const cowHTML = `<svg class="block_image"><g transform="translate(10,0)" <path="" class="blocklyPathDark" fill="#496684" d="m 0,0 H 93.40625 v 30 H 0 V 20 c 0,-10 -8,8 -8,-7.5 s 8,2.5 8,-7.5 z
24212421
"><path class="blocklyPath" stroke="none" fill="#5b80a5" d="m 0,0 H 93.40625 v 30 H 0 V 20 c 0,-10 -8,8 -8,-7.5 s 8,2.5 8,-7.5 z
24222422
"></path><path class="blocklyPathLight" stroke="#8ca6c0" d="m 0.5,0.5 H 92.90625 M 92.90625,0.5 M 0.5,29.5 V 18.5 m -7.36,-0.5 q -1.52,-5.5 0,-11 m 7.36,1 V 0.5 H 1
@@ -2545,17 +2545,55 @@ ocargo.LevelEditor = function(levelId) {
25452545
state.python_view_enabled = language === 'blocklyWithPythonView';
25462546
state.python_enabled = language === 'python' || language === 'both';
25472547

2548+
const regex = /^[\w.?!', ]*$/;
2549+
const subtitleValue = $('#subtitle').val();
2550+
const descriptionValue = $('#description').val();
2551+
const hintValue = $('#hint').val();
2552+
25482553
// Description and hint data
2549-
if ($('#subtitle').val().length > 0) {
2550-
state.subtitle = $('#subtitle').val();
2554+
if (subtitleValue.length > 0) {
2555+
if (regex.exec(subtitleValue)) {
2556+
state.subtitle = subtitleValue;
2557+
}
2558+
else {
2559+
ocargo.Drawing.startPopup(
2560+
"Oh no!",
2561+
"You used some invalid characters for your level subtitle.",
2562+
"Try saving your level again using only" +
2563+
" letters, numbers and standard punctuation."
2564+
);
2565+
return
2566+
}
25512567
}
25522568

2553-
if ($('#description').val().length > 0) {
2554-
state.lesson = $('#description').val();
2569+
if (descriptionValue.length > 0) {
2570+
if (regex.exec(descriptionValue)) {
2571+
state.lesson = descriptionValue;
2572+
}
2573+
else {
2574+
ocargo.Drawing.startPopup(
2575+
"Oh no!",
2576+
"You used some invalid characters for your level description.",
2577+
"Try saving your level again using only" +
2578+
" letters and numbers and standard punctuation."
2579+
);
2580+
return
2581+
}
25552582
}
25562583

2557-
if ($('#hint').val().length > 0) {
2558-
state.hint = $('#hint').val();
2584+
if (hintValue.length > 0) {
2585+
if (regex.exec(hintValue)) {
2586+
state.hint = hintValue;
2587+
}
2588+
else {
2589+
ocargo.Drawing.startPopup(
2590+
"Oh no!",
2591+
"You used some invalid characters for your level hint.",
2592+
"Try saving your level again using only" +
2593+
" letters and numbers and standard punctuation."
2594+
);
2595+
return
2596+
}
25592597
}
25602598

25612599
// Other data

0 commit comments

Comments
 (0)