From 2cacaaff8f6131f3168914499646d357b851ef2d Mon Sep 17 00:00:00 2001 From: Nathan Vander Wilt Date: Thu, 26 Jan 2023 16:46:53 -0800 Subject: [PATCH] fix: avoid email delegation via GET request the email validation approval process is now split into two stages: a GET request with no side effects except to load a page that then auto-submits a POST request to actually continue the flow. this fixes the API to follow proper API semantics and thus starts addressing some of #333 and presumably fixes all of #348 --- packages/access-api/src/index.js | 5 +-- .../access-api/src/routes/validate-email.js | 15 ++++++++ packages/access-api/src/utils/html.js | 34 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/access-api/src/index.js b/packages/access-api/src/index.js index a69aca203..23c2e2df8 100644 --- a/packages/access-api/src/index.js +++ b/packages/access-api/src/index.js @@ -4,7 +4,7 @@ import { notFound } from '@web3-storage/worker-utils/response' import { Router } from '@web3-storage/worker-utils/router' import { postRaw } from './routes/raw.js' import { postRoot } from './routes/root.js' -import { validateEmail } from './routes/validate-email.js' +import { preValidateEmail, validateEmail } from './routes/validate-email.js' import { validateWS } from './routes/validate-ws.js' import { version } from './routes/version.js' import { getContext } from './utils/context.js' @@ -14,7 +14,8 @@ const r = new Router({ onNotFound: notFound }) r.add('options', '*', preflight) r.add('get', '/version', version) -r.add('get', '/validate-email', validateEmail) +r.add('get', '/validate-email', preValidateEmail) +r.add('post', '/validate-email', validateEmail) r.add('get', '/validate-ws', validateWS) r.add('post', '/', postRoot) r.add('post', '/raw', postRaw) diff --git a/packages/access-api/src/routes/validate-email.js b/packages/access-api/src/routes/validate-email.js index 3da06bce7..bb91b696f 100644 --- a/packages/access-api/src/routes/validate-email.js +++ b/packages/access-api/src/routes/validate-email.js @@ -6,8 +6,23 @@ import { HtmlResponse, ValidateEmail, ValidateEmailError, + PendingValidateEmail, } from '../utils/html.js' +/** + * @param {import('@web3-storage/worker-utils/router').ParsedRequest} req + * @param {import('../bindings.js').RouteContext} env + */ +export async function preValidateEmail(req, env) { + if (!req.query?.ucan) { + return new HtmlResponse( + + ) + } + + return new HtmlResponse() +} + /** * @param {import('@web3-storage/worker-utils/router').ParsedRequest} req * @param {import('../bindings.js').RouteContext} env diff --git a/packages/access-api/src/utils/html.js b/packages/access-api/src/utils/html.js index 9db97041b..56814b4b8 100644 --- a/packages/access-api/src/utils/html.js +++ b/packages/access-api/src/utils/html.js @@ -95,6 +95,40 @@ export class HtmlResponse extends Response { } } +/** + * + * @param {object} props + * @param {boolean} [props.autoApprove] + */ +export const PendingValidateEmail = ({ autoApprove }) => ( +
+ +
+

Validating Email

+
+ +
+ {autoApprove ? ( +