Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): stricter reg exp to redirect sockjs client path #2069

Merged
merged 6 commits into from Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions client-src/default/webpack.config.js
Expand Up @@ -18,11 +18,16 @@ module.exports = {
],
},
plugins: [
new webpack.NormalModuleReplacementPlugin(/\/clients\//, (resource) => {
resource.request = resource.request.replace(
/\/clients\//,
'/../clients/'
);
}),
new webpack.NormalModuleReplacementPlugin(
/^\.\/clients\/SockJSClient$/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need search way how we can check what module was loaded from our client-src - it is right way don't break other code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean to test that it is loading the correct module in the webpack compilation? Could we run this webpack config in a test, look at the webpack CLI output, and see if it contains the right path?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it can solved using NormalModuleReplacementPlugin as you use, just need more checks (need check context contains 'webpack-dev-server/client-src' and rewrite only in this case)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi I did something like this, except instead I matched '/client-src/default', because it is possible the dev server is not in a webpack-dev-server directory if someone clones it into a different named directory. If we want to be very strict about it, we could match against path.resolve to get the exact context we want, but I'm afraid that could result in some problem.

Also, I included backwards slash (\\) in my matching string because windows seems to use that for the context paths

(resource) => {
if (resource.context.startsWith(process.cwd())) {
resource.request = resource.request.replace(
/^\.\/clients\/SockJSClient$/,
'../clients/SockJSClient'
);
}
}
),
],
};
17 changes: 11 additions & 6 deletions client-src/live/webpack.config.js
Expand Up @@ -34,11 +34,16 @@ module.exports = {
to: path.resolve(__dirname, '../../client/live.html'),
},
]),
new webpack.NormalModuleReplacementPlugin(/\/clients\//, (resource) => {
resource.request = resource.request.replace(
/\/clients\//,
'/../clients/'
);
}),
new webpack.NormalModuleReplacementPlugin(
/^\.\/clients\/SockJSClient$/,
(resource) => {
if (resource.context.startsWith(process.cwd())) {
resource.request = resource.request.replace(
/^\.\/clients\/SockJSClient$/,
'../clients/SockJSClient'
);
}
}
),
],
};
83 changes: 83 additions & 0 deletions test/e2e/Iframe.test.js
@@ -0,0 +1,83 @@
'use strict';

const testServer = require('../helpers/test-server');
const config = require('../fixtures/client-config/webpack.config');
const runBrowser = require('../helpers/run-browser');
const port = require('../ports-map').Iframe;

// iframe mode should be tested while still supported, because
// its sources differ from those of inline mode, which can cause unexpected
// breaking changes: https://github.com/webpack/webpack-dev-server/issues/2006
describe('Client iframe console.log', () => {
const baseOptions = {
port,
host: '0.0.0.0',
};
const cases = [
{
title: 'hot disabled',
options: {
hot: false,
},
},
{
title: 'hot enabled',
options: {
hot: true,
},
},
{
title: 'liveReload disabled',
options: {
liveReload: false,
},
},
{
title: 'liveReload enabled',
options: {
liveReload: true,
},
},
{
title: 'clientLogLevel is silent',
options: {
clientLogLevel: 'silent',
},
},
];

for (const { title, options } of cases) {
it(title, () => {
const res = [];
const testOptions = Object.assign({}, baseOptions, options);

// TODO: use async/await when Node.js v6 support is dropped
return Promise.resolve()
.then(() => {
return new Promise((resolve) => {
testServer.startAwaitingCompilation(config, testOptions, resolve);
});
})
.then(runBrowser)
.then(({ page, browser }) => {
return new Promise((resolve) => {
page.goto(`http://localhost:${port}/webpack-dev-server/main`);
page.on('console', ({ _text }) => {
res.push(_text);
});
setTimeout(() => {
browser.close().then(() => {
expect(res).toMatchSnapshot();
resolve();
});
}, 3000);
});
})
.then(() => {
return new Promise((resolve) => {
testServer.close(resolve);
});
});
});
}
});
35 changes: 35 additions & 0 deletions test/e2e/__snapshots__/Iframe.test.js.snap
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Client iframe console.log clientLogLevel is silent 1`] = `
Array [
"Hey.",
]
`;

exports[`Client iframe console.log hot disabled 1`] = `
Array [
"Hey.",
"[WDS] Live Reloading enabled.",
]
`;

exports[`Client iframe console.log hot enabled 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[WDS] Hot Module Replacement enabled.",
"[WDS] Live Reloading enabled.",
]
`;

exports[`Client iframe console.log liveReload disabled 1`] = `
Array [
"Hey.",
]
`;

exports[`Client iframe console.log liveReload enabled 1`] = `
Array [
"Hey.",
]
`;
1 change: 1 addition & 0 deletions test/ports-map.js
Expand Up @@ -43,6 +43,7 @@ const portsList = {
Progress: 1,
'progress-option': 1,
'profile-option': 1,
Iframe: 1,
};

let startPort = 8079;
Expand Down