north-star / commit
added room id validation, so we're not dealing with nonsense
src/app.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/app.js b/src/app.js index 922a77c..349f50b 100644 --- a/src/app.js +++ b/src/app.js @@ -110,9 +110,19 @@ console.debug('Signed in anonymously:', authSignInResponse); const uid = authSignInResponse.user.uid; console.debug('User ID:', uid); -let roomId = location.hash.split('room=')[1]; +const BASE_ROOM_URL = `${window.location.origin}${window.location.pathname}`; +const ROOM_ID_LENGTH = 8; +const ROOM_ID_PATTERN = new RegExp(`^[a-z0-9]{${ROOM_ID_LENGTH}}$`); +const parseRoomIdFromHash = (hashValue) => { + const normalizedHash = (hashValue || '').startsWith('#') ? hashValue.slice(1) : (hashValue || ''); + const hashParams = new URLSearchParams(normalizedHash); + const roomValue = (hashParams.get('room') || '').trim(); + return ROOM_ID_PATTERN.test(roomValue) ? roomValue : null; +}; + +let roomId = parseRoomIdFromHash(location.hash); if (!roomId) { - roomId = crypto.randomUUID().slice(0, 8); + roomId = crypto.randomUUID().replace(/-/g, '').slice(0, ROOM_ID_LENGTH); location.hash = 'room=' + roomId; } console.debug('Room ID:', roomId); @@ -121,8 +131,6 @@ if (roomCurrent) { roomCurrent.textContent = `You are currently in room ${roomId}`; } -const BASE_ROOM_URL = `${window.location.origin}${window.location.pathname}`; -const ROOM_ID_PATTERN = /^[a-f0-9]{8}$/i; if (inviteRoomLink) { inviteRoomLink.href = `${BASE_ROOM_URL}#room=${encodeURIComponent(roomId)}`; } @@ -202,23 +210,15 @@ const parseRoomIdFromInput = (value) => { if (!input) return null; if (ROOM_ID_PATTERN.test(input)) { - return input.toLowerCase(); + return input; } try { const parsedUrl = new URL(input, window.location.href); - const hashValue = parsedUrl.hash.startsWith('#') ? parsedUrl.hash.slice(1) : parsedUrl.hash; - const hashParams = new URLSearchParams(hashValue); - const fromHash = (hashParams.get('room') || '').trim(); - - if (ROOM_ID_PATTERN.test(fromHash)) { - return fromHash.toLowerCase(); - } + return parseRoomIdFromHash(parsedUrl.hash); } catch (err) { return null; } - - return null; }; if (inviteRoomLink) { @@ -246,11 +246,11 @@ if (joinRoomLink) { const parsedRoomId = parseRoomIdFromInput(roomInput); if (!parsedRoomId) { - alert('Invalid room ID or URL. Please enter an 8-character hexadecimal room ID or a URL containing it.'); + alert(`Invalid room ID or URL. Please enter a ${ROOM_ID_LENGTH}-character lowercase alphanumeric room ID or a URL containing it.`); return; } - if (parsedRoomId === roomId.toLowerCase()) { + if (parsedRoomId === roomId) { alert('You are already in this room!'); return; }