|
1 | 1 | using Microsoft.Azure.Functions.Worker; |
2 | 2 | using Microsoft.Azure.Functions.Worker.Http; |
3 | | -using Microsoft.Extensions.Logging; |
4 | 3 | using System.Net; |
5 | 4 |
|
6 | 5 | namespace SampleFunctions; |
7 | 6 |
|
8 | | -public class GetDataFromUrl(ILogger<GetDataFromUrl> logger) |
| 7 | +public class GetDataFromUrl() |
9 | 8 | { |
10 | | - private readonly ILogger<GetDataFromUrl> _logger = logger; |
| 9 | + private static readonly string[] AllowedDomains = [ |
| 10 | + "https://samples.azuremaps.com/", |
| 11 | + "https://www.microsoft.com/" |
| 12 | + ]; |
11 | 13 |
|
12 | 14 | private static readonly HttpClient _HttpClient = new(); |
13 | 15 |
|
14 | 16 | [Function("GetDataFromUrl")] |
15 | | - public static async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) |
| 17 | + public static async Task<HttpResponseData> RunAsync( |
| 18 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) |
16 | 19 | { |
17 | | - var badRequest = req.CreateResponse(HttpStatusCode.BadRequest); |
18 | | - |
19 | | - // Get the URL from the query string |
20 | | - var url = Uri.UnescapeDataString(req.Query["url"]); |
21 | | - if (string.IsNullOrEmpty(url)) |
| 20 | + // Check if the referer header is present and if the domain is allowed |
| 21 | + if (req.Headers.TryGetValues("Referer", out var referer) && AllowedDomains.Any(domain => referer.ToString().StartsWith(domain))) |
22 | 22 | { |
23 | | - badRequest.WriteString("Please pass a valid URL address in the query string."); |
24 | | - return badRequest; |
25 | | - } |
| 23 | + var badRequest = req.CreateResponse(HttpStatusCode.BadRequest); |
26 | 24 |
|
27 | | - // Download the content from the URL |
28 | | - var result = await _HttpClient.GetAsync(url); |
| 25 | + // Get the URL from the query string |
| 26 | + var url = Uri.UnescapeDataString(req.Query["url"]); |
| 27 | + if (string.IsNullOrEmpty(url)) |
| 28 | + { |
| 29 | + badRequest.WriteString("Please pass a valid URL address in the query string."); |
| 30 | + return badRequest; |
| 31 | + } |
29 | 32 |
|
30 | | - if (!result.IsSuccessStatusCode) |
31 | | - { |
32 | | - badRequest.WriteString("The URL you specified was unable to download."); |
33 | | - return badRequest; |
34 | | - } |
| 33 | + // Download the content from the URL |
| 34 | + var result = await _HttpClient.GetAsync(url); |
35 | 35 |
|
36 | | - // Create a new response |
37 | | - var response = req.CreateResponse(HttpStatusCode.OK); |
| 36 | + if (!result.IsSuccessStatusCode) |
| 37 | + { |
| 38 | + badRequest.WriteString("The URL you specified was unable to download."); |
| 39 | + return badRequest; |
| 40 | + } |
38 | 41 |
|
39 | | - // Copy the headers from the result to the response, except for the ones we want to skip |
40 | | - foreach (var header in result.Headers) |
41 | | - { |
42 | | - if (!IsHeaderToSkip(header.Key)) |
| 42 | + // Create a new response |
| 43 | + var response = req.CreateResponse(HttpStatusCode.OK); |
| 44 | + |
| 45 | + // Copy the headers from the result to the response, except for the ones we want to skip |
| 46 | + foreach (var header in result.Headers) |
43 | 47 | { |
44 | | - response.Headers.Add(header.Key, header.Value.First()); |
| 48 | + if (!IsHeaderToSkip(header.Key)) |
| 49 | + { |
| 50 | + response.Headers.Add(header.Key, header.Value.First()); |
| 51 | + } |
45 | 52 | } |
46 | | - } |
47 | 53 |
|
48 | | - // Set the 'Content-Type' header in the response |
49 | | - response.Headers.Add("Content-Type", result.Content.Headers.ContentType.MediaType); |
| 54 | + // Set the 'Content-Type' header in the response |
| 55 | + response.Headers.Add("Content-Type", result.Content.Headers.ContentType.MediaType); |
| 56 | + |
| 57 | + // Add the CORS header to the response |
| 58 | + response.Headers.Add("Access-Control-Allow-Origin", "*"); |
50 | 59 |
|
51 | | - // Add the CORS header to the response |
52 | | - response.Headers.Add("Access-Control-Allow-Origin", "*"); |
| 60 | + // Copy the content from the result to the response |
| 61 | + var content = await result.Content.ReadAsByteArrayAsync(); |
| 62 | + await response.Body.WriteAsync(content); |
53 | 63 |
|
54 | | - // Copy the content from the result to the response |
55 | | - var content = await result.Content.ReadAsByteArrayAsync(); |
56 | | - await response.Body.WriteAsync(content); |
| 64 | + return response; |
| 65 | + } |
57 | 66 |
|
58 | | - return response; |
| 67 | + // Return access denied if the referer domain is not allowed |
| 68 | + var forbidden = req.CreateResponse(HttpStatusCode.Forbidden); |
| 69 | + return forbidden; |
59 | 70 | } |
60 | 71 |
|
61 | 72 | private static bool IsHeaderToSkip(string headerName) |
|
0 commit comments