Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check URLs starting with http or https when adding/updating ShortURLs #123

Merged
merged 6 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
"code",
"ideas"
]
},
{
"login": "fs366e2spm",
"name": "fs366e2spm",
"avatar_url": "https://avatars2.githubusercontent.com/u/52791126?v=4",
"profile": "https://github.com/fs366e2spm",
"contributions": [
"bug",
"ideas"
]
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://github.com/AK0785"><img src="https://avatars1.githubusercontent.com/u/40241010?v=4" width="100px;" alt=""/><br /><sub><b>AKER</b></sub></a><br /><a href="#ideas-AK0785" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="http://baaijte.net"><img src="https://avatars3.githubusercontent.com/u/1761079?v=4" width="100px;" alt=""/><br /><sub><b>Vincent Baaij</b></sub></a><br /><a href="https://github.com/FBoucher/AzUrlShortener/commits?author=vnbaaij" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/kmm7"><img src="https://avatars3.githubusercontent.com/u/13196402?v=4" width="100px;" alt=""/><br /><sub><b>kmm7</b></sub></a><br /><a href="https://github.com/FBoucher/AzUrlShortener/commits?author=kmm7" title="Code">💻</a> <a href="#ideas-kmm7" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://github.com/fs366e2spm"><img src="https://avatars2.githubusercontent.com/u/52791126?v=4" width="100px;" alt=""/><br /><sub><b>fs366e2spm</b></sub></a><br /><a href="https://github.com/FBoucher/AzUrlShortener/issues?q=author%3Afs366e2spm" title="Bug reports">🐛</a> <a href="#ideas-fs366e2spm" title="Ideas, Planning, & Feedback">🤔</a></td>
</tr>
</table>

Expand Down
3 changes: 3 additions & 0 deletions src/adminTools/adminBlazorWebsite/src/Data/ShortUrlEntity.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace adminBlazorWebsite.Data
Expand All @@ -10,6 +11,8 @@ public class ShortUrlEntity

public string Title { get; set; }

[Required]
[Url]
public string Url { get; set; }

public string ShortUrl { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public string Vanity {
}

[Required]
[Url]
public string Url { get; set; }

}
Expand Down
121 changes: 50 additions & 71 deletions src/adminTools/adminBlazorWebsite/src/Pages/Urls.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,86 +46,65 @@ else
</tbody>
</table>
<p>
<!-- Add a new ShortUrl -->
<button class="btn btn-primary"
@onclick="CreateShortUrl">
Add New Url
</button>
</p>
<!-- Add a new ShortUrl -->
<button class="btn btn-primary"
@onclick="CreateShortUrl">
Add New Url
</button>
</p>
}

@if(ShowCreatePopup)
@if (ShowCreatePopup)
{
<div class="modal" tabindex="-1" style="display:block" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Short Url Request</h3>
<!-- Button to close the popup -->
<button type="button" class="close"
@onclick="ClosePopup">
<span aria-hidden="true">X</span>
</button>
</div>
<div class="modal-body">
<label>Title</label>
<input class="form-control" type="text"
placeholder="Title or description for this URL"
@bind="shortUrlRequest.Title" />
<br />
<label>The Url to redirect</label>
<input class="form-control" type="text"
placeholder="The Url to redirect"
@bind="shortUrlRequest.Url" />
<br />
<label>Vanity</label>
<input class="form-control" type="text"
placeholder="Vanity or the 'End' part of the Url"
@bind="shortUrlRequest.Vanity" />
<br />
<button class="btn btn-primary"
@onclick="SaveShortUrl">
Save
</button>
</div>
<Modal Title="Short Url Request" ClosePopup="ClosePopup">
<EditForm Model="shortUrlRequest" OnValidSubmit="SaveShortUrl">
<DataAnnotationsValidator />
<div>
<label for="title">Title</label>
<InputText id="title" class="form-control" placeholder="Title or description for this URL" @bind-Value="shortUrlRequest.Title" />
<ValidationMessage For="@(() => shortUrlRequest.Title)" />
</div>
</div>
</div>
<br />
<div>
<label for="url">The Url to redirect</label>
<InputText id="url" class="form-control" placeholder="The Url to redirect" @bind-Value="shortUrlRequest.Url" />
<ValidationMessage For="@(() => shortUrlRequest.Url)" />
</div>
<br />
<div>
<label for="vanity">Vanity</label>
<InputText id="vanity" class="form-control" placeholder="Vanity or the 'End' part of the Url" @bind-Value="shortUrlRequest.Vanity" />
<ValidationMessage For="@(() => shortUrlRequest.Vanity)" />
</div>
<button class="btn btn-primary" type="submit">
Save
</button>
</EditForm>
</Modal>
}


