Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

Commit

Permalink
#5 add delete button with improved security
Browse files Browse the repository at this point in the history
  • Loading branch information
synox committed Dec 19, 2016
1 parent a8f8748 commit 8bcdc39
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ function delete_old_messages() {
$mailbox->expungeDeletedMails();
}

/**
* deletes emails by id and username. The username must match the id.
*
* @param $mailid internal id (integer)
* @param $username the matching username
*/
function delete_mail($mailid, $username) {
global $mailbox, $config;

// in order to avoid https://www.owasp.org/index.php/Top_10_2013-A4-Insecure_Direct_Object_References
// the $username must match the $mailid.
$name = clean_name($username);
if (strlen($name) === 0) {
error(400, 'invalid username');
}
$address = get_address($name, $config['mailHostname']);
$mail_ids = search_mails($address, $mailbox);

if (in_array($mailid, $mail_ids)) {
$mailbox->deleteMail($mailid);
$mailbox->expungeDeletedMails();
print(json_encode(array("success" => true)));
} else {
error(404, 'delete error: invalid username/mailid combination');
}


}


header('Content-type: application/json');

Expand All @@ -106,7 +135,10 @@ function delete_old_messages() {
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if (isset($_GET['username'])) {

if (isset($_GET['username']) && isset($_GET['delete_email_id'])) {
delete_mail($_GET['delete_email_id'], $_GET['username']);
} else if (isset($_GET['username'])) {
print_inbox($_GET['username']);
} else {
error(400, 'invalid action');
Expand Down
17 changes: 17 additions & 0 deletions src/client-libs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ app.controller('MailboxController', ["$scope", "$interval", "$http", "$log", fun
});
};

self.deleteMail = function (mailid) {
$http.get(backend_url, {params: {username: self.username, delete_email_id: mailid}})
.then(
function successCallback(response) {
self.updateMails();
},
function errorCallback(response) {
$log.error(response, this);
self.error = {
title: "HTTP_ERROR",
desc: "There is a problem with deleting the mail. (HTTP_ERROR).",
detail: response
};
});

};

// Initial load
self.updateMails()
}]);
8 changes: 7 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@

<section class="email">
<div class="row sticky-header" ec-stickyfill>
<div class="col-sm-12 email-summary">{{mail.subject}}</div>
<div class="col-sm-12 email-summary">{{mail.subject}}
<form class="form-inline float-xs-right">
<button ng-click="$ctrl.deleteMail(mail.id)" type="button"
class="btn btn-sm btn-outline-danger">Delete
</button>
</form>
</div>
</div>

<div class="row">
Expand Down

0 comments on commit 8bcdc39

Please sign in to comment.