Skip to content

Commit 25ad69d

Browse files
committed
Refactor CloudFront function and build process
1 parent 5cdc9d2 commit 25ad69d

17 files changed

Lines changed: 1752 additions & 13 deletions

.github/actions/tf-plan-apply/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ runs:
6363
aws-region: ${{ inputs.aws_region }}
6464
mask-aws-account-id: true
6565

66+
- name: Generate CloudFront function
67+
run: npm install && npm run build
68+
working-directory: ./infrastructure/code
69+
shell: bash
70+
6671
- name: Setup Terraform
6772
uses: hashicorp/setup-terraform@v4
6873
with:

.github/workflows/automated-sonarqube-cloud-analysis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ jobs:
2121
with:
2222
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
2323

24+
- name: Set up Node.js
25+
uses: actions/setup-node@v6
26+
with:
27+
node-version: '24'
28+
29+
- name: Install and test
30+
working-directory: infrastructure/code
31+
run: |
32+
npm ci
33+
npm test
34+
2435
- name: SonarQube Cloud Scan
2536
uses: SonarSource/sonarqube-scan-action@v7
2637
env:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ tfplan
3636
.idea/
3737
.vscode/
3838
venv/
39+
coverage/
3940

4041
#Ignore certificates
4142
scripts/csrs
4243
scripts/keys
44+
infrastructure/code/dist

infrastructure/cloudfront.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ resource "aws_cloudfront_function" "block_invalid_urls" {
132132
runtime = "cloudfront-js-2.0"
133133
comment = "Blocks invalid URL requests"
134134
publish = true
135-
code = file("${path.module}/code/block-invalid-urls.js")
135+
code = file("${path.module}/code/dist/block-invalid-urls.js")
136136
}
137137

138138
resource "aws_cloudfront_origin_request_policy" "viewer" {

infrastructure/code/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

infrastructure/code/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# CloudFront Functions
2+
3+
TypeScript source for CloudFront Functions deployed by Terraform.
4+
5+
## Runtime constraints
6+
7+
These functions run on CloudFront Functions 2.0 (QuickJS), which rejects:
8+
9+
- ES modules (`import`/`export`)
10+
- `require()` for user code (built-ins like `crypto`, `querystring`, `buffer` only)
11+
- `eval`, `Function` constructor, `setTimeout`, network, filesystem
12+
13+
So each source file under `src/` must be a flat script with a top-level
14+
`function handler(event)`. No imports, no exports. Types come from
15+
triple-slash references only.
16+
17+
## Layout
18+
19+
```text
20+
src/ TypeScript sources — one file per deployed function
21+
test/ Vitest suites; load the compiled artifact via node:vm
22+
so tests exercise the exact script CloudFront will run
23+
dist/ Build output. Committed. Terraform reads from here.
24+
```
25+
26+
## Commands
27+
28+
```bash
29+
npm run build compile src/ → dist/
30+
npm test build then run vitest
31+
npm run test:watch vitest in watch mode
32+
```
33+
34+
Rebuild and commit `dist/` whenever a source file changes. Terraform reads
35+
`dist/block-invalid-urls.js` at plan time.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function handler(event) {
2+
const uri = event.request.uri;
3+
const allowedPattern = /^\/(review\/|upload\/)?[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/;
4+
if (allowedPattern.test(uri)) {
5+
return event.request;
6+
}
7+
return {
8+
statusCode: 403,
9+
statusDescription: 'Forbidden',
10+
headers: {
11+
'content-type': { value: 'text/plain' }
12+
},
13+
body: 'Access Denied'
14+
};
15+
}

0 commit comments

Comments
 (0)