Skip to content

Commit

Permalink
Handle Watchdog callback requests. (mozilla-services#4378, mozilla-se…
Browse files Browse the repository at this point in the history
  • Loading branch information
chenba committed Jun 18, 2018
1 parent bcade0e commit 7a6cb5c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/src/middleware/csrf.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function isAuthPath(path) {
function isCsrfExemptPath(path) {
return isAuthPath(path)
|| path.startsWith("/data")
|| path.startsWith("/watchdog")
|| path === "/event"
|| path === "/timing"
|| path === "/error"
Expand Down
5 changes: 5 additions & 0 deletions server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,11 @@ app.get("/api/fxa-oauth/confirm-login", function(req, res, next) {
}).catch(next);
});

app.post("/watchdog/:submissionId", function(req, res) {
Watchdog.handleResult(req);
res.end();
});

app.use((req, res, next) => {
genUuid.generate(genUuid.V_RANDOM, function(err, uuid) {
if (!err) {
Expand Down
53 changes: 53 additions & 0 deletions server/src/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ function getSubmissionId() {
});
}

function getSubmission(id) {
return db.select(
"SELECT * FROM watchdog_submissions WHERE id = $1", [id]
).then(rows => {
if (rows.length) {
return rows[0];
}
throw new Error(`Unable to find a Watchdog submission with id ${id}.`);
});
}

const credentials = {
id: config.watchdog.id,
key: config.watchdog.key,
Expand Down Expand Up @@ -138,3 +149,45 @@ exports.submit = function(shot) {
mozlog.error("watchdog-failed-submission", {err: e});
});
};

exports.handleResult = function(req) {
getSubmission(req.params.submissionId).then(record => {
if (record.request_id !== req.body.watchdog_id) {
throw new Error(`Received mismatching Watchdog ID in callback for submission ${record.id}.`);
}
if (req.query.nonce !== record.nonce) {
throw new Error(`Received mismatching nonce in callback for submission ${record.id}.`);
}
if (record.positive_result !== null) {
throw new Error(`Watchdog submission ${record.id} already has a result.`);
}

if (req.body.positive === false) {
handleNegative(record);
} else {
handlePositive(record);
}
}).catch(e => {
mozlog.error("watchdog-failed-callback", {err: e});
});
};

function handleNegative(record) {
return db.update("UPDATE watchdog_submissions SET positive_result = FALSE WHERE id = $1", [record.id]);
}

function handlePositive(record) {
return db.transaction(client => {
return db.queryWithClient(
client,
`UPDATE data
SET expire_time = NULL, deleted = FALSE, block_type = 'watchdog'
WHERE id = $1`,
[record.shot_id]
).then(() => db.queryWithClient(
client,
"UPDATE watchdog_submissions SET positive_result = TRUE WHERE id = $1",
[record.id]
));
});
}

0 comments on commit 7a6cb5c

Please sign in to comment.