Skip to content

Commit 922ed5a

Browse files
authored
Merge pull request #8 from usds/tomn-usds/csp-header
Added content-security-policy
2 parents 6ba34ba + c63c43c commit 922ed5a

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

.env.development

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# No Content-Security-Policy for dev because it breaks the
2+
# hot reloading
3+
VITE_META_CSP=

.env.production

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# .env.production
2+
3+
# Content-Security-Policy content="
4+
# base-uri 'self';
5+
# default-src 'self';
6+
# script-src 'self';
7+
# style-src 'self' https://usds.github.io/ https://usds.gov/ 'unsafe-inline';
8+
# img-src 'self' https: data: blob:;
9+
# font-src 'self' https://usds.github.io/ https://usds.gov/ https://*.gov/;
10+
# form-action 'none';
11+
# worker-src 'self' https: data: blob:;
12+
#
13+
VITE_META_CSP=<meta http-equiv="Content-Security-Policy" content="base-uri 'self'; default-src 'self'; script-src 'self'; style-src 'self' https://usds.github.io/ https://usds.gov/ 'unsafe-inline'; img-src 'self' https: data: blob:; font-src 'self' https://usds.github.io/ https://usds.gov/ https://*.gov/; form-action 'none';worker-src 'self' https: data: blob:;">

README.md

+82-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# USDS Site Editor
22

3+
This tool addresses the error-prone part of adding or editing posts on the Jekyll Frontmatter website,
4+
editing the yaml header and markdown content using WYSIWYG editor and guided forms modals.
5+
36
A static-html, front-end React client to edit markdown and have it look visually similar to how it appears on the final
47
site.
58

@@ -9,6 +12,8 @@ Results of edits can be saved to the local device as a zip file, including any i
912

1013
Site is github pages deployed here: https://usds.github.io/website-content-editor/
1114

15+
16+
1217
## What's working:
1318

1419
- Heavily leverages the open source project: [MDXEditor](https://github.com/mdx-editor/editor)
@@ -18,7 +23,7 @@ Site is github pages deployed here: https://usds.github.io/website-content-edito
1823
- Downloads zip file of the markdown and images ready to submitted (all without a server)
1924
- Can "upload" some existing markdown to make quick edits.
2025

21-
## Developers
26+
# Developers
2227
1. To deploy, merge `main` branch into `gh-pages`. [See branches](https://github.com/usds/website-content-editor/branches)
2328
1. The [deploy action](https://github.com/usds/website-content-editor/actions) will automatically run.
2429
1. Go to the github hosted website: https://usds.github.io/website-content-editor/
@@ -30,3 +35,79 @@ Site is github pages deployed here: https://usds.github.io/website-content-edito
3035
- Link component showing offsite links correctly
3136
- Help pages for how to merge in markdown edits back into website
3237
- Need to reorganize code. Lots of things should be separated out into components.
38+
39+
## Build
40+
The build scripts are all standard Vite stuff:
41+
- `dev` Builds + watches + runs the site in development mode.
42+
- `build` Builds `production` mode into the `dist/` directory
43+
- `preview` Starts up a webserver pointing to `dist/` directory to check release builds
44+
- `update` Is the `yarn` command to update dependencies
45+
46+
### Some build exception - because... of course
47+
The service-worker is a non-standard. The normal usage of a service-worker is to provide an offline app.
48+
49+
OUR usage is to allow embedded images in the rich editor to function without a server.
50+
51+
Build-wise, the service worker script is standard `.js` and the two files
52+
are copied directly into the `dist/` directory. Because `registerSW.js`
53+
and `service-worker.js` are standard javascript, they are **not** transpiled
54+
and they don't import anything. So, some constants associated with the cache
55+
are duplicated and need to kept in sync.
56+
57+
## Service-Worker
58+
59+
60+
## Security
61+
Site is 100% static. All content is stored in the browser's cache and localstorage.
62+
63+
64+
### Content-Security-Policy
65+
Since it's running on github.io, we cannot set a CSP http header.
66+
The next best thing is a `<meta>` header.
67+
68+
Vite dev mode uses hot reloading, which uses unsafe `eval`, so for
69+
localdev, the CSP is disabled.
70+
71+
Enable/disable is done via the `.env.development` `.env.production` files.
72+
73+
Here's the current CSP:
74+
```html
75+
<meta http-equiv="Content-Security-Policy"
76+
content="
77+
base-uri 'self';
78+
default-src 'self';
79+
script-src 'self';
80+
style-src 'self' https://usds.github.io/ https://usds.gov/ 'unsafe-inline';
81+
img-src 'self' https: data: blob:;
82+
font-src 'self' https://usds.github.io/ https://usds.gov/ https://*.gov/;
83+
form-action 'none';
84+
worker-src 'self' https: data: blob:;">
85+
```
86+
87+
#### csp scripting
88+
The most important items are:
89+
```
90+
default-src 'self';
91+
script-src 'self';
92+
```
93+
These lock down scripting to just the site and prevents `eval` injections.
94+
95+
#### csp style
96+
```
97+
style-src 'self' https://usds.github.io/ https://usds.gov/ 'unsafe-inline';
98+
```
99+
The tool loads style scripts from website staging.
100+
101+
`unsafe-inline` is required because some of the 3rd party tools dynamically sets `style=` attribute.
102+
103+
#### csp images
104+
```
105+
img-src 'self' https: data: blob:;
106+
```
107+
The `https:` allows images to be loaded from anywhere. This allows the
108+
109+
110+
## Debugging
111+
112+
[details about how to debug a service worker]
113+

index.html

+5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
<meta name="viewport" content="width=device-width,initial-scale=1">
66
<title>Website Content Editor</title>
77
<meta name="description" content="Markdown content editor for USDS">
8+
<!-- to debug CSP, use `build` then `preview` -->
9+
%VITE_META_CSP%
10+
811
<link rel="icon" href="favicon.ico">
912
<link rel="apple-touch-icon" href="apple-touch-icon.png" sizes="180x180">
1013
<link rel="mask-icon" href="mask-icon.svg" color="#FFFFFF">
1114
<meta name="theme-color" content="#ffffff">
15+
1216
<!-- this is the DESTINATION of the script when it's copied over. DO not add type="module" or vite will remove -->
17+
1318
<link rel="manifest" href="manifest.webmanifest" />
1419
<link rel="stylesheet" media="all" href="https://usds.github.io/website-staging/assets/stylesheets/styles.css" />
1520
</head>

src/mdxcomponents/frontmatterCustom/FrontmatterCustomEditor.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ export const FrontmatterCustomEditor = ({yaml, onChange}: FrontmatterCustomEdito
130130
setValue("readtime_minutes", readingTimeInMinutes);
131131
}
132132
const previewImgFilename = cleanupFilename(getValues("carousel_image"));
133-
const fileInputDefaultImage = previewImgFilename.length ? `/mdedit/img/${previewImgFilename}` : undefined
134-
135-
console.log(`inital value for carousel_show: ${getValues("carousel_show")}`)
133+
const fileInputDefaultImage = previewImgFilename.length ? `/mdedit/img/${previewImgFilename}` : undefined;
134+
console.log(`inital value for carousel_show: ${getValues("carousel_show")}`);
136135
return (
137136
<Fragment>
138137
<Dialog.Root open={frontmatterDialogOpen} onOpenChange={(open) => setFrontmatterDialogOpen(open)}>

src/vite-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/// https://vitejs.dev/guide/env-and-mode#intellisense-for-typescript
12
/// <reference types="vite/client" />

0 commit comments

Comments
 (0)