Skip to content

Commit 8a0ada9

Browse files
authored
Merge pull request #35 from GTPSHAX/security
Security: add hCaptcha validation for generating docx
2 parents 105efdd + e34507f commit 8a0ada9

30 files changed

Lines changed: 133 additions & 56 deletions

File tree

.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ CORS_TRUSTED_CDN_HOSTS="cdn.example.com"
1818
# --- OpenAI configuration ---
1919
OPENAI_API_KEY="your-openai-api-key-here"
2020
OPENAI_MODEL="gpt-3.5-turbo"
21-
OPENAI_BASE_URL="https://api.openai.com/v1"
21+
OPENAI_BASE_URL="https://api.openai.com/v1"
22+
23+
# --- hCaptcha configuration ---
24+
HCAPTCHA_SECRET_KEY=your-hcaptcha-secret-key-here
25+
HCAPTCHA_SITE_KEY=your-hcaptcha-site-key-here

.env.schema

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ CORS_TRUSTED_CDN_HOSTS=
1818
# --- OpenAI configuration ---
1919
OPENAI_API_KEY=
2020
OPENAI_MODEL=
21-
OPENAI_BASE_URL=
21+
OPENAI_BASE_URL=
22+
23+
# --- hCaptcha configuration ---
24+
HCAPTCHA_SECRET_KEY=
25+
HCAPTCHA_SITE_KEY=

apps/api/.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ CORS_TRUSTED_CDN_HOSTS="cdn.example.com"
1212
# --- OpenAI configuration ---
1313
OPENAI_API_KEY="your-openai-api-key-here"
1414
OPENAI_MODEL="gpt-3.5-turbo"
15-
OPENAI_BASE_URL="https://api.openai.com/v1"
15+
OPENAI_BASE_URL="https://api.openai.com/v1"
16+
17+
# --- hCaptcha configuration ---
18+
HCAPTCHA_SECRET_KEY=your-hcaptcha-secret-key-here
19+
HCAPTCHA_SITE_KEY=your-hcaptcha-site-key-here

apps/api/.env.schema

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ CORS_TRUSTED_CDN_HOSTS=
1212
# --- OpenAI configuration ---
1313
OPENAI_API_KEY=
1414
OPENAI_MODEL=
15-
OPENAI_BASE_URL=
15+
OPENAI_BASE_URL=
16+
17+
# --- hCaptcha configuration ---
18+
HCAPTCHA_SECRET_KEY=
19+
HCAPTCHA_SITE_KEY=

