Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Instructor Portal security tests
on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-22.04 # 22.04 has libssl1.1 for mongod 6.0.x
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-6.0.14-${{ runner.os }}
- run: npm ci # installs the server+client workspaces
- run: npm test --workspace server
177 changes: 144 additions & 33 deletions client/src/pages/InstructorDashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { api } from '../api'

// Instructor Portal dashboard. Reads the naive session 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.
export default function InstructorDashboard() {
const nav = useNavigate()

const [instructor, setInstructor] = useState(null)
const [directory, setDirectory] = useState([])
const [term, setTerm] = useState('')
Expand All @@ -16,91 +14,204 @@ export default function InstructorDashboard() {

useEffect(() => {
const raw = localStorage.getItem('instructor')
if (!raw) {
const token = localStorage.getItem('instructorToken')

// Redirect if session information is missing
if (!raw || !token) {
localStorage.removeItem('instructor')
localStorage.removeItem('instructorToken')
nav('/portal', { replace: true })
return
}

try {
setInstructor(JSON.parse(raw))
const savedInstructor = JSON.parse(raw)

if (!savedInstructor || !savedInstructor.id) {
throw new Error('Invalid instructor session')
}

setInstructor(savedInstructor)

} catch {
localStorage.removeItem('instructor')
localStorage.removeItem('instructorToken')
nav('/portal', { replace: true })
return
}

// Load public instructor directory
api.get('/instructors')
.then(res => setDirectory(res.data.instructors))
.catch(() => {})
}, [])
.then(res => {
setDirectory(res.data.instructors || [])
})
.catch(() => {
setError('Failed to load instructor directory')
})

}, [nav])

async function onSearch(e) {
e.preventDefault()
setError('')
setResults(null)

if (!term.trim()) {
setError('Please enter an instructor name')
return
}

try {
const res = await api.get('/instructors/search', { params: { name: term } })
setResults(res.data.instructors)
const res = await api.get('/instructors/search', {
params: {
name: term.trim()
}
})

setResults(res.data.instructors || [])

} catch (err) {
setError(err?.response?.data?.message || 'Search failed')
setError(
err?.response?.data?.message ||
'Search failed'
)
}
}

function logout() {
localStorage.removeItem('instructor')
localStorage.removeItem('instructorToken')

nav('/portal', { replace: true })
}

if (!instructor) return null
if (!instructor) {
return null
}

return (
<div className="space-y-4">

{/* Instructor profile */}
<div className="card flex items-start justify-between">

<div>
<h1 className="text-xl font-semibold">Welcome, {instructor.name}</h1>
<h1 className="text-xl font-semibold">
Welcome, {instructor.name}
</h1>

<div className="text-sm text-zinc-600">
@{instructor.username} • {instructor.department} • role: <b>{instructor.role}</b>
@{instructor.username}
{' • '}
{instructor.department}
{' • '}
role: <b>{instructor.role}</b>
</div>
{instructor.apiKey && (
<div className="text-sm mt-1">API key: <code>{instructor.apiKey}</code></div>
)}
</div>
<button onClick={logout} className="btn">Log out</button>

<button
onClick={logout}
className="btn"
>
Log out
</button>

</div>

{/* Search instructors */}
<div className="card">
<h2 className="font-semibold mb-2">Find an instructor</h2>
<form onSubmit={onSearch} className="flex items-center gap-2">

<h2 className="font-semibold mb-2">
Find an instructor
</h2>

<form
onSubmit={onSearch}
className="flex items-center gap-2"
>

<input
className="input"
placeholder="Search by name (e.g. Smith)"
value={term}
onChange={e => setTerm(e.target.value)}
/>
<button className="btn" type="submit">Search</button>

<button className="btn" type="submit">
Search
</button>

</form>
{error && <div className="text-red-600 text-sm mt-2">{error}</div>}
{results && (

{error && (
<div className="text-red-600 text-sm mt-2">
{error}
</div>
)}

{results !== null && (
<div className="mt-3 space-y-2">
<div className="text-sm text-zinc-600">{results.length} result(s)</div>

<div className="text-sm text-zinc-600">
{results.length} result(s)
</div>

{results.map(r => (
<pre key={r._id} className="text-xs bg-zinc-100 rounded-lg p-2 overflow-x-auto">
{JSON.stringify(r, null, 2)}
</pre>
<div
key={r._id}
className="text-sm border border-zinc-200 rounded-lg p-3"
>
<div className="font-medium">
{r.name}
</div>

<div className="text-zinc-600">
{r.department} • {r.role}
</div>
</div>
))}

</div>
)}

</div>

{/* Public instructor directory */}
<div className="card">
<h2 className="font-semibold mb-2">Directory</h2>

<h2 className="font-semibold mb-2">
Directory
</h2>

<div className="grid grid-cols-1 md:grid-cols-2 gap-2">

{directory.map(d => (
<div key={d._id} className="text-sm border border-zinc-200 rounded-lg p-2">
<div className="font-medium">{d.name}</div>
<div className="text-zinc-600">{d.department} • {d.role}</div>
<div
key={d._id}
className="text-sm border border-zinc-200 rounded-lg p-2"
>

<div className="font-medium">
{d.name}
</div>

<div className="text-zinc-600">
{d.department} • {d.role}
</div>

</div>
))}
{directory.length === 0 && <div className="text-sm text-zinc-600">No instructors.</div>}

{directory.length === 0 && (
<div className="text-sm text-zinc-600">
No instructors.
</div>
)}

</div>

</div>

</div>
)
}
75 changes: 62 additions & 13 deletions client/src/pages/InstructorLogin.jsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,99 @@

import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
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.
export default function InstructorLogin() {
const nav = useNavigate()

const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)

async function handleSubmit(e) {
e.preventDefault()
setError('')
setLoading(true)

try {
const res = await api.post('/instructors/login', { username, password })
localStorage.setItem('instructor', JSON.stringify(res.data.instructor))
nav('/portal/dashboard')
const res = await api.post('/instructors/login', {
username,
password
})

const { token, instructor } = res.data

if (!token || !instructor) {
throw new Error('Invalid login response')
}

// Store instructor session
localStorage.setItem(
'instructor',
JSON.stringify(instructor)
)

localStorage.setItem('instructorToken', token)

// Redirect to dashboard
nav('/portal/dashboard', { replace: true })

} catch (err) {
setError(err?.response?.data?.message || 'Login failed')
setError(
err?.response?.data?.message ||
err?.message ||
'Login failed'
)
} finally {
setLoading(false)
}
}

return (
<div className="max-w-md mx-auto card">
<h1 className="text-xl font-semibold mb-1">Instructor Portal</h1>
<p className="text-sm text-zinc-600 mb-4">Staff sign-in</p>

<h1 className="text-xl font-semibold mb-1">
Instructor Portal
</h1>

<p className="text-sm text-zinc-600 mb-4">
Staff sign-in
</p>

<form onSubmit={handleSubmit} className="space-y-3">

<input
className="input"
placeholder="Username"
value={username}
onChange={e => setUsername(e.target.value)}
required
/>

<input
className="input"
type="password"
placeholder="Password"
value={password}
onChange={e => setPassword(e.target.value)}
required
/>
{error && <div className="text-red-600 text-sm">{error}</div>}
<button className="btn" type="submit">Sign in</button>

{error && (
<div className="text-red-600 text-sm">
{error}
</div>
)}

<button
className="btn"
type="submit"
disabled={loading}
>
{loading ? 'Signing in...' : 'Sign in'}
</button>

</form>
</div>
)
Expand Down
Loading