-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mariusz-schimke/feature/nuget
Add NuGet configuration for publishing SensitiveString
- Loading branch information
Showing
10 changed files
with
338 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
name: Build and Test | ||
|
||
on: | ||
push: | ||
branches: [ main, develop ] | ||
pull_request_target: | ||
branches: [ main, develop ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: 8.0.x | ||
|
||
- name: Restore dependencies | ||
run: dotnet restore | ||
|
||
- name: Build solution | ||
run: dotnet build --configuration Release --no-restore | ||
|
||
- name: Run tests | ||
run: dotnet test --no-restore |
This file contains 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,44 @@ | ||
name: Publish a NuGet Package | ||
|
||
on: | ||
release: | ||
types: [ published ] | ||
|
||
env: | ||
BRANCH: main | ||
|
||
jobs: | ||
build: | ||
runs-on: windows-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: 8.0.x | ||
|
||
- name: Restore dependencies | ||
run: dotnet restore | ||
|
||
- name: Build solution | ||
run: dotnet build --configuration Release --no-restore | ||
|
||
- name: Run tests | ||
run: dotnet test --no-restore | ||
|
||
- shell: pwsh | ||
name: Create SNK File | ||
env: | ||
SNK: ${{ secrets.snk }} | ||
run: | | ||
$snk = [Convert]::FromBase64String("$env:SNK") | ||
Set-Content "src\SensitiveString\SensitiveString.snk" -Value $snk -AsByteStream | ||
- name: Build NuGet Package | ||
run: dotnet pack src\SensitiveString\SensitiveString.csproj --configuration Publish -p:Repository=${{ github.repository }} -p:Branch=${{ env.BRANCH }} -p:Commit=${{ github.sha }} | ||
|
||
- name: Publish NuGet Package | ||
run: dotnet nuget push src\SensitiveString\bin\Publish\SensitiveString.*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.nuget_api_key }} |
This file contains 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
This file contains 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,2 +1,123 @@ | ||
# SensitiveString | ||
A proof of concept to deliver a solution that mitigates the risk of sensitive information leaking into application logs | ||
Introducing SensitiveString, your shield against inadvertent exposure of sensitive information in application logs and beyond. | ||
|
||
This lightweight NuGet package wraps strings in a protective layer, ensuring that sensitive data remains secure and inaccessible without explicit handling. | ||
|
||
Safeguard your users' privacy and your application's integrity effortlessly with SensitiveString. | ||
|
||
## Example | ||
|
||
Let's try to initialize and print a simple record with personal information: | ||
|
||
```c# | ||
internal record PersonDto(string Name, SensitiveString PhoneNumber, SensitiveEmail Email); | ||
``` | ||
|
||
```c# | ||
using SensitiveString; | ||
|
||
var person = new PersonDto( | ||
"John Doe", | ||
"(800) 555‑0123".AsSensitive(), | ||
"[email protected]".AsSensitiveEmail() | ||
); | ||
|
||
Console.WriteLine($"Person info: {person}"); | ||
``` | ||
|
||
What we get in the console is: | ||
|
||
``` | ||
Person info: PersonDto { Name = John Doe, PhoneNumber = ***, Email = ***@example.com } | ||
``` | ||
|
||
Now let's try to access the original information: | ||
|
||
```c# | ||
Console.WriteLine($"Phone number: {person.PhoneNumber.Reveal()}"); | ||
Console.WriteLine($"Email: {person.Email.Reveal()}"); | ||
``` | ||
|
||
And what we now get in the console is: | ||
|
||
``` | ||
Phone number: (800) 555‑0123 | ||
Email: [email protected] | ||
``` | ||
|
||
## How it works? | ||
|
||
The `SensitiveString` and `SensitiveEmail` types are straightforward string wrappers. Without special handling, their content remains hidden from standard stringifiers and serializers. So if you're afraid some sensitive data may leak into application logs when stringified implicitly, use one of these two types to prevent that. | ||
|
||
`SensitiveEmail` differs from `SensitiveString` only in how it masks the original value. If you prefer having emails fully masked rather than the login part before @, use the `SensitiveString` instead. | ||
|
||
|
||
|
||
## Serialization/deserialization | ||
|
||
In client-server communication we want the information in its original form present in the data being transmitted between the parties. Because, however, of how the types are designed, without explicit handling, serializers will output nothing but an empty object. | ||
|
||
Therefore, to use the type in DTOs, you'll need to extend your serializers in use with special converters to handle these types. Converters for the `System.Text.Json.JsonSerializer` are available in this repository and are part of the associated NuGet package. See below how serialization behaves with and without them. | ||
|
||
Let's use the same person object as in the example used earlier: | ||
|
||
```c# | ||
using System.Text.Json; | ||
... | ||
|
||
var serialized = JsonSerializer.Serialize(person); | ||
Console.WriteLine($"Serialized: {serialized}"); | ||
``` | ||
|
||
This is what we will get as the output: | ||
|
||
``` | ||
Serialized: {"Name":"John Doe","PhoneNumber":{},"Email":{}} | ||
``` | ||
|
||
As you can see, the sensitive strings are just empty JSON objects `{}`. | ||
|
||
Now let's add appropriate converters to serializer options: | ||
|
||
```c# | ||
using System.Text.Json; | ||
using SensitiveString.Json; | ||
... | ||
|
||
var serializerOptions = new JsonSerializerOptions(); | ||
serializerOptions.AddSensitiveStringSupport(); | ||
|
||
var serialized = JsonSerializer.Serialize(person, serializerOptions); | ||
Console.WriteLine($"Serialized: {serialized}"); | ||
``` | ||
|
||
Now the output is complete: | ||
|
||
``` | ||
Serialized: {"Name":"John Doe","PhoneNumber":"(800) 555\u20110123","Email":"[email protected]"} | ||
``` | ||
|
||
The same options should be used for deserialization. | ||
|
||
## REST API | ||
|
||
To make sure your web API handles the types correctly, call this on startup: | ||
|
||
```c# | ||
builder.Services | ||
.AddControllers() | ||
.AddJsonOptions( | ||
o => o.JsonSerializerOptions.AddSensitiveStringSupport() | ||
); | ||
|
||
builder.Services.ConfigureHttpJsonOptions( | ||
o => o.SerializerOptions.AddSensitiveStringSupport() | ||
); | ||
``` | ||
|
||
|
||
|
||
## Disclaimer | ||
|
||
This is a proof of concept. If you find any issues using the package or have any thoughts on it, your comments reported as issues are more than welcome! | ||
|
This file contains 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
This file contains 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,3 @@ | ||
namespace SensitiveString.Examples; | ||
|
||
internal record PersonDto(string Name, SensitiveString PhoneNumber, SensitiveEmail Email); |
This file contains 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,24 @@ | ||
using System.Text.Json; | ||
using SensitiveString; | ||
using SensitiveString.Examples; | ||
using SensitiveString.Json; | ||
|
||
var person = new PersonDto( | ||
"John Doe", | ||
"(800) 555‑0123".AsSensitive(), | ||
"[email protected]".AsSensitiveEmail() | ||
); | ||
|
||
|
||
// --- stringification --- | ||
Console.WriteLine($"Person info: {person}"); | ||
Console.WriteLine($"Phone number: {person.PhoneNumber.Reveal()}"); | ||
Console.WriteLine($"Email: {person.Email.Reveal()}"); | ||
|
||
|
||
// --- JSON serialization --- | ||
var serializerOptions = new JsonSerializerOptions(); | ||
serializerOptions.AddSensitiveStringSupport(); | ||
|
||
var serialized = JsonSerializer.Serialize(person, serializerOptions); | ||
Console.WriteLine($"Serialized: {serialized}"); |
14 changes: 14 additions & 0 deletions
14
src/SensitiveString.Examples/SensitiveString.Examples.csproj
This file contains 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SensitiveString\SensitiveString.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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
This file contains 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 +1,72 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" /> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<!-- https://www.meziantou.net/creating-reproducible-build-in-dotnet.htm --> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Configurations>Debug;Release;Publish</Configurations> | ||
<Platforms>AnyCPU</Platforms> | ||
|
||
<!-- <ApplicationIcon>..\..\assets\icon.ico</ApplicationIcon> --> | ||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild> | ||
|
||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
|
||
<!-- to include generated files in the NuGet package --> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
|
||
<Deterministic>true</Deterministic> | ||
|
||
<Version>0.1.0</Version> | ||
<IncludeSourceRevisionInInformationalVersion>true</IncludeSourceRevisionInInformationalVersion> | ||
|
||
<PackageId>SensitiveString</PackageId> | ||
<Title>SensitiveString</Title> | ||
<Product>SensitiveString</Product> | ||
<Description>Introducing SensitiveString, your shield against inadvertent exposure of sensitive information in application logs and beyond. This lightweight NuGet package wraps strings in a protective layer, ensuring that sensitive data remains secure and inaccessible without explicit handling. Safeguard your users' privacy and your application's integrity effortlessly with SensitiveString.</Description> | ||
<Authors>Mariusz Schimke</Authors> | ||
<Copyright>Copyright © 2024 Mariusz Schimke</Copyright> | ||
<PackageTags>sensitive strings privacy</PackageTags> | ||
|
||
<PackageProjectUrl>https://github.com/$(Repository)</PackageProjectUrl> | ||
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile> | ||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance> | ||
<!-- <PackageIcon>icon.png</PackageIcon> --> | ||
<EnablePackageValidation>true</EnablePackageValidation> | ||
|
||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/$(Repository).git</RepositoryUrl> | ||
<RepositoryBranch>$(Branch)</RepositoryBranch> | ||
<RepositoryCommit>$(Commit)</RepositoryCommit> | ||
|
||
<PackageReleaseNotes>This initial package release is presented as a proof of concept. All comments and suggestions are warmly welcomed and encouraged to be reported as issues on GitHub. Your input is highly valued to help refine and improve this offering. | ||
|
||
See also https://github.com/$(Repository)/releases</PackageReleaseNotes> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)' == 'Publish'"> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>SensitiveString.snk</AssemblyOriginatorKeyFile> | ||
<Optimize>true</Optimize> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'"> | ||
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" /> | ||
<PackageReference Include="SauceControl.InheritDoc"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<!-- <ItemGroup> | ||
<None Include="../../assets/icon.png" Pack="true" Visible="false" PackagePath="/" /> | ||
</ItemGroup> --> | ||
|
||
</Project> |