THINK FIRST·CODE LATER

← All labs

Allow-list input validator

Problem

Validate StudyBuddy form fields on the server with allow-lists, and flag suspicious input for the security log.

Input: lines field value... until the end of input (the value is the rest of the line, possibly with spaces). Rules:

Field Valid if
courseCode 3 or 4 capital letters, one space, 4 digits (e.g. CPS 4301)
studentId exactly 7 digits
email letters, digits, ., _ or - (at least 1), then @wku.edu.cn or @kean.edu
displayName 2 to 30 characters, only letters, spaces, ' and -, and not only spaces
groupSize an integer from 2 to 6 (no sign, no leading zeros)

Output per line: field: ACCEPT or field: REJECT, and, for rejected values only, add [SUSPICIOUS] if the value contains any of: ', --, ;, <, > — except that a ' alone in a displayName is legal (e.g., O'Brien), so for displayName only --, ;, <, > count. An unknown field gives field: UNKNOWN FIELD.

At the end: Accepted: a, Rejected: r, Suspicious: s.

Input:

courseCode CPS 4301
courseCode cps4301
studentId 1234567
email yuki.tanaka@wku.edu.cn
email yuki@gmail.com
displayName Sean O'Brien
displayName <script>alert(1)</script>
groupSize 7

Output:

courseCode: ACCEPT
courseCode: REJECT
studentId: ACCEPT
email: ACCEPT
email: REJECT
displayName: ACCEPT
displayName: REJECT [SUSPICIOUS]
groupSize: REJECT
Accepted: 4, Rejected: 4, Suspicious: 1

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

s10-1-data-1.txt

courseCode CPS 4301
courseCode cps4301
studentId 1234567
email yuki.tanaka@wku.edu.cn
email yuki@gmail.com
displayName Sean O'Brien
displayName <script>alert(1)</script>
groupSize 7
What your program must print for s10-1-data-1.txt
courseCode: ACCEPT
courseCode: REJECT
studentId: ACCEPT
email: ACCEPT
email: REJECT
displayName: ACCEPT
displayName: REJECT [SUSPICIOUS]
groupSize: REJECT
Accepted: 4, Rejected: 4, Suspicious: 1

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < s10-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content s10-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.