Skip to content

Commit fc655ed

Browse files
committed
Fix CORS security vulnerability (ICM 111099)
- Restrict CORS origins in PublicVMEnvironment to specific service origin - Restrict CORS origins in LocalIPythonEnvironment to localhost origins - Add allow_all_origins parameter as escape hatch for users who need wildcard CORS and understand the security implications This fixes CWE-942 (Wildcard CORS) by defaulting to restrictive CORS policy while preserving backward compatibility via opt-in parameter.
1 parent 917a491 commit fc655ed

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

rai_core_flask/rai_core_flask/environments/local_ipython_environment.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ def __init__(self, service):
3131

3232
def select(self, service):
3333
service.with_credentials = False
34-
service.cors = CORS(service.app)
34+
if service.allow_all_origins:
35+
# User explicitly opted into allowing all origins (less secure)
36+
service.cors = CORS(service.app)
37+
else:
38+
# Default: restrict CORS to localhost origins
39+
origins = [
40+
f"http://localhost:{service.port}",
41+
f"http://127.0.0.1:{service.port}"
42+
]
43+
service.cors = CORS(service.app, origins=origins)
3544
service.env_name = LOCAL

rai_core_flask/rai_core_flask/environments/public_vm_environment.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ def __init__(self, service):
3131

3232
def select(self, service):
3333
service.with_credentials = False
34-
service.cors = CORS(service.app)
34+
if service.allow_all_origins:
35+
# User explicitly opted into allowing all origins (less secure)
36+
service.cors = CORS(service.app)
37+
else:
38+
# Default: restrict CORS to the specific service origin
39+
origin = f"http://{service.ip}:{service.port}"
40+
service.cors = CORS(service.app, origins=[origin])
3541
service.env_name = PUBLIC_VM

rai_core_flask/rai_core_flask/flask_helper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ class FlaskHelper(object):
2525
"""FlaskHelper is a class for common Flask utilities used in dashboards."""
2626

2727
def __init__(self, ip=None, port=None, with_credentials=False,
28-
is_private_link=False):
28+
is_private_link=False, allow_all_origins=False):
2929
# The name passed to Flask needs to be unique per instance.
3030
self.app = Flask(uuid.uuid4().hex)
3131

3232
self.port = port
3333
self.ip = ip
3434
self.with_credentials = with_credentials
3535
self.is_private_link = is_private_link
36+
self.allow_all_origins = allow_all_origins
3637
# dictionary to store arbitrary state for use by consuming classes
3738
self.shared_state = {}
3839
if self.ip is None:

0 commit comments

Comments
 (0)