Skip to content

Commit 882a862

Browse files
committed
Hardning security in authors and posts APIs
1 parent 3e2ae11 commit 882a862

File tree

8 files changed

+47
-34
lines changed

8 files changed

+47
-34
lines changed

src/Core/Api/AssetsController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ public async Task<AssetsModel> Get(int page = 1, string filter = "", string sear
6868
}
6969

7070
/// <summary>
71-
/// Select an asset in the File Manager to include in the post
71+
/// Select an asset in the File Manager (authentication required)
7272
/// </summary>
7373
/// <param name="type">Type of asset (post cover, logo, avatar or post image/attachment)</param>
7474
/// <param name="asset">Selected asset</param>
7575
/// <param name="post">Post ID</param>
7676
/// <returns>Asset Item</returns>
7777
[HttpGet("pick")]
78+
[Authorize]
7879
public async Task<AssetItem> Pick(string type, string asset, string post)
7980
{
8081
if (type == "postCover")
@@ -111,7 +112,7 @@ public async Task<AssetItem> Pick(string type, string asset, string post)
111112
}
112113

113114
/// <summary>
114-
/// Upload file(s) to user data store, authentication required
115+
/// Upload file(s) to user data store (authentication required)
115116
/// </summary>
116117
/// <param name="files">Selected files</param>
117118
/// <returns>Success or internal error</returns>
@@ -134,7 +135,7 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
134135
}
135136

136137
/// <summary>
137-
/// Remove file from user data store, authentication required
138+
/// Remove file from user data store (authentication required)
138139
/// </summary>
139140
/// <param name="url">Relative URL of the file to remove</param>
140141
/// <returns></returns>

src/Core/Api/AuthorsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task<ActionResult<IEnumerable<Author>>> Get(int page = 1)
4141
try
4242
{
4343
var pager = new Pager(page);
44-
var authors = await _data.Authors.GetList(u => u.Created > DateTime.MinValue, pager);
44+
var authors = await _data.Authors.GetList(u => u.Created > DateTime.MinValue, pager, !User.Identity.IsAuthenticated);
4545
return Ok(authors);
4646
}
4747
catch (Exception ex)
@@ -60,7 +60,7 @@ public async Task<ActionResult<Author>> Get(string author)
6060
{
6161
try
6262
{
63-
var result = _data.Authors.Single(a => a.AppUserName == author);
63+
var result = _data.Authors.GetItem(a => a.AppUserName == author, !User.Identity.IsAuthenticated);
6464
if (result == null) return NotFound();
6565

6666
return Ok(await Task.FromResult(result));
@@ -72,7 +72,7 @@ public async Task<ActionResult<Author>> Get(string author)
7272
}
7373

7474
/// <summary>
75-
/// Register new author. Authorized admins only.
75+
/// Register new author (admins only)
7676
/// </summary>
7777
/// <param name="model">Author model</param>
7878
/// <returns>Created Author object</returns>
@@ -114,7 +114,7 @@ public async Task<ActionResult<Author>> Post(RegisterModel model)
114114
}
115115

116116
/// <summary>
117-
/// Update author
117+
/// Update author (admins only)
118118
/// </summary>
119119
/// <param name="model">Author model</param>
120120
/// <returns>Success or 500 error</returns>
@@ -138,7 +138,7 @@ public async Task<ActionResult> Update(Author model)
138138
}
139139

140140
/// <summary>
141-
/// Change author password. Authorized users only.
141+
/// Change author password (authentication required)
142142
/// </summary>
143143
/// <param name="model">Author model</param>
144144
/// <returns>Success or 500 error</returns>
@@ -169,7 +169,7 @@ public async Task<ActionResult> ChangePwd(ChangePasswordModel model)
169169
}
170170

171171
/// <summary>
172-
/// Delete author, from membership, database and file system. Admin only.
172+
/// Delete author, from membership, database and file system (admins only)
173173
/// </summary>
174174
/// <param name="id">Author ID</param>
175175
/// <returns>Success or 500 error</returns>

src/Core/Api/PostsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ public async Task<PostItem> GetPost(int id)
9595
{
9696
if (id > 0)
9797
{
98-
return await _data.BlogPosts.GetItem(p => p.Id == id);
98+
return await _data.BlogPosts.GetItem(p => p.Id == id, !User.Identity.IsAuthenticated);
9999
}
100100
else
101101
{
102-
var author = await _data.Authors.GetItem(a => a.AppUserName == User.Identity.Name);
102+
var author = await _data.Authors.GetItem(a => a.AppUserName == User.Identity.Name, !User.Identity.IsAuthenticated);
103103
var blog = await _data.CustomFields.GetBlogSettings();
104104
return new PostItem { Author = author, Cover = blog.Cover };
105105
}
106106
}
107107

108108
/// <summary>
109-
/// Set post as published or draft
109+
/// Set post as published or draft (authentication required)
110110
/// </summary>
111111
/// <param name="id">Post ID</param>
112112
/// <param name="flag">Flag; P - publish, U - unpublish</param>
@@ -137,7 +137,7 @@ public async Task<ActionResult> Publish(int id, string flag)
137137
}
138138

139139
/// <summary>
140-
/// Set post as featured
140+
/// Set post as featured (admins only)
141141
/// </summary>
142142
/// <param name="id">Post ID</param>
143143
/// <param name="flag">Flag; F - featured, U - remove from featured</param>
@@ -168,7 +168,7 @@ public async Task<ActionResult> Feature(int id, string flag)
168168
}
169169

170170
/// <summary>
171-
/// Save blog post
171+
/// Save blog post (authentication required)
172172
/// </summary>
173173
/// <param name="post">Post item</param>
174174
/// <returns>Saved post item</returns>
@@ -189,7 +189,7 @@ public async Task<ActionResult<PostItem>> Post(PostItem post)
189189
}
190190

