Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ program
'-cu, --channelUrl [channelUrl]',
'Set a custom domain url to bypass dns/proxy protection',
)
.option('-p --port [portnumber]', 'Set port number to listen dev server');
.option('-p --port [portnumber]', 'Set port number to listen dev server')
.option(
'--serverHost [serverHost]',
'Set the hostname for the dev server (default: localhost)',
'localhost',
);
const cliOptions = prepareCommand(program);
const options = {
open: cliOptions.open,
Expand All @@ -35,6 +40,7 @@ const options = {
cache: cliOptions.cache,
channelUrl: cliOptions.channelUrl,
port: cliOptions.port,
host: cliOptions.serverHost,
};

async function run() {
Expand Down
4 changes: 3 additions & 1 deletion lib/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class StencilStart {
themePath: this._themeConfigManager.themePath,
stencilCliVersion: PACKAGE_INFO.version,
storeSettingsLocale: this._storeSettingsLocale,
host: cliOptions.serverHost,
Copy link

Choose a reason for hiding this comment

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

Bug: startLocalServer and _browserSync.init incorrectly use cliOptions.serverHost instead of cliOptions.host for server binding.
Severity: CRITICAL | Confidence: 1.00

🔍 Detailed Analysis

The startLocalServer function at lib/stencil-start.js:152 and _browserSync.init at lib/stencil-start.js:217 incorrectly attempt to read cliOptions.serverHost. The bin/stencil-start.js script maps the --serverHost CLI argument to options.host, not options.serverHost. Consequently, the host value passed to Server.create() and _browserSync.init() will be undefined, causing the server to default to 'localhost' instead of the user-specified value. This prevents binding to custom hostnames like 0.0.0.0.

💡 Suggested Fix

Modify lib/stencil-start.js at lines 152 and 217 to use cliOptions.host instead of cliOptions.serverHost when configuring the server and BrowserSync.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: lib/stencil-start.js#L152

Potential issue: The `startLocalServer` function at `lib/stencil-start.js:152` and
`_browserSync.init` at `lib/stencil-start.js:217` incorrectly attempt to read
`cliOptions.serverHost`. The `bin/stencil-start.js` script maps the `--serverHost` CLI
argument to `options.host`, not `options.serverHost`. Consequently, the `host` value
passed to `Server.create()` and `_browserSync.init()` will be `undefined`, causing the
server to default to `'localhost'` instead of the user-specified value. This prevents
binding to custom hostnames like `0.0.0.0`.

Did we get this right? 👍 / 👎 to inform future reviews.

});
}

Expand Down Expand Up @@ -213,13 +214,14 @@ class StencilStart {
const watchIgnored = (watchOptions && watchOptions.ignored) || DEFAULT_WATCH_IGNORED;
this._browserSync.init({
open: !!cliOptions.open,
host: cliOptions.serverHost || 'localhost',
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need here and below to default to localhost? I think commander should be doing it here

port: browserSyncPort,
files: watchFiles.map((val) => path.join(themePath, val)),
watchOptions: {
ignoreInitial: true,
ignored: watchIgnored.map((val) => path.join(themePath, val)),
},
proxy: `localhost:${Number(browserSyncPort) + 1}`,
proxy: `${cliOptions.host || 'localhost'}:${Number(browserSyncPort) + 1}`,
tunnel,
});
// Handle manual reloading of browsers by typing 'rs';
Expand Down
2 changes: 1 addition & 1 deletion server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Confidence from 'confidence';
const config = {
$meta: 'Config file',
server: {
host: 'localhost',
host: process.env.STENCIL_SERVER_HOST || 'localhost',
port: 3000,
},
};
Expand Down
1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function buildManifest(srcManifest, options) {
pluginsByName['./plugins/renderer/renderer.module.js'].storeUrl = storeUrl;
pluginsByName['./plugins/renderer/renderer.module.js'].storeSettingsLocale =
options.storeSettingsLocale;
pluginsByName['./plugins/renderer/renderer.module.js'].serverHost = options.serverHost;
pluginsByName['./plugins/theme-assets/theme-assets.module.js'].themePath = options.themePath;
resManifest.register.plugins = _.reduce(
pluginsByName,
Expand Down
3 changes: 2 additions & 1 deletion server/plugins/renderer/renderer.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ function getAcceptLanguageHeader(request) {
* @returns {*}
*/
internals.getPencilResponse = (data, request, response, configuration, renderedRegions = {}) => {
const serverHost = internals.options.serverHost || 'localhost';
const context = {
...data.context,
theme_settings: configuration.settings,
Expand All @@ -368,7 +369,7 @@ internals.getPencilResponse = (data, request, response, configuration, renderedR
theme_version_id: int2uuid(1),
theme_config_id: int2uuid(request.app.themeConfig.variationIndex + 1),
theme_session_id: null,
maintenance: { secure_path: `http://localhost:${internals.options.port}` },
maintenance: { secure_path: `http://${serverHost}:${internals.options.port}` },
},
};
return new PencilResponse(
Expand Down
4 changes: 2 additions & 2 deletions server/plugins/renderer/responses/pencil-response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('PencilResponse', () => {
data = {
context: {
settings: {
base_url: 'http://localhost:3000',
secure_base_url: 'https://localhost:3000',
base_url: 'http://0.0.0.0:3000',
secure_base_url: 'https://0.0.0.0:3000',
},
theme_settings: {},
template_engine: 'handlebars-v3',
Expand Down