Skip to content

Commit 846f19c

Browse files
authored
Merge pull request #6 from fdoxyz/aasa-env-enforced-and-invalid-url-enhancement
Enhancements for invalid URL handling + AASA ENV variable enforced
2 parents c63be80 + c47fc89 commit 846f19c

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

server.rb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,33 @@
4747
end
4848

4949
get '/.well-known/apple-app-site-association' do
50-
aasa_app_id = ENV['AASA_APP_ID'].to_s
5150
content_type :json
52-
{
53-
"applinks": {
54-
"apps": [],
55-
"details":[
56-
{
57-
"appID": aasa_app_id,
58-
"paths": ["/*"]
59-
}
60-
]
61-
},
62-
"activitycontinuation": {
63-
"apps": [aasa_app_id]
64-
}
65-
}.to_json
51+
52+
aasa_app_id = ENV['AASA_APP_ID'].to_s
53+
if aasa_app_id.present?
54+
{
55+
"applinks": {
56+
"apps": [],
57+
"details":[
58+
{
59+
"appID": aasa_app_id,
60+
"paths": ["/*"]
61+
}
62+
]
63+
},
64+
"activitycontinuation": {
65+
"apps": [aasa_app_id]
66+
}
67+
}.to_json
68+
else
69+
{ error: 'AASA_APP_ID not configured' }.to_json
70+
end
6671
end
6772

6873
get '/*' do
6974
begin
70-
target_url = URI(params['splat'].first)
71-
raise 'Invalid redirect URL' if target_url.host != request.host
75+
target_url = URI(params['splat'].first.gsub('https:/', 'https://'))
76+
raise 'Invalid redirect URL' unless target_url.host.present?
7277
redirect target_url
7378
rescue => error
7479
@error = error

spec/app_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ def app
1818
expect(last_response).to be_redirect
1919
expect(last_response.location).to eq(target_url)
2020
end
21+
22+
it "redirects when passing target in REST first level param" do
23+
target_url = "https://dev.to/fdoxyz"
24+
get "/#{target_url}"
25+
expect(last_response).to be_redirect
26+
expect(last_response.location).to eq(target_url)
27+
end
2128
end
2229

2330
context "failure" do
@@ -31,12 +38,28 @@ def app
3138
get '/'
3239
end
3340

34-
it "renders fallback page if requesting any other path" do
41+
it "renders fallback page if requesting anything other than URL redirect" do
3542
get '/about-us'
3643
end
3744

3845
it "renders fallback page if r parameter is an invalid URL" do
3946
get '/?r=poorthing-ble$$ur<3'
4047
end
4148
end
49+
50+
context "AASA" do
51+
it "responds with AASA when AASA_APP_ID is configured" do
52+
allow(ENV).to receive(:[]).with('AASA_APP_ID').and_return("R9SWHSQNV8.com.forem.app")
53+
get '/.well-known/apple-app-site-association'
54+
expect(last_response).to be_ok
55+
expect(last_response.body).to eq("{\"applinks\":{\"apps\":[],\"details\":[{\"appID\":\"R9SWHSQNV8.com.forem.app\",\"paths\":[\"/*\"]}]},\"activitycontinuation\":{\"apps\":[\"R9SWHSQNV8.com.forem.app\"]}}")
56+
end
57+
58+
it "responds with error when AASA_APP_ID isn't configured" do
59+
allow(ENV).to receive(:[]).with('AASA_APP_ID').and_return("")
60+
get '/.well-known/apple-app-site-association'
61+
expect(last_response).to be_ok
62+
expect(last_response.body).to eq("{\"error\":\"AASA_APP_ID not configured\"}")
63+
end
64+
end
4265
end

0 commit comments

Comments
 (0)