webgme / webgme-taxonomy

A design studio for creating taxonomies which can then be used to tag various resources.
MIT License
0 stars 1 forks source link

websocket.js, exception split() on undefined while running PlayWright test #307

Open dax-westerman opened 1 year ago

dax-westerman commented 1 year ago

Found in branch branch: 304-Data-dashboard-e2e-tests

Context

Steps to recreate:

1) Run PlatWright test, "Create new repo (and it shows up)." 2) Results in error

stack: [ "TypeError: Cannot read properties of undefined (reading 'split')", ' at parseCookie (C:\_\wl\webgme_projects\webgme-taxonomy\nodemodules\webgme-engine\src\server\storage\websocket.js:888:22)', ' at Socket. (C:\\wl\webgme_projects\webgme-taxonomy\nodemodules\webgme-engine\src\server\storage\websocket.js:893:33)', ' at Socket.emit (node:events:513:28)', ' at C:\\wl\webgme_projects\webgme-taxonomy\node_modules\socket.io\lib\socket.js:531:14', ' at process.processTicksAndRejections (node:internal/process/task_queues:77:11)' ] 7-14-2023-DMW-webgme-creating-project-through-playwright.log

dax-westerman commented 1 year ago

I ended up coding a spot-fix in

node_modules\webgme-engine\src\server\storage\websocket.js

in order to resolve the issue, since the call to parse the cookie was occurring before the check to see if the authentication was enabled.

// Worker commands
socket.on('simpleRequest', function (data, callback) {
    //TODO: parser needs to be moved to common place

    const parseCookie = str => str
        .split(';')
        .map(v => v.split('='))
        .reduce((acc, v) => {
            acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
            return acc;}, {});
    const cookies = parseCookie(this.handshake.headers.cookie);

    getUserIdFromToken(socket, data && data.webgmeToken)
        .then(function (userId) {
            data.userId = userId;
            data.socketId = socket.id;

            if (gmeConfig.authentication.enable === true) {
                return gmeAuth.regenerateJWToken(data.webgmeToken);
            }
        })
        .then(function (newToken) {
            data.webgmeToken = newToken;
            //TODO this should probably come from authenticator and not the request!!!
            if (gmeConfig.authentication.enable === true &&
                gmeConfig.authentication.azureActiveDirectory.enable === true) {
                    data.aadToken = cookies[gmeConfig.authentication.azureActiveDirectory.cookieId] || null;
                }
            ...

Encapsulated parseCookie lambda as a closure to use in latter scope.

// Worker commands
socket.on('simpleRequest', function (data, callback) {
    //TODO: parser needs to be moved to common place
    const parseCookie = str => str
        .split(';')
        .map(v => v.split('='))
        .reduce((acc, v) => {
            acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
            return acc;}, {});

    const cookieRef = this.handshake.headers.cookie;
    const cookies = () => parseCookie(cookieRef);

    getUserIdFromToken(socket, data && data.webgmeToken)
        .then(function (userId) {
            data.userId = userId;
            data.socketId = socket.id;

            if (gmeConfig.authentication.enable === true) {
                return gmeAuth.regenerateJWToken(data.webgmeToken);
            }
        })
        .then(function (newToken) {
            data.webgmeToken = newToken;
            //TODO this should probably come from authenticator and not the request!!!
            if (gmeConfig.authentication.enable === true &&
                gmeConfig.authentication.azureActiveDirectory.enable === true) {
                    data.aadToken = cookies()[gmeConfig.authentication.azureActiveDirectory.cookieId] || null;
                }
    ...
dax-westerman commented 1 year ago

Assigning, since I'm guessing it's worth knowing where the issue is occurring, since it looks like quick fix, and my code is locally changed.