Page MenuHomePhabricator

D6022.diff
No OneTemporary

D6022.diff

diff --git a/keyserver/src/responders/landing-handler.js b/keyserver/src/responders/landing-handler.js
--- a/keyserver/src/responders/landing-handler.js
+++ b/keyserver/src/responders/landing-handler.js
@@ -7,6 +7,8 @@
import ReactDOMServer from 'react-dom/server';
import { promisify } from 'util';
+import { isValidSIWENonce } from 'lib/utils/siwe-utils.js';
+
import { type LandingSSRProps } from '../landing/landing-ssr.react';
import { waitForStream } from '../utils/json-stream';
import { getAndAssertLandingURLFacts } from '../utils/urls';
@@ -104,6 +106,16 @@
async function landingResponder(req: $Request, res: $Response) {
const siweNonce = req.header('siwe-nonce');
+ if (
+ siweNonce !== null &&
+ siweNonce !== undefined &&
+ !isValidSIWENonce(siweNonce)
+ ) {
+ res.status(400).send({
+ message: 'Invalid nonce in siwe-nonce header.',
+ });
+ return;
+ }
const [{ jsURL, fontURLs, cssInclude }, LandingSSR] = await Promise.all([
getAssetInfo(),
getWebpackCompiledRootComponentForSSR(),
diff --git a/lib/utils/siwe-utils.js b/lib/utils/siwe-utils.js
new file mode 100644
--- /dev/null
+++ b/lib/utils/siwe-utils.js
@@ -0,0 +1,8 @@
+// @flow
+
+const siweNonceRegex: RegExp = /^[a-zA-Z0-9]{17}$/;
+function isValidSIWENonce(candidate: string): boolean {
+ return siweNonceRegex.test(candidate);
+}
+
+export { isValidSIWENonce };
diff --git a/lib/utils/siwe-utils.test.js b/lib/utils/siwe-utils.test.js
new file mode 100644
--- /dev/null
+++ b/lib/utils/siwe-utils.test.js
@@ -0,0 +1,78 @@
+// @flow
+
+import { isValidSIWENonce } from './siwe-utils.js';
+
+describe('SIWE Nonce utils', () => {
+ it('isValidSIWENonce should match valid nonces', () => {
+ // Following valid nonces generated using `siwe/generateNonce()` function
+ const validNonces = [
+ '1VxXbANmmHLFP4eks',
+ '2MmJuAAseMtLv5mCn',
+ '2USPurs7uBwua8S8x',
+ '3Qk22CmMo65LpaG9c',
+ '4QXu7RFVlXreNzQrK',
+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ValidSIWENonce(nonce)).toBe(true);
+ });
+ });
+
+ it('isValidSIWENonce should fail if nonce is wrong length', () => {
+ const shortNonce = '1VxXbANmmHLFP4ek';
+ expect(isValidSIWENonce(shortNonce)).toBe(false);
+
+ const longNonce = '1VxXbANmmHLFP4eks1';
+ expect(isValidSIWENonce(longNonce)).toBe(false);
+ });
+
+ it('isValidSIWENonce should fail if nonce has invalid characters', () => {
+ const invalidNonce = '1VxXbANmmHLFP4ek!';
+ expect(isValidSIWENonce(invalidNonce)).toBe(false);
+ });
+});

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 21, 7:47 AM (18 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2687012
Default Alt Text
D6022.diff (3 KB)

Event Timeline