apps/api/middleware/helmet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const helmetMiddleware = helmet({
1313

1414
'upgrade-insecure-requests': isDevelopment ? null : [],
1515
'frame-ancestors': ["'self'", ...CORS_TRUSTED_HOSTS],
16-
'script-src': ["'self'", "'unsafe-inline'", 'https://static.cloudflareinsights.com', ...CORS_TRUSTED_CDN_HOSTS],
17-
'style-src': ["'self'", "'unsafe-inline'", ...CORS_TRUSTED_CDN_HOSTS],
16+
'script-src': ["'self'", "'unsafe-inline'", 'https://static.cloudflareinsights.com', 'https://js.hcaptcha.com', 'https://newassets.hcaptcha.com', ...CORS_TRUSTED_CDN_HOSTS],
17+
'style-src': ["'self'", "'unsafe-inline'", 'https://newassets.hcaptcha.com', ...CORS_TRUSTED_CDN_HOSTS],
1818
'img-src': ["'self'", 'data:', 'https://contrib.rocks', ...CORS_TRUSTED_CDN_HOSTS],
1919
'font-src': ["'self'", ...CORS_TRUSTED_CDN_HOSTS]
2020
}

apps/web/.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ CORS_TRUSTED_CDN_HOSTS="cdn.example.com"
1818
# --- OpenAI configuration ---
1919
OPENAI_API_KEY="your-openai-api-key-here"
2020
OPENAI_MODEL="gpt-3.5-turbo"
21-
OPENAI_BASE_URL="https://api.openai.com/v1"
21+
OPENAI_BASE_URL="https://api.openai.com/v1"
22+
23+
# --- hCaptcha configuration ---
24+
HCAPTCHA_SECRET_KEY=your-hcaptcha-secret-key-here
25+
HCAPTCHA_SITE_KEY=your-hcaptcha-site-key-here

apps/web/.env.schema

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ CORS_TRUSTED_CDN_HOSTS=
1818
# --- OpenAI configuration ---
1919
OPENAI_API_KEY=
2020
OPENAI_MODEL=
21-
OPENAI_BASE_URL=
21+
OPENAI_BASE_URL=
22+
23+
# --- hCaptcha configuration ---
24+
HCAPTCHA_SECRET_KEY=
25+
HCAPTCHA_SITE_KEY=

apps/web/middleware/helmet.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ const helmetMiddleware = helmet({
3030

3131
'upgrade-insecure-requests': isDevelopment ? null : [],
3232
'frame-ancestors': ["'self'", ...CORS_TRUSTED_HOSTS],
33-
'script-src': ["'self'", "'unsafe-inline'", 'https://static.cloudflareinsights.com', ...CORS_TRUSTED_CDN_HOSTS],
34-
'style-src': ["'self'", "'unsafe-inline'", ...CORS_TRUSTED_CDN_HOSTS],
33+
'frame-src': ["'self'", 'https://newassets.hcaptcha.com', 'https://js.hcaptcha.com', ...CORS_TRUSTED_HOSTS],
34+
'script-src': ["'self'", "'unsafe-inline'", 'https://static.cloudflareinsights.com', 'https://js.hcaptcha.com', 'https://newassets.hcaptcha.com', ...CORS_TRUSTED_CDN_HOSTS],
35+
'style-src': ["'self'", "'unsafe-inline'", 'https://newassets.hcaptcha.com', ...CORS_TRUSTED_CDN_HOSTS],
3536
'img-src': ["'self'", 'data:', 'https://contrib.rocks', ...CORS_TRUSTED_CDN_HOSTS],
3637
'font-src': ["'self'", ...CORS_TRUSTED_CDN_HOSTS],
37-
'connect-src': ["'self'", ...(APP_API_BASE_URL ? [removePathFromUrl(APP_API_BASE_URL)] : [])]
38+
'connect-src': ["'self'", 'https://hcaptcha.com', 'https://*.hcaptcha.com', ...(APP_API_BASE_URL ? [removePathFromUrl(APP_API_BASE_URL)] : [])]
3839
}
3940
},
4041
frameguard: false,

apps/web/routes/home.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export const route = new AppRoute('/', 'get', async (req, res) => {
3939
const META = {
4040
APP_API_BASE_URL: process.env.APP_USE_BUILTIN_API === 'true'
4141
? '/api'
42-
: normalizeApiBaseUrl(String(process.env.APP_API_BASE_URL))
42+
: normalizeApiBaseUrl(String(process.env.APP_API_BASE_URL)),
43+
HCAPTCHA_SITE_KEY: String(process.env.HCAPTCHA_SITE_KEY || '')
4344
}
4445

4546
res.render('layout/main', { VIEW: 'home', APP_NAME, TITLE: 'Home', BASE_URL: process.env.BASE_URL || '', META })

docs/api-reference/packages/core/ai/openai-wrapper/classes/default.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Class: default
88

9-
Defined in: [packages/core/ai/openai-wrapper.js:26](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L26)
9+
Defined in: [packages/core/ai/openai-wrapper.js:26](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L26)
1010

1111
OpenAIWrapper is a simple wrapper around the OpenAI API to manage contexts and generate responses.
1212

@@ -29,7 +29,7 @@ Make sure to set the OPENAI_API_KEY and OPENAI_MODEL environment variables befor
2929

3030
> **new default**(`apiKey`, `model?`, `baseURL?`): `OpenAIWrapper`
3131
32-
Defined in: [packages/core/ai/openai-wrapper.js:43](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L43)
32+
Defined in: [packages/core/ai/openai-wrapper.js:43](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L43)
3333

3434
Creates an instance of OpenAIWrapper.
3535

@@ -65,7 +65,7 @@ The base URL for the OpenAI API
6565

6666
> **get** **baseURL**(): `string` \| `null`
6767
68-
Defined in: [packages/core/ai/openai-wrapper.js:63](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L63)
68+
Defined in: [packages/core/ai/openai-wrapper.js:63](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L63)
6969

7070
Gets the base URL for the OpenAI API.
7171

@@ -81,7 +81,7 @@ Gets the base URL for the OpenAI API.
8181

8282
> **get** **client**(): `OpenAI`
8383
84-
Defined in: [packages/core/ai/openai-wrapper.js:88](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L88)
84+
Defined in: [packages/core/ai/openai-wrapper.js:88](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L88)
8585

8686
Gets the OpenAI client instance.
8787

@@ -97,7 +97,7 @@ Gets the OpenAI client instance.
9797

9898
> **get** **context**(): [`Message`](../interfaces/Message.md)[]
9999
100-
Defined in: [packages/core/ai/openai-wrapper.js:96](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L96)
100+
Defined in: [packages/core/ai/openai-wrapper.js:96](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L96)
101101

102102
Gets the default context.
103103

@@ -109,7 +109,7 @@ Gets the default context.
109109

110110
> **set** **context**(`context`): `void`
111111
112-
Defined in: [packages/core/ai/openai-wrapper.js:104](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L104)
112+
Defined in: [packages/core/ai/openai-wrapper.js:104](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L104)
113113

114114
Sets the default context.
115115

@@ -131,7 +131,7 @@ Sets the default context.
131131

132132
> **get** **model**(): `string`
133133
134-
Defined in: [packages/core/ai/openai-wrapper.js:71](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L71)
134+
Defined in: [packages/core/ai/openai-wrapper.js:71](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L71)
135135

136136
Gets the model.
137137

@@ -143,7 +143,7 @@ Gets the model.
143143

144144
> **set** **model**(`model`): `void`
145145
146-
Defined in: [packages/core/ai/openai-wrapper.js:79](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L79)
146+
Defined in: [packages/core/ai/openai-wrapper.js:79](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L79)
147147

148148
Sets the model.
149149

@@ -163,7 +163,7 @@ Sets the model.
163163

164164
> **addContext**(...`contexts`): `void`
165165
166-
Defined in: [packages/core/ai/openai-wrapper.js:115](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L115)
166+
Defined in: [packages/core/ai/openai-wrapper.js:115](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L115)
167167

168168
Adds one or more context objects to the existing default context.
169169

@@ -183,7 +183,7 @@ Adds one or more context objects to the existing default context.
183183

184184
> **chat**(`messages`): `Promise`\<`string`\>
185185
186-
Defined in: [packages/core/ai/openai-wrapper.js:173](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L173)
186+
Defined in: [packages/core/ai/openai-wrapper.js:173](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L173)
187187

188188
Send a chat message to the OpenAI model with the loaded contexts.
189189

@@ -205,7 +205,7 @@ Send a chat message to the OpenAI model with the loaded contexts.
205205

206206
> **loadContext**(`filePath`): `void`
207207
208-
Defined in: [packages/core/ai/openai-wrapper.js:151](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L151)
208+
Defined in: [packages/core/ai/openai-wrapper.js:151](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L151)
209209

210210
Loads a context from a file and adds it to the default context as a system message.
211211

@@ -225,7 +225,7 @@ Loads a context from a file and adds it to the default context as a system messa
225225

226226
> **loadContextsFromDir**(`pathDir`): `void`
227227
228-
Defined in: [packages/core/ai/openai-wrapper.js:126](https://github.com/GTPSHAX/modul-ajar-generator/blob/9db625383fe8ae8db846d4419c7c2eb1c653406a/packages/core/ai/openai-wrapper.js#L126)
228+
Defined in: [packages/core/ai/openai-wrapper.js:126](https://github.com/GTPSHAX/modul-ajar-generator/blob/d4b0e01a4a3d86231cbebd8dec7d9bc8aedf2a93/packages/core/ai/openai-wrapper.js#L126)
229229

230230
Load context(s) from a directory.
231231

0 commit comments

Comments
 (0)