Skip to content

Commit

Permalink
feat(bazel): allow for configuration to relax CORS requirements for h…
Browse files Browse the repository at this point in the history
…ttp-server (#2275)

Allow the configuration to instruct to relax the CORS requirements

PR Close #2275
  • Loading branch information
josephperrott committed Aug 27, 2024
1 parent 5c4a705 commit 5cd9db4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions bazel/http-server/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def _http_server_rule_impl(ctx):
if ctx.attr.enable_dev_ui:
args += "--enable-dev-ui=true "

if ctx.attr.relax_cors:
args += "--relax-cors=true "

for variable_name in ctx.attr.environment_variables:
args += "--environment-variables '%s' " % variable_name

Expand Down Expand Up @@ -101,6 +104,10 @@ http_server_rule = rule(
is a feature from the underlying browsersync implementation.
""",
),
"relax_cors": attr.bool(
default = False,
doc = """Whether to relax the CORS settings on requests.""",
),
"history_api_fallback": attr.bool(
default = True,
doc = """
Expand Down
12 changes: 10 additions & 2 deletions bazel/http-server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import yargs from 'yargs';
import {HttpServer} from './server';
import {setupBazelWatcherSupport} from './ibazel';

const {rootPaths, historyApiFallback, enableDevUi, environmentVariables, port} = yargs(
const {rootPaths, historyApiFallback, enableDevUi, environmentVariables, port, relaxCors} = yargs(
process.argv.slice(2),
)
.strict()
Expand All @@ -20,11 +20,19 @@ const {rootPaths, historyApiFallback, enableDevUi, environmentVariables, port} =
.option('rootPaths', {type: 'array', string: true, default: ['']})
.option('environmentVariables', {type: 'array', string: true, default: []})
.option('enableDevUi', {type: 'boolean', default: false})
.option('relaxCors', {type: 'boolean', default: false})
.parseSync();

// In non-test executions, we will never allow for the browser-sync dev UI.
const enableUi = process.env.TEST_TARGET === undefined && enableDevUi;
const server = new HttpServer(port, rootPaths, enableUi, historyApiFallback, environmentVariables);
const server = new HttpServer(
port,
rootPaths,
enableUi,
historyApiFallback,
environmentVariables,
relaxCors,
);

// Setup ibazel support.
setupBazelWatcherSupport(server);
Expand Down
9 changes: 6 additions & 3 deletions bazel/http-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class HttpServer {
enableUi: boolean,
private _historyApiFallback: boolean = false,
private _environmentVariables: string[] = [],
private _relaxCors: boolean = false,
) {
if (enableUi === false) {
this.options.ui = false;
Expand Down Expand Up @@ -174,9 +175,11 @@ export class HttpServer {
}

private _corsMiddleware(req: http.IncomingMessage, res: http.ServerResponse) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
res.setHeader('cross-origin-opener-policy', 'same-origin');
if (this._relaxCors) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cross-Origin-Embedder-Policy', 'credentialless');
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
}
}
}

Expand Down

0 comments on commit 5cd9db4

Please sign in to comment.