Web Security Programming I
Web Security Programming I
Except where otherwise noted all portions of this work are Copyright (c) 2007 Google
and are licensed under the Creative Commons Attribution 3.0 License
http://creativecommons.org/licenses/by/3.0/
A Simple Web Server
To illustrate what can go wrong if we do not
design for security in our web applications from
the start, consider a simple web server
implemented in Java.
command = st.nextToken();
pathname = st.nextToken();
if (command.equals("GET")) {
/* if the request is a GET
try to respond with the file
the user is requesting */
serveFile (osw,pathname);
}
else {
/* if the request is a NOT a GET,
return an error saying this server
does not implement the requested command */
osw.write ("HTTP/1.0 501 Not Implemented\n\n");
}
return;
}
SimpleWebServer:
serveFile 3
/* if the requested file can be
successfully opened and read, then
return an OK response code and send
the contents of the file */
osw.write ("HTTP/1.0 200 OK\n\n");
while (c != -1) {
sb.append((char)c);
c = fr.read();
}
Can you identify any security vulnerabilities
in SimpleWebServer?
What Can Go Wrong?
command = st.nextToken();
pathname = st.nextToken();
DoS on SimpleWebServer?
command = st.nextToken();
pathname = st.nextToken();
A possible solution
/* read the HTTP request from the client */
String request = br.readLine();
String command = null;
String pathname = null;
try {
/* parse the HTTP request */
StringTokenizer st =
new StringTokenizer (request, " ");
command = st.nextToken();
pathname = st.nextToken();
} catch (Exception e) {
osw.write (“HTTP/1.0 400 Bad Request\n\n”);
osw.close();
return;
}
Importance of “Careful”
Exception Handling
1) ERROR_ACCESS_DENIED
ERROR_PASS_FILE_NOT_FOUND
ERROR_OUT_OF_MEMORY
NO_ERROR_ACCESS_ALLOWED
2) NO_ERROR
ERROR
int getError ()
• http://www.learnsecurity.com/ntk