Skip to content

Commit

Permalink
[keyserver] Convert urlInfo in website responders
Browse files Browse the repository at this point in the history
Summary:
[ENG-4670](https://linear.app/comm/issue/ENG-4670)

We also need to convert the urlInfo in the website responder so it works on link containing the new id schema. We can also handle the older links by not converting (the older link already contain schema that's inside the keyserver db).

Depends on D8879 (moving stuff because of cyclic dependencies)

Test Plan:
Open links:
- http://localhost:3000/comm/chat/thread/256%7C86139/ -> navigated to the `86139`
- http://localhost:3000/comm/chat/thread/86139/ -> navigated to the `86139` thread, url was changed to match id schema after loading

Try opening links to non-existant threads:
- http://localhost:3000/comm/chat/thread/256%7C861392/ -> navigated to the thread at the top of the thread list
- http://localhost:3000/comm/chat/thread/861392/ -> navigated to the thread at the top of the thread list

Try navigating to settings and check if it worked correctly: http://localhost:3000/comm/settings/account/

Tried navigating to a pending (non-sidebar) threads with two users and was navigated correctly. Navigating to sidebar pending threads doesn't work but from analyzing the code and diffs it isn't supported by the keyserver anyway.

Also opened landing to check if there are no weird cyclic dependecies errors there

Reviewers: tomek, inka, kamil, ginsu

Reviewed By: kamil

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D8880
  • Loading branch information
MichalGniadek committed Aug 22, 2023
1 parent feb5be9 commit ef10c8f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
15 changes: 12 additions & 3 deletions keyserver/src/responders/website-responders.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { currentDateInTimeZone } from 'lib/utils/date-utils.js';
import { ServerError } from 'lib/utils/errors.js';
import { promiseAll } from 'lib/utils/promises.js';
import { defaultNotifPermissionAlertInfo } from 'lib/utils/push-alerts.js';
import { infoFromURL } from 'lib/utils/url-utils.js';
import { infoFromURL, urlInfoValidator } from 'lib/utils/url-utils.js';
import {
tBool,
tNumber,
Expand Down Expand Up @@ -77,7 +77,7 @@ import {
getAppURLFactsFromRequestURL,
getCommAppURLFacts,
} from '../utils/urls.js';
import { validateOutput } from '../utils/validation-utils.js';
import { validateOutput, validateInput } from '../utils/validation-utils.js';

const { renderToNodeStream } = ReactDOMServer;

Expand Down Expand Up @@ -274,7 +274,16 @@ async function websiteResponder(

const initialNavInfoPromise = (async () => {
try {
const urlInfo = infoFromURL(req.url);
let urlInfo = infoFromURL(decodeURI(req.url));

try {
urlInfo = await validateInput(viewer, urlInfoValidator, urlInfo, true);
} catch (exc) {
// We should still be able to handle older links
if (exc.message !== 'invalid_client_id_prefix') {
throw exc;
}
}

let backupInfo = {
now: currentDateInTimeZone(viewer.timeZone),
Expand Down
6 changes: 4 additions & 2 deletions keyserver/src/utils/validation-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ async function validateInput<T>(
viewer: Viewer,
inputValidator: TType<T>,
input: mixed,
ignoreViewerVersion?: boolean,
): Promise<T> {
if (!viewer.isSocket) {
if (!ignoreViewerVersion && !viewer.isSocket) {
await checkClientSupported(viewer, inputValidator, input);
}
const convertedInput = checkInputValidator(inputValidator, input);
Expand All @@ -41,7 +42,8 @@ async function validateInput<T>(
hasMinStateVersion(viewer.platformDetails, {
native: 43,
web: 3,
})
}) ||
ignoreViewerVersion
) {
try {
return convertClientIDsToServerIDs(
Expand Down
18 changes: 17 additions & 1 deletion lib/utils/url-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow

import { idSchemaRegex } from './validation-utils.js';
import t, { type TInterface } from 'tcomb';

import { idSchemaRegex, tID, tShape } from './validation-utils.js';
import { pendingThreadIDRegex } from '../shared/thread-utils.js';

export type URLInfo = {
Expand All @@ -18,6 +20,20 @@ export type URLInfo = {
...
};

export const urlInfoValidator: TInterface<URLInfo> = tShape<URLInfo>({
year: t.maybe(t.Number),
month: t.maybe(t.Number),
verify: t.maybe(t.String),
calendar: t.maybe(t.Boolean),
chat: t.maybe(t.Boolean),
thread: t.maybe(tID),
settings: t.maybe(t.enums.of(['account', 'danger-zone'])),
threadCreation: t.maybe(t.Boolean),
selectedUserList: t.maybe(t.list(t.String)),
inviteSecret: t.maybe(t.String),
qrCode: t.maybe(t.Boolean),
});

// We use groups to capture parts of the URL and any changes
// to regexes must be reflected in infoFromURL.
const yearRegex = new RegExp('(/|^)year/([0-9]+)(/|$)', 'i');
Expand Down

0 comments on commit ef10c8f

Please sign in to comment.