-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda_function.rb
106 lines (99 loc) · 3.84 KB
/
lambda_function.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Returns a URL (as string) or `nil`.
def parse_route(location)
case location
when "/"
"https://www.worldcubeassociation.org/"
# Denylist (Reserved)
# These resources should be accessed at the canonical WCA website URL.
when /^\/(api|oauth)(\/.*)?$/
return nil
# Website (Reserved)
when /^\/(about|admin|competitions|contact|delegates|documents|faq|notifications|organizations|panel|persons|privacy|profile|regulations|search|teams-committees|tutorial|users).*$/
"https://www.worldcubeassociation.org/#{$1}"
# Rankings and Records
when /^\/rankings(\/.*)?$/
"https://www.worldcubeassociation.org/results/rankings#{$1}"
when /^\/records(\/.*)?$/
"https://www.worldcubeassociation.org/results/records#{$1}"
when /^\/results(\/.*)?$/
"https://www.worldcubeassociation.org/results/rankings#{$1}"
# Website
when /^\/([0-9][0-9][0-9][0-9][A-Z][A-Z][A-Z][A-Z][0-9][0-9])$/ #WCA ID
"https://www.worldcubeassociation.org/persons/#{$1}"
# Competitions
when /^\/([^\/]+)([0-9][0-9][0-9][0-9])$/
"https://www.worldcubeassociation.org/competitions/#{$1}#{$2}"
when /^\/live\/([^\/]+)([0-9][0-9][0-9][0-9])$/
"https://live.worldcubeassociation.org/link/competitions/#{$1}#{$2}"
when /^\/([^\/]+)([0-9][0-9][0-9][0-9])\/live$/
# Note: `/live/CompetitionName2019` is the canonical short link, recommended
# over `/CompetitionName2019/live` when posting a link.
#
# But we support the latter, just in case someone types that. (This could
# happen if they're going by vague memory rather than reading the URL
# directly.) That way, we take them where they're clearly trying to go,
# instead of causing confusion or annoyance.
"https://live.worldcubeassociation.org/link/competitions/#{$1}#{$2}"
# Regulations and Guidelines
when /^\/([0-9]{1,2}[a-z]([0-9]{1,2}[a-z]?)?|[A-Z][0-9]{1,2}([a-z]([0-9]{1,2})?)?)(\+*)$/
guidelines_page = "#{$5}" == "" ? "" : "guidelines.html"
"https://www.worldcubeassociation.org/regulations/#{guidelines_page}\##{$1}#{$5}"
# Social Media
when /^\/(instagram|ig)$/
"https://www.instagram.com/thewcaofficial/"
when /^\/(facebook|fb)$/
"https://www.facebook.com/WorldCubeAssociation/"
when /^\/twitter$/
"https://www.twitter.com/theWCAofficial/"
when /^\/reddit$/
"https://www.reddit.com/r/TheWCAOfficial/"
when /^\/twitch$/
"https://www.twitch.tv/worldcubeassociation/"
when /^\/weibo$/
"https://www.weibo.com/theWCA/"
when /^\/(youtube|yt)$/
"https://www.youtube.com/channel/UC5OUMUnS8PvY1RvtB1pQZ0g"
# Software
when /^\/(github|gh)$/
"https://github.com/thewca/"
# Domain
when /^\/staging$/
"https://staging.worldcubeassociation.org"
when /^\/(cubecomps|cc)$/
"https://www.cubecomps.com"
when /^\/live$/
"https://live.worldcubeassociation.org"
# Shop
when /^\/shop$/
"https://shop.worldcubeassociation.org"
when /^\/newcomer$/
"https://linktr.ee/newcomermonth"
# Communication
when /^\/slack$/
"https://worldcubeassociation.slack.com"
when /^\/forum$/
"https://forum.worldcubeassociation.org"
when /^\/groups$/
"https://groups.google.com/a/worldcubeassociation.org/"
# Default
when /^\/(.*)$/
"https://www.worldcubeassociation.org/#{$1}"
end
end
def get_response(event)
# Only allow GET requests, to discourage sending sensitive data.
return { statusCode: 405 } unless event["httpMethod"] == "GET"
redirect_url = parse_route(event["path"])
# If the path was on the denylist, deny access.
return { statusCode: 404 } if redirect_url.nil?
# Redirect!
{ statusCode: 302, headers: { Location: redirect_url } }
end
def lambda_handler(event:, context:)
response = get_response(event)
# Add unconditional headers.
response[:headers] = (response[:headers] || {}).merge({
"Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload"
})
response
end