-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce a vulnerability for SQL Injection #4
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
Open
jaliyaudagedara
wants to merge
5
commits into
main
Choose a base branch
from
introduce-sql-injection-vulnerability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b67526b
Introduce a vulnerability for SQL Injection
jaliyaudagedara 0580bc4
Move away from top-level statements as CodeQL breaks.
jaliyaudagedara ad57f61
Move classes to it's own files.
jaliyaudagedara 743720d
Move away from File scoped namespaces.
jaliyaudagedara 37bb6f3
Remove global usings.
jaliyaudagedara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace Todo.Api.Models | ||
{ | ||
// Create ToDo class | ||
public class ToDo | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Title { get; set; } | ||
|
||
public bool IsDone { get; set; } | ||
|
||
public DateTime CreatedDateTime { get; set; } | ||
|
||
public DateTime? CompletedDateTime { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,109 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
WebApplicationBuilder? builder = WebApplication.CreateBuilder(args); | ||
|
||
// ConfigureServices | ||
builder.Services.AddDbContext<ToDoDbContext>(options => | ||
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
WebApplication app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
// Map endpoint to get ToDos | ||
app.MapGet("/Todos", async (ToDoDbContext dbContext) => | ||
{ | ||
return Results.Ok(await dbContext.ToDos.ToListAsync()); | ||
}); | ||
|
||
// Map endpoint to get ToDo by id | ||
app.MapGet("/Todos/{id}", async (ToDoDbContext dbContext, int id) => | ||
{ | ||
ToDo? toDo = await dbContext.ToDos.FindAsync(id); | ||
if (toDo is null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
return Results.Ok(toDo); | ||
}); | ||
|
||
// Map endpoint to create ToDo | ||
app.MapPost("/Todos", async (ToDoDbContext dbContext, ToDo toDo) => | ||
{ | ||
await dbContext.ToDos.AddAsync(toDo); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return Results.Created($"/Todos/{toDo.Id}", toDo); | ||
}); | ||
|
||
// Map endpoint to update ToDo | ||
app.MapPut("/Todos/{id}", async (ToDoDbContext dbContext, int id, ToDo toDo) => | ||
{ | ||
if (await dbContext.ToDos.FindAsync(id) == null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
toDo.Id = id; | ||
dbContext.ToDos.Update(toDo); | ||
|
||
return Results.NoContent(); | ||
}); | ||
|
||
// Map endpoint to delete ToDo | ||
app.MapDelete("/Todos/{id}", async (ToDoDbContext dbContext, int id) => | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Todo.Api.Models; | ||
|
||
namespace Todo.Api | ||
{ | ||
ToDo? toDo = await dbContext.ToDos.FindAsync(id); | ||
if (toDo is null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
dbContext.ToDos.Remove(toDo); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return Results.NoContent(); | ||
}); | ||
|
||
await app.RunAsync(); | ||
|
||
// Create ToDo class | ||
public class ToDo | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Title { get; set; } | ||
|
||
public bool IsDone { get; set; } | ||
|
||
public DateTime CreatedDateTime { get; set; } | ||
|
||
public DateTime? CompletedDateTime { get; set; } | ||
} | ||
|
||
// Create ToDoDbContext with Entity Framework | ||
public class ToDoDbContext : DbContext | ||
{ | ||
public DbSet<ToDo> ToDos { get; set; } | ||
|
||
public ToDoDbContext(DbContextOptions<ToDoDbContext> options) | ||
: base(options) | ||
public class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
WebApplicationBuilder? builder = WebApplication.CreateBuilder(args); | ||
|
||
// ConfigureServices | ||
builder.Services.AddDbContext<ToDoDbContext>(options => | ||
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
WebApplication app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
// Map endpoint to get ToDos | ||
app.MapGet("/Todos", async (ToDoDbContext dbContext) => | ||
{ | ||
return Results.Ok(await dbContext.ToDos.ToListAsync()); | ||
}); | ||
|
||
// Map endpoint to get ToDo by id | ||
app.MapGet("/Todos/{id}", async (ToDoDbContext dbContext, int id) => | ||
{ | ||
ToDo? toDo = await dbContext.ToDos.FindAsync(id); | ||
if (toDo is null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
return Results.Ok(toDo); | ||
}); | ||
|
||
// Map endpoint to search ToDo by Title | ||
app.MapGet("/Todos/Search", async (ToDoDbContext dbContext, string title) => | ||
{ | ||
// Introduce a vulnerability for SQL Injection | ||
// Ex: title = ';DROP TABLE dbo.ToDos;-- | ||
|
||
List<ToDo>? results = await dbContext.ToDos | ||
.FromSqlRaw("SELECT * FROM dbo.ToDos WHERE Title = '" + title + "'") | ||
.ToListAsync(); | ||
|
||
return Results.Ok(results); | ||
}); | ||
|
||
// Map endpoint to create ToDo | ||
app.MapPost("/Todos", async (ToDoDbContext dbContext, ToDo toDo) => | ||
{ | ||
await dbContext.ToDos.AddAsync(toDo); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return Results.Created($"/Todos/{toDo.Id}", toDo); | ||
}); | ||
|
||
// Map endpoint to update ToDo | ||
app.MapPut("/Todos/{id}", async (ToDoDbContext dbContext, int id, ToDo toDo) => | ||
{ | ||
if (await dbContext.ToDos.FindAsync(id) == null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
toDo.Id = id; | ||
dbContext.ToDos.Update(toDo); | ||
|
||
return Results.NoContent(); | ||
}); | ||
|
||
// Map endpoint to delete ToDo | ||
app.MapDelete("/Todos/{id}", async (ToDoDbContext dbContext, int id) => | ||
{ | ||
ToDo? toDo = await dbContext.ToDos.FindAsync(id); | ||
if (toDo is null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
dbContext.ToDos.Remove(toDo); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return Results.NoContent(); | ||
}); | ||
|
||
await app.RunAsync(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Todo.Api.Models; | ||
|
||
namespace Todo.Api | ||
{ | ||
// Create ToDoDbContext with Entity Framework | ||
public class ToDoDbContext : DbContext | ||
{ | ||
public DbSet<ToDo> ToDos { get; set; } | ||
|
||
public ToDoDbContext(DbContextOptions<ToDoDbContext> options) | ||
Check noticeCode scanning / CodeQL Compilation message
Warning CS8618 Non-nullable property 'ToDos' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
|
||
: base(options) | ||
{ | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Compilation message