Skip to content

Commit 4157cc9

Browse files
committed
Guard the whole boundary ring, not only the parse (Copilot on #141)
JSON.parse succeeding does not make coordinates[0] a ring of number pairs; an undefined or malformed ring would throw at .map inside the submit listener, exactly the failure mode the guard removes. Test pins it.
1 parent 5ee91bc commit 4157cc9

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

assets/javascripts/gtt_fiware_form.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,15 @@
191191
try {
192192
ring = JSON.parse(mode.dataset.geom).geometry.coordinates[0];
193193
} catch (err) {
194-
// data-geom is server-rendered; if it is missing or malformed,
195-
// leave the stored triple untouched rather than submitting a
196-
// half-written one from inside a throwing submit listener.
197-
return;
194+
ring = null;
198195
}
196+
// data-geom is server-rendered; if it is missing, malformed or not
197+
// a ring of number pairs, leave the stored triple untouched rather
198+
// than throwing from inside the submit listener.
199+
var isPair = function(c) {
200+
return Array.isArray(c) && typeof c[0] === 'number' && typeof c[1] === 'number';
201+
};
202+
if (!Array.isArray(ring) || ring.length === 0 || !ring.every(isPair)) { return; }
199203
var geom = ring
200204
.map(function(c) { return [Number(c[1].toFixed(5)), Number(c[0].toFixed(5))]; })
201205
.join(';');

test/javascripts/serialization.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ describe('geographic area', () => {
139139
.toBe('35.67,139.69;35.68,139.7;35.675,139.695;35.67,139.69');
140140
});
141141

142+
// data-geom is server-rendered, but a malformed value must not throw from
143+
// inside the submit listener (the submit proceeds regardless), and must
144+
// leave the stored triple alone rather than half-writing it.
145+
it('boundary with a malformed data-geom leaves the triple untouched', () => {
146+
buildForm({ geoMode: 'boundary' });
147+
initForm();
148+
document.querySelector('input[value="boundary"]').dataset.geom = '{"geometry":{"coordinates":[]}}';
149+
field('subscription_template_expression_georel').value = 'kept';
150+
expect(() => submitForm()).not.toThrow();
151+
expect(field('subscription_template_expression_georel').value).toBe('kept');
152+
expect(field('subscription_template_expression_geometry').value).toBe('');
153+
});
154+
142155
it('custom leaves the triple untouched', () => {
143156
buildForm({ geoMode: 'custom' });
144157
initForm();

0 commit comments

Comments
 (0)