Skip to content

Commit

Permalink
fix: avoid email delegation via GET request
Browse files Browse the repository at this point in the history
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 storacha#333 and presumably fixes all of storacha#348
  • Loading branch information
natevw committed Jan 27, 2023
1 parent a1d5ecf commit 02c40bf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/access-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions packages/access-api/src/routes/validate-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,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(
<ValidateEmailError msg={'Missing delegation in the URL.'} />
)
}

return new HtmlResponse(<PendingValidateEmail autoApprove={true} />)
}

/**
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} req
* @param {import('../bindings.js').RouteContext} env
Expand Down
34 changes: 34 additions & 0 deletions packages/access-api/src/utils/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ export class HtmlResponse extends Response {
}
}

/**
*
* @param {object} props
* @param {boolean} [props.autoApprove]
*/
export const PendingValidateEmail = ({ autoApprove }) => (
<div class="fcenter">
<img
src="https://web3.storage/android-chrome-512x512.png"
height="80"
width="80"
/>
<div>
<h1>Validating Email</h1>
<form id="approval" method="post" class="fcenter">
<button class="mcenter">Approve</button>
</form>
{autoApprove ? (
<script
dangerouslySetInnerHTML={{
// NOTE: this script sticks to ES3-era syntax for compat with more browsers
__html: `(function () {
// auto-submit the form for any user w/JS enabled
var form = document.getElementById('approval');
form.style.display = 'none';
form.submit();
})();`,
}}
/>
) : undefined}
</div>
</div>
)

/**
*
* @param {object} param0
Expand Down

0 comments on commit 02c40bf

Please sign in to comment.