-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-account-profile.php
executable file
·317 lines (282 loc) · 10.5 KB
/
edit-account-profile.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
require_once (__SITE_ROOT__.'/classes/CSRFTokenHandler.php');
$lCSRFTokenHandler = new CSRFTokenHandler($_SESSION["security-level"], "edit-account-profile");
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
// DO NOTHING: This is equivalent to using client side security
$lEnableJavaScriptValidation = FALSE;
$lEnableHTMLControls = FALSE;
$lProtectAgainstMethodTampering = FALSE;
$lProtectAgainstIDOR = FALSE;
$lProtectAgainstPasswordLeakage = FALSE;
$lEncodeOutput = FALSE;
break;
case "1": // This code is insecure
// DO NOTHING: This is equivalent to using client side security
$lEnableJavaScriptValidation = TRUE;
$lEnableHTMLControls = TRUE;
$lProtectAgainstMethodTampering = FALSE;
$lProtectAgainstIDOR = FALSE;
$lProtectAgainstPasswordLeakage = FALSE;
$lEncodeOutput = FALSE;
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
/*
* Concerning SQL Injection, use parameterized stored procedures. Parameterized
* queries is not good enough. You cannot use least privilege with queries.
*/
$lEnableJavaScriptValidation = TRUE;
$lEnableHTMLControls = TRUE;
$lProtectAgainstMethodTampering = TRUE;
$lProtectAgainstIDOR = TRUE;
$lProtectAgainstPasswordLeakage = TRUE;
$lEncodeOutput = TRUE;
break;
}// end switch
$lNewCSRFTokenForNextRequest = $lCSRFTokenHandler->generateCSRFToken();
$lFormSubmitted = isset($_REQUEST["edit-account-profile-php-submit-button"]);
?>
<div class="page-title">Edit Profile</div>
<?php include_once (__SITE_ROOT__.'/includes/back-button.inc');?>
<?php include_once (__SITE_ROOT__.'/includes/hints/hints-menu-wrapper.inc'); ?>
<?php
if ($lFormSubmitted){
try {
$lValidationFailed = false;
if ($lProtectAgainstMethodTampering) {
$lUsername = $_POST["username"];
$lPassword = $_POST["password"];
$lConfirmedPassword = $_POST["confirm_password"];
$lUserSignature = $_POST["my_signature"];
$lPostedCSRFToken = $_POST['csrf-token'];
}else{
$lUsername = $_REQUEST["username"];
$lPassword = $_REQUEST["password"];
$lConfirmedPassword = $_REQUEST["confirm_password"];
$lUserSignature = $_REQUEST["my_signature"];
$lPostedCSRFToken = $_REQUEST['csrf-token'];
}//end if
if ($lEncodeOutput){
$lUsernameText = $Encoder->encodeForHTML($lUsername);
}else{
//allow XSS by not encoding
$lUsernameText = $lUsername;
}//end if
$LogHandler->writeToLog("Attempting to add account for: " . $lUsername);
if (!$lCSRFTokenHandler->validateCSRFToken($lPostedCSRFToken)){
throw (new Exception("Security Violation: Cross Site Request Forgery attempt detected.", 500));
}// end if
if (strlen($lUsername) == 0) {
$lValidationFailed = TRUE;
echo '<h2 class="error-message">Username cannot be blank</h2>';
}// end if
if ($lPassword != $lConfirmedPassword ) {
$lValidationFailed = TRUE;
echo '<h2 class="error-message">Passwords do not match</h2>';
}// end if
if (!$lValidationFailed){
$lRowsAffected = $SQLQueryHandler->updateUserAccount($lUsername, $lPassword, $lUserSignature);
echo '<div class="success-message">Profile updated for ' . $lUsernameText . '</div>';
$LogHandler->writeToLog("Profile updated for: " . $lUsername);
}// end if (!$lValidationFailed)
} catch (Exception $e) {
echo $CustomErrorHandler->FormatError($e, "Failed to add account");
$LogHandler->writeToLog("Failed to update profile for: " . $lUsername);
}// end try
}// end if $lFormSubmitted
if($lProtectAgainstIDOR){
if(isset($_SESSION['uid'])){
$lUserUID = $_SESSION['uid'];
}else{
$lUserUID = NULL;
} // if isset
}else{
if(isset($_REQUEST['uid'])){
$lUserUID = $_REQUEST['uid'];
}else{
if(isset($_COOKIE['uid'])){
$lUserUID = $_COOKIE['uid'];
}else{
$lUserUID = NULL;
} // if isset
} // if isset
} // $lProtectAgainstIDOR
$lUserLoggedIn = !(is_null($lUserUID));
$lUsername = "";
$lPassword = "";
$lSignature = "";
$lResultsFound = FALSE;
if($lUserLoggedIn){
try {
$lQueryResult = $SQLQueryHandler->getUserAccountByID($lUserUID);
$LogHandler->writeToLog("Got account with UID : " . $lUserUID);
if (isset($lQueryResult->num_rows)){
if ($lQueryResult->num_rows > 0) {
$lResultsFound = TRUE;
}//end if
}//end if
if($lResultsFound){
$row = $lQueryResult->fetch_object();
if(!$lEncodeOutput){
$lUsername = $row->username;
if (!$lProtectAgainstPasswordLeakage){
$lPassword = $row->password;
}
$lSignature = $row->mysignature;
}else{
$lUsername = $Encoder->encodeForHTML($row->username);
if (!$lProtectAgainstPasswordLeakage){
$lPassword = $Encoder->encodeForHTML($row->password);
}
$lSignature = $Encoder->encodeForHTML($row->mysignature);
}// end if
}
} catch (Exception $e) {
echo $CustomErrorHandler->FormatError($e, "Failed to get account");
$LogHandler->writeToLog("Failed to get account with UID : " . $lUserUID);
}// end try
} // if $lUserLoggedIn
?>
<script type="text/javascript">
<!--
<?php
if($lEnableJavaScriptValidation){
echo "var lValidateInput = \"TRUE\"" . PHP_EOL;
}else{
echo "var lValidateInput = \"FALSE\"" . PHP_EOL;
}// end if
?>
function onSubmitOfForm(/*HTMLFormElement*/ theForm){
try{
if(lValidateInput == "TRUE"){
var lUnsafeCharacters = /[`~!@#$%^&*()-_=+\[\]{}\\|;':",./<>?]/;
if (theForm.username.value.length > 15 ||
theForm.password.value.length > 15){
alert('Username too long. We dont want to allow too many characters.\n\nSomeone might have enough room to enter a hack attempt.');
return false;
};// end if
if (theForm.username.value.search(lUnsafeCharacters) > -1 ||
theForm.password.value.search(lUnsafeCharacters) > -1){
alert('Dangerous characters detected. We can\'t allow these. This all powerful blacklist will stop such attempts.\n\nMuch like padlocks, filtering cannot be defeated.\n\nBlacklisting is l33t like l33tspeak.');
return false;
};// end if
};// end if(lValidateInput)
return true;
}catch(e){
alert("Error: " + e.message);
};// end catch
};// end function onSubmitOfLoginForm(/*HTMLFormElement*/ theForm)
//-->
</script>
<span>
<a style="text-decoration: none; cursor: pointer;" href="./webservices/rest/ws-user-account.php">
<img style="vertical-align: middle;" src="./images/ajax_logo-75-79.jpg" height="75px" width="78px" />
<span style="font-weight:bold;">Switch to RESTful Web Service Version of this Page</span>
</a>
</span>
<div id="id-edit-account-profile-form-div" style="display: hidden;">
<form action="index.php?page=edit-account-profile.php" method="post" enctype="application/x-www-form-urlencoded"
onsubmit="return onSubmitOfForm(this);"
>
<input name="csrf-token" type="hidden" value="<?php echo $lNewCSRFTokenForNextRequest; ?>" />
<table>
<tr><td> </td></tr>
<tr>
<td colspan="2" class="form-header">Please choose your username, password and signature</td>
</tr>
<tr><td> </td></tr>
<tr>
<td class="label">Username</td>
<td>
<input type="text" name="username" size="15" autofocus="autofocus"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="15" required="required"');
}// end if
echo('value="' . $lUsername . '"');
?>
/>
</td>
</tr>
<tr>
<td class="label">Password</td>
<td>
<input type="password" name="password" size="15"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="15" required="required"');
}// end if
echo('value="' . $lPassword . '"');
?>
/>
<a href="index.php?page=password-generator.php&username=<?php echo $logged_in_user ?>" target="_blank">Password Generator</a>
</td>
</tr>
<tr>
<td class="label">Confirm Password</td>
<td>
<input type="password" name="confirm_password" size="15"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="15" required="required"');
}// end if
echo('value="' . $lPassword . '"');
?>
/>
</td>
</tr>
<tr>
<td class="label">Signature</td>
<td>
<textarea rows="3" cols="50" name="my_signature"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="100" required="required"');
}// end if
?>
><?php echo $lSignature; ?></textarea>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td colspan="2" style="text-align:center;">
<input name="edit-account-profile-php-submit-button" class="button" type="submit" value="Update Profile" />
</td>
</tr>
<tr><td> </td></tr>
</table>
</form>
</div>
<div id="id-profile-not-found-div" style="text-align: center; display: none;">
<table>
<tr>
<td class="label">User profile not found. You may <a href="index.php?page=login.php">login here</a></td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr>
<td style="text-align:center; font-style: italic;">
Dont have an account? <a href="index.php?page=register.php">Please register here</a>
</td>
</tr>
</table>
</div>
<script>
var lResultsFound = <?php echo $lResultsFound?"true":"false"; ?>;
if (lResultsFound){
document.getElementById("id-edit-account-profile-form-div").style.display="";
document.getElementById("id-profile-not-found-div").style.display="none";
}else{
document.getElementById("id-edit-account-profile-form-div").style.display="none";
document.getElementById("id-profile-not-found-div").style.display="";
}// end if lResultsFound
</script>
<?php
if ($lFormSubmitted) {
echo $lCSRFTokenHandler->generateCSRFHTMLReport();
}// end if
?>