Skip to content

Commit 17fb769

Browse files
authored
Fix web component static file serving (#942)
1 parent 6f021e8 commit 17fb769

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

docs/guides/web-security.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Web Security
22

3-
Mesop by default configures its apps to follow a set of web security best practices.
3+
## Static file serving
44

5-
## How
5+
Mesop allows serving JS and CSS files located within the Mesop app's file subtree to support [web components](../web-components/index.md).
6+
7+
**Security Warning:** Do not place any sensitive or confidential JS and CSS files in your Mesop project directory. These files may be inadvertently exposed and served by the Mesop web server, potentially compromising your application's security.
8+
9+
## JavaScript Security
610

711
At a high-level, Mesop is built on top of Angular which provides [built-in security protections](https://angular.io/guide/security) and Mesop configures a strict [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP).
812

mesop/components/input/e2e/input_test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ test('test input on_blur works', async ({page}) => {
4444
).toBeVisible();
4545
});
4646

47-
test('test textarea shortcuts', async ({page}) => {
47+
// TODO: unskip this test once the flakiness is fixed
48+
test.skip('test textarea shortcuts', async ({page}) => {
4849
await page.goto('/components/input/e2e/textarea_shortcut_app');
4950
const textbox = page.getByLabel('Textarea');
5051
await textbox.fill('hi');
@@ -89,7 +90,8 @@ test('test textarea shortcuts', async ({page}) => {
8990
).toBeVisible();
9091
});
9192

92-
test('test native textarea shortcuts', async ({page}) => {
93+
// TODO: unskip this test once the flakiness is fixed
94+
test.skip('test native textarea shortcuts', async ({page}) => {
9395
await page.goto('/components/input/e2e/textarea_shortcut_app');
9496
const textbox = page.getByPlaceholder('Native textarea');
9597

mesop/examples/web_component/BUILD

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ package(
77
py_library(
88
name = "web_component",
99
srcs = glob(["*.py"]),
10-
data = glob(["*.js"]),
10+
data = glob([
11+
"*.js",
12+
"*.css",
13+
]),
1114
deps = [
1215
"//mesop",
1316
"//mesop/examples/web_component/async_action",

mesop/examples/web_component/testing.css

Whitespace-only changes.

mesop/server/static_file_serving.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,19 @@ def serve_web_components(path: str):
9696
serving_path = (
9797
get_runfile_location(path)
9898
if has_runfiles()
99-
else os.path.join(os.getcwd(), path)
99+
else safe_join(os.getcwd(), path)
100100
)
101+
102+
file_name = os.path.basename(path)
103+
file_extension = os.path.splitext(file_name)[1].lower()
104+
allowed_extensions = {".js", ".css"}
105+
if file_extension not in allowed_extensions:
106+
raise MesopException(
107+
f"Unexpected file type: {file_extension}. Only {', '.join(allowed_extensions)} files are allowed."
108+
)
109+
110+
if not serving_path:
111+
raise MesopException("Unexpected request to " + path)
101112
return send_file_compressed(
102113
serving_path,
103114
disable_gzip_cache=disable_gzip_cache,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {test, expect} from '@playwright/test';
2+
3+
test('web components serving blocks non-js/css files', async ({page}) => {
4+
const response = await page.goto(
5+
'/__web-components-module__/mesop/mesop/examples/web_component/quickstart/counter_component.py',
6+
);
7+
expect(response!.status()).toBe(500);
8+
});
9+
10+
test('web components serving allows js and css files', async ({page}) => {
11+
const jsResponse = await page.goto(
12+
'/__web-components-module__/mesop/mesop/examples/web_component/quickstart/counter_component.js',
13+
);
14+
expect(jsResponse!.status()).toBe(200);
15+
16+
const cssResponse = await page.goto(
17+
'/__web-components-module__/mesop/mesop/examples/web_component/testing.css',
18+
);
19+
expect(cssResponse!.status()).toBe(200);
20+
});

0 commit comments

Comments
 (0)