🔊(y-provider) improve and add logs
We have somes entries with "No cookies", we add more logs to understand why we have this case. We add the datetime in front of each entries as well.
This commit is contained in:
@@ -15,10 +15,12 @@ jest.mock('../src/env', () => {
|
|||||||
COLLABORATION_SERVER_ORIGIN: origin,
|
COLLABORATION_SERVER_ORIGIN: origin,
|
||||||
COLLABORATION_SERVER_SECRET: 'test-secret-api-key',
|
COLLABORATION_SERVER_SECRET: 'test-secret-api-key',
|
||||||
COLLABORATION_BACKEND_BASE_URL: 'http://app-dev:8000',
|
COLLABORATION_BACKEND_BASE_URL: 'http://app-dev:8000',
|
||||||
|
COLLABORATION_LOGGING: 'true',
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
console.error = jest.fn();
|
console.error = jest.fn();
|
||||||
|
console.log = jest.fn();
|
||||||
|
|
||||||
const mockDocFetch = jest.fn();
|
const mockDocFetch = jest.fn();
|
||||||
jest.mock('@/api/getDoc', () => ({
|
jest.mock('@/api/getDoc', () => ({
|
||||||
@@ -109,7 +111,9 @@ describe('Server Tests', () => {
|
|||||||
quiet: true,
|
quiet: true,
|
||||||
preserveConnection: false,
|
preserveConnection: false,
|
||||||
onClose: (data) => {
|
onClose: (data) => {
|
||||||
expect(console.error).toHaveBeenCalledWith(
|
expect(console.log).toHaveBeenCalledWith(
|
||||||
|
expect.any(String),
|
||||||
|
' --- ',
|
||||||
'Invalid room name - Probable hacking attempt:',
|
'Invalid room name - Probable hacking attempt:',
|
||||||
providerName,
|
providerName,
|
||||||
room,
|
room,
|
||||||
@@ -145,7 +149,9 @@ describe('Server Tests', () => {
|
|||||||
quiet: true,
|
quiet: true,
|
||||||
preserveConnection: false,
|
preserveConnection: false,
|
||||||
onClose: (data) => {
|
onClose: (data) => {
|
||||||
expect(console.error).toHaveBeenCalledWith(
|
expect(console.log).toHaveBeenLastCalledWith(
|
||||||
|
expect.any(String),
|
||||||
|
' --- ',
|
||||||
'Room name is not a valid uuid:',
|
'Room name is not a valid uuid:',
|
||||||
room,
|
room,
|
||||||
);
|
);
|
||||||
@@ -180,7 +186,9 @@ describe('Server Tests', () => {
|
|||||||
quiet: true,
|
quiet: true,
|
||||||
preserveConnection: false,
|
preserveConnection: false,
|
||||||
onClose: (data) => {
|
onClose: (data) => {
|
||||||
expect(console.error).toHaveBeenCalledWith(
|
expect(console.log).toHaveBeenLastCalledWith(
|
||||||
|
expect.any(String),
|
||||||
|
' --- ',
|
||||||
'Room name is not a valid uuid:',
|
'Room name is not a valid uuid:',
|
||||||
room,
|
room,
|
||||||
);
|
);
|
||||||
@@ -218,7 +226,7 @@ describe('Server Tests', () => {
|
|||||||
quiet: true,
|
quiet: true,
|
||||||
preserveConnection: false,
|
preserveConnection: false,
|
||||||
onClose: (data) => {
|
onClose: (data) => {
|
||||||
expect(console.error).toHaveBeenCalledWith(
|
expect(console.error).toHaveBeenLastCalledWith(
|
||||||
'[onConnect]',
|
'[onConnect]',
|
||||||
'Backend error: Unauthorized',
|
'Backend error: Unauthorized',
|
||||||
);
|
);
|
||||||
@@ -261,7 +269,9 @@ describe('Server Tests', () => {
|
|||||||
quiet: true,
|
quiet: true,
|
||||||
preserveConnection: false,
|
preserveConnection: false,
|
||||||
onClose: (data) => {
|
onClose: (data) => {
|
||||||
expect(console.error).toHaveBeenCalledWith(
|
expect(console.log).toHaveBeenLastCalledWith(
|
||||||
|
expect.any(String),
|
||||||
|
' --- ',
|
||||||
'onConnect: Unauthorized to retrieve this document',
|
'onConnect: Unauthorized to retrieve this document',
|
||||||
room,
|
room,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ import { Request } from 'express';
|
|||||||
import * as ws from 'ws';
|
import * as ws from 'ws';
|
||||||
|
|
||||||
import { hocusPocusServer } from '@/servers/hocusPocusServer';
|
import { hocusPocusServer } from '@/servers/hocusPocusServer';
|
||||||
import { logger } from '@/utils';
|
|
||||||
|
|
||||||
export const collaborationWSHandler = (ws: ws.WebSocket, req: Request) => {
|
export const collaborationWSHandler = (ws: ws.WebSocket, req: Request) => {
|
||||||
logger('Incoming Origin:', req.headers['origin']);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
hocusPocusServer.handleConnection(ws, req);
|
hocusPocusServer.handleConnection(ws, req);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import {
|
|||||||
Y_PROVIDER_API_KEY,
|
Y_PROVIDER_API_KEY,
|
||||||
} from '@/env';
|
} from '@/env';
|
||||||
|
|
||||||
|
import { logger } from './utils';
|
||||||
|
|
||||||
const VALID_API_KEYS = [COLLABORATION_SERVER_SECRET, Y_PROVIDER_API_KEY];
|
const VALID_API_KEYS = [COLLABORATION_SERVER_SECRET, Y_PROVIDER_API_KEY];
|
||||||
const allowedOrigins = COLLABORATION_SERVER_ORIGIN.split(',');
|
const allowedOrigins = COLLABORATION_SERVER_ORIGIN.split(',');
|
||||||
|
|
||||||
@@ -42,14 +44,16 @@ export const wsSecurity = (
|
|||||||
const origin = req.headers['origin'];
|
const origin = req.headers['origin'];
|
||||||
if (!origin || !allowedOrigins.includes(origin)) {
|
if (!origin || !allowedOrigins.includes(origin)) {
|
||||||
ws.close(4001, 'Origin not allowed');
|
ws.close(4001, 'Origin not allowed');
|
||||||
console.error('CORS policy violation: Invalid Origin', origin);
|
logger('CORS policy violation: Invalid Origin', origin);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cookies = req.headers['cookie'];
|
const cookies = req.headers['cookie'];
|
||||||
if (!cookies) {
|
if (!cookies) {
|
||||||
ws.close(4001, 'No cookies');
|
ws.close(4001, 'No cookies');
|
||||||
console.error('CORS policy violation: No cookies');
|
logger('CORS policy violation: No cookies');
|
||||||
|
logger('UA:', req.headers['user-agent']);
|
||||||
|
logger('URL:', req.url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,21 +15,24 @@ export const hocusPocusServer = Server.configure({
|
|||||||
documentName,
|
documentName,
|
||||||
requestParameters,
|
requestParameters,
|
||||||
context,
|
context,
|
||||||
|
request,
|
||||||
}) {
|
}) {
|
||||||
const roomParam = requestParameters.get('room');
|
const roomParam = requestParameters.get('room');
|
||||||
|
|
||||||
if (documentName !== roomParam) {
|
if (documentName !== roomParam) {
|
||||||
console.error(
|
logger(
|
||||||
'Invalid room name - Probable hacking attempt:',
|
'Invalid room name - Probable hacking attempt:',
|
||||||
documentName,
|
documentName,
|
||||||
requestParameters.get('room'),
|
requestParameters.get('room'),
|
||||||
);
|
);
|
||||||
|
logger('UA:', request.headers['user-agent']);
|
||||||
|
logger('URL:', request.url);
|
||||||
|
|
||||||
return Promise.reject(new Error('Wrong room name: Unauthorized'));
|
return Promise.reject(new Error('Wrong room name: Unauthorized'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!uuidValidate(documentName) || uuidVersion(documentName) !== 4) {
|
if (!uuidValidate(documentName) || uuidVersion(documentName) !== 4) {
|
||||||
console.error('Room name is not a valid uuid:', documentName);
|
logger('Room name is not a valid uuid:', documentName);
|
||||||
|
|
||||||
return Promise.reject(new Error('Wrong room name: Unauthorized'));
|
return Promise.reject(new Error('Wrong room name: Unauthorized'));
|
||||||
}
|
}
|
||||||
@@ -40,7 +43,7 @@ export const hocusPocusServer = Server.configure({
|
|||||||
const document = await fetchDocument(documentName, requestHeaders);
|
const document = await fetchDocument(documentName, requestHeaders);
|
||||||
|
|
||||||
if (!document.abilities.retrieve) {
|
if (!document.abilities.retrieve) {
|
||||||
console.error(
|
logger(
|
||||||
'onConnect: Unauthorized to retrieve this document',
|
'onConnect: Unauthorized to retrieve this document',
|
||||||
documentName,
|
documentName,
|
||||||
);
|
);
|
||||||
@@ -50,7 +53,7 @@ export const hocusPocusServer = Server.configure({
|
|||||||
can_edit = document.abilities.update;
|
can_edit = document.abilities.update;
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
console.error('onConnect: backend error', error.message);
|
logger('onConnect: backend error', error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.reject(new Error('Backend error: Unauthorized'));
|
return Promise.reject(new Error('Backend error: Unauthorized'));
|
||||||
@@ -69,12 +72,10 @@ export const hocusPocusServer = Server.configure({
|
|||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
logger(
|
logger(
|
||||||
'Connection established:',
|
'Connection established on room:',
|
||||||
documentName,
|
documentName,
|
||||||
'canEdit:',
|
'canEdit:',
|
||||||
can_edit,
|
can_edit,
|
||||||
'room:',
|
|
||||||
requestParameters.get('room'),
|
|
||||||
);
|
);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { COLLABORATION_LOGGING } from './env';
|
|||||||
export function logger(...args: any[]) {
|
export function logger(...args: any[]) {
|
||||||
if (COLLABORATION_LOGGING === 'true') {
|
if (COLLABORATION_LOGGING === 'true') {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||||
console.log(...args);
|
console.log(new Date().toISOString(), ' --- ', ...args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user