diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..ea5ab25 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,21 @@ +name: Instructor Portal security tests +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-22.04 + env: + MONGOMS_VERSION: 7.0.14 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm # uses the root package-lock.json + - name: Cache mongod binary + uses: actions/cache@v4 + with: + path: ~/.cache/mongodb-binaries + key: mongodb-memory-server-7.0.14-${{ runner.os }} + - run: npm ci # installs the server+client workspaces + - run: npm test --workspace server diff --git a/.gitignore b/.gitignore index 177447d..43be891 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # Node.js dependencies node_modules/ +.npm-cache/ +.cache/ # Build artifacts build/ @@ -26,4 +28,4 @@ Thumbs.db # Server-side specific ignores (if server is in a subfolder) server/node_modules/ -server/build/ \ No newline at end of file +server/build/ diff --git a/.vscode/setupCI.js b/.vscode/setupCI.js index e1ba46d..fc11cbf 100644 --- a/.vscode/setupCI.js +++ b/.vscode/setupCI.js @@ -9,7 +9,9 @@ on: [push, pull_request] jobs: test: - runs-on: ubuntu-22.04 # 22.04 has libssl1.1 for mongod 6.0.x + runs-on: ubuntu-22.04 + env: + MONGOMS_VERSION: 7.0.14 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 @@ -20,11 +22,11 @@ jobs: uses: actions/cache@v4 with: path: ~/.cache/mongodb-binaries - key: mongodb-memory-server-6.0.14-\${{ runner.os }} + key: mongodb-memory-server-7.0.14-\${{ runner.os }} - run: npm ci # installs the server+client workspaces - run: npm test --workspace server`; if (!fs.existsSync(workflowDir)) { fs.mkdirSync(workflowDir, { recursive: true }); } -fs.writeFileSync(filePath, ciContent, { encoding: 'utf8' }); \ No newline at end of file +fs.writeFileSync(filePath, ciContent, { encoding: 'utf8' }); diff --git a/.vscode/setupHelper.js b/.vscode/setupHelper.js index 97842a7..1afb5e4 100644 --- a/.vscode/setupHelper.js +++ b/.vscode/setupHelper.js @@ -78,7 +78,7 @@ export const SEED = [ export const SECRET_FIELDS = ['password', 'apiKey', 'privateEmail', 'privatePhone']; export async function startDb() { - mongod = await MongoMemoryServer.create(); + mongod = await MongoMemoryServer.create({ instance: { storageEngine: 'wiredTiger' } }); await mongoose.connect(mongod.getUri(), { autoIndex: true }); } @@ -94,4 +94,4 @@ export async function seed() { } `; -fs.writeFileSync(envHelperFilePath, envHelperContent, { encoding: 'utf8' }); \ No newline at end of file +fs.writeFileSync(envHelperFilePath, envHelperContent, { encoding: 'utf8' }); diff --git a/client/src/pages/InstructorDashboard.jsx b/client/src/pages/InstructorDashboard.jsx index 1dd0ded..139205d 100644 --- a/client/src/pages/InstructorDashboard.jsx +++ b/client/src/pages/InstructorDashboard.jsx @@ -2,10 +2,9 @@ import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { api } from '../api' -// Instructor Portal dashboard. Reads the naive session from localStorage. +// Instructor Portal dashboard. Reads the public profile from localStorage. // Shows the signed-in profile, the public directory, and a name search. -// The search box sends a plain string; students intercept the request -// (Postman / proxy) to inject MongoDB operators — see Flow.md. +// The search box sends a plain name string to the public search endpoint. export default function InstructorDashboard() { const nav = useNavigate() const [instructor, setInstructor] = useState(null) @@ -58,9 +57,6 @@ export default function InstructorDashboard() {
@{instructor.username} • {instructor.department} • role: {instructor.role}
- {instructor.apiKey && ( -
API key: {instructor.apiKey}
- )} diff --git a/client/src/pages/InstructorLogin.jsx b/client/src/pages/InstructorLogin.jsx index dbd2c55..2389c9b 100644 --- a/client/src/pages/InstructorLogin.jsx +++ b/client/src/pages/InstructorLogin.jsx @@ -5,8 +5,7 @@ import { api } from '../api' // Instructor Portal login. This portal is separate from the student JWT auth // (AuthContext) — it has its own simple session stored in localStorage. // -// The form sends plain strings (honest use). The backing endpoint is -// intentionally vulnerable to NoSQL injection; see Flow.md for the lab. +// The backing endpoint validates strings and verifies a bcrypt password hash. export default function InstructorLogin() { const nav = useNavigate() const [username, setUsername] = useState('') diff --git a/client/src/pages/ReviewForm.jsx b/client/src/pages/ReviewForm.jsx index bf85442..9d65e82 100644 --- a/client/src/pages/ReviewForm.jsx +++ b/client/src/pages/ReviewForm.jsx @@ -2,10 +2,6 @@ import { useEffect, useState } from 'react' import { useNavigate, useParams } from 'react-router-dom' import { api } from '../api' -// TODO: build the Write Review page — see README.md "Your task". -// This page is already routed at /reviews/new (write) and /reviews/:id (edit), -// and both routes are wrapped in . - const defaults = { courseCode: '', rating: 5, comment: '' } export default function ReviewForm() { @@ -13,33 +9,82 @@ export default function ReviewForm() { const { id } = useParams() const [form, setForm] = useState(defaults) const [error, setError] = useState('') + const [loading, setLoading] = useState(Boolean(id)) + const [saving, setSaving] = useState(false) + const [loadedId, setLoadedId] = useState(null) - // TODO (edit mode): when there is an `id`, load the review and fill the form. useEffect(() => { + let active = true + setError('') + setLoadedId(null) + setForm(defaults) + setLoading(Boolean(id)) if (!id) return - // TODO + + api.get('/reviews/' + id) + .then(({ data }) => { + if (!active) return + const { courseCode, rating, comment } = data.review + setForm({ courseCode, rating, comment: comment || '' }) + setLoadedId(id) + }) + .catch(err => { + if (active) setError(err?.response?.data?.message || 'Could not load review') + }) + .finally(() => { if (active) setLoading(false) }) + + return () => { active = false } }, [id]) - // TODO: update `form` when an input changes (rating should be a number). function onChange(e) { - // TODO + const { name, value } = e.target + setForm(prev => ({ ...prev, [name]: name === 'rating' ? Number(value) : value })) } - // TODO: POST a new review, or PATCH the existing one when editing, - // then go back to /reviews. Show the server's error message on failure. + const unavailable = loading || saving || Boolean(id && loadedId !== id) + async function onSubmit(e) { e.preventDefault() + if (unavailable) return setError('') - // TODO + setSaving(true) + try { + const payload = { ...form, courseCode: form.courseCode.trim().toUpperCase() } + if (id) await api.patch('/reviews/' + id, payload) + else await api.post('/reviews', payload) + nav('/reviews') + } catch (err) { + setError(err?.response?.data?.message || 'Could not save review') + } finally { + setSaving(false) + } } return (

{id ? 'Edit' : 'Write'} Review

+ {loading &&

Loading review...

}
- {/* TODO: course code input, rating select (1-5) and comment textarea */} - {error &&
{error}
} - +
+ + +