Skip to content

Commit 4bf2bdb

Browse files
committed
Merge branch 'do-not-index' into wasm-content-type
# Conflicts: # s3.tf
2 parents 4764b8f + 012e0c7 commit 4bf2bdb

File tree

2 files changed

+119
-41
lines changed

2 files changed

+119
-41
lines changed

domain.tf

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
21
variable "domain_name_zone" {
32
}
43
variable "domain_name" {
54
}
5+
variable "content_security_policy" {
6+
type = map(list(string))
7+
default = {
8+
default-src : [
9+
"*",
10+
"'unsafe-eval'",
11+
"'wasm-unsafe-eval'",
12+
"'unsafe-inline'",
13+
"data:",
14+
"mediastream:",
15+
"blob:",
16+
"filesystem:",
17+
"about:",
18+
"ws:",
19+
"wss:",
20+
]
21+
frame-src : [
22+
"*",
23+
"data:",
24+
"blob:",
25+
]
26+
form-action : [
27+
"*"
28+
]
29+
frame-ancestors : [
30+
"*",
31+
"data:",
32+
"blob:",
33+
]
34+
}
35+
}
636
variable "geo_restrictions_mode" {
7-
type = string
37+
type = string
838
default = "none"
939
validation {
1040
condition = contains([
@@ -20,7 +50,7 @@ variable "geo_restrictions_list" {
2050
default = []
2151
}
2252
variable "react_mode" {
23-
type = bool
53+
type = bool
2454
default = false
2555
}
2656

@@ -29,7 +59,7 @@ data "aws_route53_zone" "main" {
2959
}
3060

3161
resource "aws_acm_certificate" "web" {
32-
provider = aws.acm
62+
provider = aws.acm
3363
domain_name = var.domain_name
3464
validation_method = "DNS"
3565
}
@@ -41,8 +71,8 @@ resource "aws_route53_record" "web" {
4171
ttl = "300"
4272
}
4373
resource "aws_acm_certificate_validation" "web" {
44-
provider = aws.acm
45-
certificate_arn = aws_acm_certificate.web.arn
74+
provider = aws.acm
75+
certificate_arn = aws_acm_certificate.web.arn
4676
validation_record_fqdns = [aws_route53_record.web.fqdn]
4777
}
4878
resource "aws_route53_record" "web_cloudfront" {
@@ -64,7 +94,7 @@ resource "aws_cloudfront_origin_access_identity" "oai" {
6494
resource "aws_cloudfront_distribution" "main" {
6595
depends_on = [aws_s3_bucket.files]
6696
enabled = true
67-
aliases = [var.domain_name]
97+
aliases = [var.domain_name]
6898
default_root_object = "index.html"
6999

70100
origin {
@@ -78,13 +108,14 @@ resource "aws_cloudfront_distribution" "main" {
78108
}
79109
}
80110
default_cache_behavior {
81-
allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
82-
cached_methods = ["GET", "HEAD", "OPTIONS"]
83-
target_origin_id = "origin-${var.domain_name}"
111+
allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"]
112+
cached_methods = ["GET", "HEAD", "OPTIONS"]
113+
target_origin_id = "origin-${var.domain_name}"
84114
viewer_protocol_policy = "redirect-to-https" # other options - https only, http
115+
response_headers_policy_id = aws_cloudfront_response_headers_policy.webapp_security_headers.id
85116

86117
forwarded_values {
87-
headers = []
118+
headers = []
88119
query_string = true
89120

90121
cookies {
@@ -107,7 +138,7 @@ resource "aws_cloudfront_distribution" "main" {
107138
restrictions {
108139
geo_restriction {
109140
restriction_type = var.geo_restrictions_mode
110-
locations = var.geo_restrictions_list
141+
locations = var.geo_restrictions_list
111142
}
112143
}
113144

@@ -116,4 +147,39 @@ resource "aws_cloudfront_distribution" "main" {
116147
ssl_support_method = "sni-only"
117148
minimum_protocol_version = "TLSv1.2_2018"
118149
}
150+
}
151+
152+
resource "aws_cloudfront_response_headers_policy" "webapp_security_headers" {
153+
name = "webapp-security-headers-${replace(var.domain_name, "/[^a-zA-Z0-9\\-]/", "-")}"
154+
security_headers_config {
155+
content_type_options {
156+
override = true
157+
}
158+
frame_options {
159+
frame_option = "DENY"
160+
override = true
161+
}
162+
referrer_policy {
163+
referrer_policy = "same-origin"
164+
override = true
165+
}
166+
xss_protection {
167+
mode_block = true
168+
protection = true
169+
override = true
170+
}
171+
strict_transport_security {
172+
access_control_max_age_sec = "63072000"
173+
include_subdomains = true
174+
preload = true
175+
override = true
176+
}
177+
content_security_policy {
178+
content_security_policy = join("; ", [
179+
for key, value in var.content_security_policy : "${key} ${join(" ", value)}"
180+
])
181+
# content_security_policy = "frame-ancestors 'self'; default-src 'self'; img-src ${var.external_media_sources}; media-src ${var.external_media_sources}; script-src 'self' ${var.external_script_sources}; style-src 'self' 'unsafe-inline'; object-src 'none'; connect-src ${var.external_connections}"
182+
override = true
183+
}
184+
}
119185
}

s3.tf

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,53 @@ resource "aws_s3_bucket_cors_configuration" "files" {
5555
}
5656
}
5757

58+
variable "create_robots_txt" {
59+
type = bool
60+
default = true
61+
}
62+
63+
resource "local_file" "robots-txt" {
64+
count = var.create_robots_txt ? 1 : 0
65+
content = "User-agent: *\nDisallow: "
66+
filename = "${var.dist_folder}/robots.txt"
67+
}
68+
5869
locals {
5970
content_type_overrides = {
6071
"apple-app-site-association" = "application/json"
6172
}
6273
# Taken from https://github.com/hashicorp/terraform-template-dir/blob/17b81de441645a94f4db1449fc8269cd32f26fde/variables.tf
6374
# with some additions for file types we need to support
6475
known_mime_types = {
65-
".3g2" : "video/3gpp2",
66-
".3gp" : "video/3gpp",
67-
".atom" : "application/atom+xml",
68-
".css" : "text/css; charset=utf-8",
69-
".eot" : "application/vnd.ms-fontobject",
70-
".gif" : "image/gif",
71-
".htm" : "text/html; charset=utf-8",
72-
".html" : "text/html; charset=utf-8",
73-
".ico" : "image/vnd.microsoft.icon",
74-
".jar" : "application/java-archive",
75-
".jpeg" : "image/jpeg",
76-
".jpg" : "image/jpeg",
77-
".js" : "application/javascript",
78-
".json" : "application/json",
79-
".jsonld" : "application/ld+json",
80-
".otf" : "font/otf",
81-
".pdf" : "application/pdf",
82-
".png" : "image/png",
83-
".rss" : "application/rss+xml",
84-
".svg" : "image/svg+xml",
85-
".swf" : "application/x-shockwave-flash",
86-
".ttf" : "font/ttf",
87-
".txt" : "text/plain; charset=utf-8",
88-
".weba" : "audio/webm",
89-
".webm" : "video/webm",
90-
".webp" : "image/webp",
91-
".woff" : "font/woff",
92-
".woff2" : "font/woff2",
93-
".xhtml" : "application/xhtml+xml",
76+
".3g2" : "video/3gpp2",
77+
".3gp" : "video/3gpp",
78+
".atom" : "application/atom+xml",
79+
".css" : "text/css; charset=utf-8",
80+
".eot" : "application/vnd.ms-fontobject",
81+
".gif" : "image/gif",
82+
".htm" : "text/html; charset=utf-8",
83+
".html" : "text/html; charset=utf-8",
84+
".ico" : "image/vnd.microsoft.icon",
85+
".jar" : "application/java-archive",
86+
".jpeg" : "image/jpeg",
87+
".jpg" : "image/jpeg",
88+
".js" : "application/javascript",
89+
".json" : "application/json",
90+
".jsonld" : "application/ld+json",
91+
".otf" : "font/otf",
92+
".pdf" : "application/pdf",
93+
".png" : "image/png",
94+
".rss" : "application/rss+xml",
95+
".svg" : "image/svg+xml",
96+
".swf" : "application/x-shockwave-flash",
97+
".ttf" : "font/ttf",
98+
".txt" : "text/plain; charset=utf-8",
99+
".weba" : "audio/webm",
100+
".webm" : "video/webm",
101+
".webp" : "image/webp",
102+
".woff" : "font/woff",
103+
".woff2" : "font/woff2",
104+
".xhtml" : "application/xhtml+xml",
94105
".xml" : "application/xml",
95106
".wasm" : "application/wasm"
96107
}
@@ -100,6 +111,7 @@ module "template_files" {
100111

101112
base_dir = var.dist_folder
102113
file_types = local.known_mime_types
114+
depends_on = ["local_file.robots-txt"]
103115
}
104116
resource "aws_s3_object" "app_storage" {
105117
for_each = module.template_files.files

0 commit comments

Comments
 (0)