191191
/// <summary>
192-
/// Remove post item
192+
/// Remove post item (authentication required)
193193
/// </summary>
194194
/// <param name="id">Post ID</param>
195195
/// <returns>Success or failure</returns>

src/Core/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Constants
1717
public static string BlogCover = "blog-cover";
1818
public static string Culture = "culture";
1919

20+
public static string DummyEmail = "dummy@blog.com";
21+
2022
public static string ThemeEditReturnUrl = "~/admin/settings/theme";
2123
}
2224
}

src/Core/CoreAPI.xml

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Core/Data/Repositories/AuthorRepository.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace Core.Data
99
{
1010
public interface IAuthorRepository : IRepository<Author>
1111
{
12-
Task<Author> GetItem(Expression<Func<Author, bool>> predicate);
13-
Task<IEnumerable<Author>> GetList(Expression<Func<Author, bool>> predicate, Pager pager);
12+
Task<Author> GetItem(Expression<Func<Author, bool>> predicate, bool sanitized = false);
13+
Task<IEnumerable<Author>> GetList(Expression<Func<Author, bool>> predicate, Pager pager, bool sanitize = false);
1414
Task Save(Author author);
1515
Task Remove(int id);
1616
}
@@ -24,7 +24,7 @@ public AuthorRepository(AppDbContext db) : base(db)
2424
_db = db;
2525
}
2626

27-
public async Task<Author> GetItem(Expression<Func<Author, bool>> predicate)
27+
public async Task<Author> GetItem(Expression<Func<Author, bool>> predicate, bool sanitized = false)
2828
{
2929
try
3030
{
@@ -33,6 +33,7 @@ public async Task<Author> GetItem(Expression<Func<Author, bool>> predicate)
3333
if (author != null)
3434
{
3535
author.Avatar = author.Avatar ?? AppSettings.Avatar;
36+
author.Email = sanitized ? Constants.DummyEmail : author.Email;
3637
}
3738

3839
return await Task.FromResult(author);
@@ -43,7 +44,7 @@ public async Task<Author> GetItem(Expression<Func<Author, bool>> predicate)
4344
}
4445
}
4546

46-
public async Task<IEnumerable<Author>> GetList(Expression<Func<Author, bool>> predicate, Pager pager)
47+
public async Task<IEnumerable<Author>> GetList(Expression<Func<Author, bool>> predicate, Pager pager, bool sanitize = false)
4748
{
4849
var take = pager.ItemsPerPage == 0 ? 10 : pager.ItemsPerPage;
4950
var skip = pager.CurrentPage * take - take;
@@ -55,6 +56,14 @@ public async Task<IEnumerable<Author>> GetList(Expression<Func<Author, bool>> pr
5556

5657
var list = users.Skip(skip).Take(take).ToList();
5758

59+
if (sanitize)
60+
{
61+
foreach (var item in list)
62+
{
63+
item.Email = Constants.DummyEmail;
64+
}
65+
}
66+
5867
return await Task.FromResult(list);
5968
}
6069

src/Core/Data/Repositories/PostRepository.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IPostRepository : IRepository<BlogPost>
1414
Task<IEnumerable<PostItem>> GetList(Expression<Func<BlogPost, bool>> predicate, Pager pager);
1515
Task<IEnumerable<PostItem>> GetList(Pager pager, int author = 0, string category = "", string include = "", bool sanitize = false);
1616
Task<IEnumerable<PostItem>> Search(Pager pager, string term, int author = 0, string include = "", bool sanitize = false);
17-
Task<PostItem> GetItem(Expression<Func<BlogPost, bool>> predicate);
17+
Task<PostItem> GetItem(Expression<Func<BlogPost, bool>> predicate, bool sanitize = false);
1818
Task<PostModel> GetModel(string slug);
1919
Task<PostItem> SaveItem(PostItem item);
2020
Task SaveCover(int postId, string asset);
@@ -127,12 +127,13 @@ public async Task<IEnumerable<PostItem>> Search(Pager pager, string term, int au
127127
return await Task.Run(() => posts.Skip(skip).Take(pager.ItemsPerPage).ToList());
128128
}
129129

130-
public async Task<PostItem> GetItem(Expression<Func<BlogPost, bool>> predicate)
130+
public async Task<PostItem> GetItem(Expression<Func<BlogPost, bool>> predicate, bool sanitize = false)
131131
{
132132
var post = _db.BlogPosts.Single(predicate);
133133
var item = PostToItem(post);
134134

135135
item.Author.Avatar = string.IsNullOrEmpty(item.Author.Avatar) ? "lib/img/avatar.jpg" : item.Author.Avatar;
136+
item.Author.Email = sanitize ? Constants.DummyEmail : item.Author.Email;
136137

137138
return await Task.FromResult(item);
138139
}
@@ -273,7 +274,7 @@ PostItem PostToItem(BlogPost p, bool sanitize = false)
273274
{
274275
post.Author.Avatar = string.IsNullOrEmpty(post.Author.Avatar) ?
275276
AppSettings.Avatar : post.Author.Avatar;
276-
post.Author.Email = sanitize ? "" : post.Author.Email;
277+
post.Author.Email = sanitize ? Constants.DummyEmail : post.Author.Email;
277278
}
278279
return post;
279280
}

src/Core/Services/StorageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public async Task<IEnumerable<AssetItem>> Find(Func<AssetItem, bool> predicate,
271271
{
272272
foreach (var p in page)
273273
{
274-
p.Path = "";
274+
p.Path = p.Path.Replace(Location, "");
275275
}
276276
}
277277

0 commit comments

Comments
 (0)