forked from ShiftLeftSecurity/shiftleft-java-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminController.java
More file actions
118 lines (103 loc) · 4.03 KB
/
Copy pathAdminController.java
File metadata and controls
118 lines (103 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package io.shiftleft.controller;
import io.shiftleft.model.AuthToken;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Admin checks login
*/
@Controller
public class AdminController {
private String fail = "redirect:/";
// helper
private boolean isAdmin(String auth)
{
try {
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(auth));
ObjectInputStream objectInputStream = new ObjectInputStream(bis);
// Fixing object deserialization vulnerability by checking if the deserialized object is of the expected type
Object authToken = objectInputStream.readObject();
if(authToken instanceof AuthToken) {
return ((AuthToken) authToken).isAdmin();
} else {
throw new Exception("Unexpected object type");
}
} catch (Exception ex) {
System.out.println(" cookie cannot be deserialized: "+ex.getMessage());
return false;
}
}
// Set 'HttpOnly' and 'secure' flag for cookies
@RequestMapping(value = "/admin/printSecrets", method = RequestMethod.GET)
public String doGetPrintSecrets(@CookieValue(value = "auth", defaultValue = "notset") String auth, HttpServletResponse response, HttpServletRequest request) throws Exception {
if (request.getSession().getAttribute("auth") == null) {
return fail;
}
String authToken = request.getSession().getAttribute("auth").toString();
if(!isAdmin(authToken)) {
return fail;
}
ClassPathResource cpr = new ClassPathResource("static/calculations.csv");
try {
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
response.getOutputStream().println(new String(bdata, StandardCharsets.UTF_8));
return null;
} catch (IOException ex) {
ex.printStackTrace();
// redirect to /
return fail;
}
}
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
public String doPostLogin(@CookieValue(value = "auth", defaultValue = "notset") String auth, @RequestBody String password, HttpServletResponse response, HttpServletRequest request) throws Exception {
String succ = "redirect:/admin/printSecrets";
try {
if (!auth.equals("notset")) {
if(isAdmin(auth)) {
request.getSession().setAttribute("auth",auth);
return succ;
}
}
String[] pass = password.split("=");
if(pass.length!=2) {
return fail;
}
if(pass[1] != null && pass[1].length()>0 && pass[1].equals("shiftleftsecret"))
{
AuthToken authToken = new AuthToken(AuthToken.ADMIN);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(authToken);
String cookieValue = new String(Base64.getEncoder().encode(bos.toByteArray()));
// Set 'HttpOnly' and 'secure' flag for the cookie
response.addCookie(new Cookie("auth", cookieValue ).setHttpOnly(true).setSecure(true));
request.getSession().setAttribute("auth",cookieValue);
return succ;
}
return fail;
}
catch (Exception ex)
{
ex.printStackTrace();
return fail;
}
}
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public String doGetLogin(HttpServletResponse response, HttpServletRequest request) {
return "redirect:/";
}
}