diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6b13cb6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: CI + +on: + pull_request: + push: + branches: + - main + +jobs: + build-and-validate: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + + - name: Set up Java + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 21 + + - name: Install dependencies + run: npm ci + + - name: Validate Firebase rules JSON + run: npm run validate:rules:json + + - name: Validate Firebase rules syntax + run: npm run validate:rules + + - name: Build app + run: npm run build diff --git a/.gitignore b/.gitignore index 3b68ffa..daba8bc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dist/ **/#*# .env.local .env.*.local +database-debug.log diff --git a/README.md b/README.md index c564100..893237f 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ VITE_TEACHER_PASSWORD=your_password_here ### Step 7 — Set Firebase security rules 1. Firebase Console → **Realtime Database → Rules** tab -2. Replace everything with the contents of `firebase-rules.json` +2. Replace everything with the contents of [`firebase-rules.json`](firebase-rules.json) 3. Click **Publish** ### Step 8 — Test locally diff --git a/firebase-rules.json b/firebase-rules.json new file mode 100644 index 0000000..2be1a67 --- /dev/null +++ b/firebase-rules.json @@ -0,0 +1,49 @@ +{ + "rules": { + ".read": false, + ".write": false, + "session": { + ".read": "auth != null", + "students": { + "$student": { + ".write": "auth != null && (!newData.exists() || (newData.hasChildren(['joinedAt', 'date']) && newData.child('date').isString()))", + ".validate": "!newData.exists() || (newData.hasChildren(['joinedAt', 'date']) && newData.child('joinedAt').isNumber() && newData.child('date').isString())" + } + }, + "activePoll": { + ".write": "auth != null", + ".validate": "!newData.exists() || (newData.hasChildren(['id', 'question', 'options', 'duration', 'resultPolicy', 'correctPolicy', 'startedAt', 'ended', 'revealResults', 'revealCorrect']) && newData.child('id').isString() && newData.child('question').isString() && newData.child('options').hasChildren() && newData.child('duration').isNumber() && newData.child('duration').val() >= 1 && newData.child('duration').val() <= 3600 && newData.child('resultPolicy').isString() && (newData.child('resultPolicy').val() == 'on_submit' || newData.child('resultPolicy').val() == 'manual' || newData.child('resultPolicy').val() == 'never') && newData.child('correctPolicy').isString() && (newData.child('correctPolicy').val() == 'with_results' || newData.child('correctPolicy').val() == 'manual' || newData.child('correctPolicy').val() == 'never') && newData.child('startedAt').isNumber() && newData.child('ended').isBoolean() && newData.child('revealResults').isBoolean() && newData.child('revealCorrect').isBoolean() && (!newData.child('correctIndex').exists() || (newData.child('correctIndex').isNumber() && newData.child('correctIndex').val() >= 0 && (newData.child('correctIndex').val() % 1 == 0) && newData.child('options').child(newData.child('correctIndex').val()).exists())))", + "responses": { + "$student": { + ".validate": "newData.isNumber() && newData.val() >= 0 && (newData.val() % 1 == 0) && root.child('session/activePoll/options').child(newData.val()).exists()" + } + } + }, + "queue": { + ".write": "auth != null", + ".validate": "!newData.exists() || (newData.hasChildren(['setId', 'setName', 'currentIndex', 'totalPolls', 'sessionKey']) && newData.child('setId').isString() && newData.child('setName').isString() && newData.child('currentIndex').isNumber() && newData.child('totalPolls').isNumber() && newData.child('sessionKey').isString())" + } + }, + "sessionStudents": { + ".read": "auth != null", + "$entry": { + ".write": "auth != null", + ".validate": "!newData.exists() || (newData.hasChildren(['name', 'date', 'joinedAt']) && newData.child('name').isString() && newData.child('date').isString() && newData.child('joinedAt').isNumber())" + } + }, + "pollHistory": { + ".read": "auth != null", + "$poll": { + ".write": "auth != null", + ".validate": "!newData.exists() || (newData.hasChildren(['id', 'question', 'options', 'duration', 'resultPolicy', 'correctPolicy', 'startedAt']) && newData.child('id').isString() && newData.child('question').isString() && newData.child('options').hasChildren() && newData.child('duration').isNumber() && newData.child('resultPolicy').isString() && (newData.child('resultPolicy').val() == 'on_submit' || newData.child('resultPolicy').val() == 'manual' || newData.child('resultPolicy').val() == 'never') && newData.child('correctPolicy').isString() && (newData.child('correctPolicy').val() == 'with_results' || newData.child('correctPolicy').val() == 'manual' || newData.child('correctPolicy').val() == 'never') && newData.child('startedAt').isNumber() && (!newData.child('endedAt').exists() || newData.child('endedAt').isNumber()) && (!newData.child('correctIndex').exists() || (newData.child('correctIndex').isNumber() && newData.child('correctIndex').val() >= 0 && (newData.child('correctIndex').val() % 1 == 0) && newData.child('options').child(newData.child('correctIndex').val()).exists())))" + } + }, + "pollSets": { + ".read": "auth != null", + "$set": { + ".write": "auth != null", + ".validate": "!newData.exists() || (newData.hasChildren(['id', 'name', 'createdAt', 'defaults']) && newData.child('id').isString() && newData.child('name').isString() && newData.child('createdAt').isNumber())" + } + } + } +} diff --git a/firebase.json b/firebase.json new file mode 100644 index 0000000..18282ea --- /dev/null +++ b/firebase.json @@ -0,0 +1,5 @@ +{ + "database": { + "rules": "firebase-rules.json" + } +} diff --git a/package.json b/package.json index f02177d..1c4b756 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "scripts": { "dev": "vite", "build": "vite build", + "validate:rules:json": "node -e \"JSON.parse(require('fs').readFileSync('firebase-rules.json','utf8'))\"", + "validate:rules": "npx firebase-tools@15.19.0 emulators:exec --only database --project demo-classroom-polling \"node scripts/validateFirebaseRules.mjs\"", "preview": "vite preview", "deploy": "npm run build && npx gh-pages -d dist" }, diff --git a/scripts/validateFirebaseRules.mjs b/scripts/validateFirebaseRules.mjs new file mode 100644 index 0000000..1109c98 --- /dev/null +++ b/scripts/validateFirebaseRules.mjs @@ -0,0 +1,52 @@ +import http from 'node:http'; + +const host = process.env.FIREBASE_DATABASE_EMULATOR_HOST || '127.0.0.1:9000'; +const baseUrl = `http://${host}`; +const authOverride = JSON.stringify({ + uid: 'rules-smoke-test-user', + token: {}, +}); + +async function put(path, body) { + const url = new URL(`${baseUrl}${path}.json`); + url.searchParams.set('auth_variable_override', authOverride); + + const payload = JSON.stringify(body); + + const response = await new Promise((resolve, reject) => { + const request = http.request(url, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(payload), + }, + }, resolve); + + request.on('error', reject); + request.end(payload); + }); + + const text = await new Promise((resolve, reject) => { + let data = ''; + response.setEncoding('utf8'); + response.on('data', chunk => { data += chunk; }); + response.on('end', () => resolve(data)); + response.on('error', reject); + }); + + if (response.statusCode < 200 || response.statusCode >= 300) { + throw new Error(`${path} returned ${response.statusCode}: ${text}`); + } +} + +const joinedAt = { '.sv': 'timestamp' }; +const date = '2026-06-14'; + +await put('/session/students/Alice', { joinedAt, date }); +await put('/sessionStudents/2026-06-14_Alice', { + name: 'Alice', + date, + joinedAt, +}); + +console.log('Firebase rules smoke tests passed');