@if(ShowEditPopup)
@if (ShowEditPopup)
{
<div class="modal" tabindex="-1" style="display:block" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Edit Short Url</h3>
<!-- Button to close the popup -->
<button type="button" class="close"
@onclick="ClosePopup">
<span aria-hidden="true">X</span>
</button>
</div>
<div class="modal-body">
<label>Title</label>
<input class="form-control" type="text"
placeholder="Title or description for this URL"
@bind="editedUrl.Title" />
<br />
<label>The Url to redirect</label>
<input class="form-control" type="text"
placeholder="https://..."
@bind="editedUrl.Url" />
<br />
<button class="btn btn-primary"
@onclick="SaveUpdatedShortUrl">
Save
</button>
</div>
<Modal Title="Edit Short Url" ClosePopup="ClosePopup">
<EditForm Model="editedUrl" OnValidSubmit="SaveUpdatedShortUrl">
<DataAnnotationsValidator />
<div>
<label for="edit-title">Title</label>
<InputText id="edit-title" class="form-control" placeholder="Title or description for this URL" @bind-Value="editedUrl.Title" />
<ValidationMessage For="@(() => editedUrl.Title)" />
</div>
<br />
<div>
<label for="edit-url">The Url to redirect</label>
<InputText id="edit-url" class="form-control" placeholder="The Url to redirect" @bind-Value="editedUrl.Url" />
<ValidationMessage For="@(() => editedUrl.Url)" />
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">
Save
</button>
</EditForm>
</Modal>
}


Expand Down
32 changes: 32 additions & 0 deletions src/adminTools/adminBlazorWebsite/src/Shared/Modal.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="modal" tabindex="-1" style="display:block" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">@Title</h3>
<!-- Button to close the popup -->
<button type="button" class="close"
@onclick="ClosePopup">
<span aria-hidden="true">X</span>
</button>
</div>
<div class="modal-body">
@ChildContent
</div>
</div>
</div>
</div>

<div class="modal-backdrop show"></div>



@code {
[Parameter]
public string Title { get; set; }

[Parameter]
public EventCallback ClosePopup { get; set; }

[Parameter]
public RenderFragment ChildContent { get; set; }
}
14 changes: 11 additions & 3 deletions src/shortenerTools/UrlShortener/UrlShortener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@

namespace Cloud5mins.Function
{

public static class UrlShortener
{

[FunctionName("UrlShortener")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
Expand All @@ -53,7 +55,13 @@ public static async Task<HttpResponseMessage> Run(
{
return req.CreateResponse(HttpStatusCode.NotFound);
}


// Validates if input.url is a valid aboslute url, aka is a complete refrence to the resource, ex: http(s)://google.com
if (!Uri.IsWellFormedUriString(input.Url, UriKind.Absolute))
{
return req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{input.Url} is not a valid absolute Url. The Url parameter must start with 'http://' or 'http://'.");
}

var result = new ShortResponse();
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
Expand All @@ -66,8 +74,8 @@ public static async Task<HttpResponseMessage> Run(
try
{
string longUrl = input.Url.Trim();
string vanity = input.Vanity.Trim();
string title = input.Title.Trim();
string vanity = string.IsNullOrWhiteSpace(input.Vanity) ? "" : input.Vanity.Trim();
string title = string.IsNullOrWhiteSpace(input.Title) ? "" : input.Title.Trim();

ShortUrlEntity newRow;

Expand Down
6 changes: 6 additions & 0 deletions src/shortenerTools/UrlUpdate/UrlUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public static async Task<HttpResponseMessage> Run(
return req.CreateResponse(HttpStatusCode.NotFound);
}

// Validates if input.url is a valid aboslute url, aka is a complete refrence to the resource, ex: http(s)://google.com
if (!Uri.IsWellFormedUriString(input.Url, UriKind.Absolute))
{
return req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{input.Url} is not a valid absolute Url. The Url parameter must start with 'http://' or 'http://'.");
}

ShortUrlEntity result;
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
Expand Down