Skip to content

Commit

Permalink
Better token metadata sanitizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Sep 27, 2022
1 parent 27dd890 commit ff99ad0
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Tzkt.Sync/Services/TokenMetadata/TokenMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Dapper;
using Npgsql;
using Netezos.Encoding;

namespace Tzkt.Sync.Services
{
Expand All @@ -20,6 +22,7 @@ public class TokenMetadata : BackgroundService
readonly string ConnectionString;
readonly TokenMetadataConfig Config;
readonly ILogger Logger;
readonly Regex Regex = new(@"(?<=(^|[^\\])(\\\\)*)\\u0000", RegexOptions.Compiled);

TokenMetadataState State;

Expand Down Expand Up @@ -327,7 +330,7 @@ async Task<List<DipDupItem>> GetDipDupMetadata(int lastUpdateId, DipDupConfig di
};

return JsonSerializer.Deserialize<DipDupResponse<DipDupItem>>(
await res.Content.ReadAsStringAsync(), options).Data.Items;
Utf8.Parse(await res.Content.ReadAsStringAsync()), options).Data.Items;
}

async Task<List<DipDupItem>> GetDipDupMetadata<T>(Dictionary<(string, string), T> tokens, DipDupConfig dipDupConfig)
Expand All @@ -354,7 +357,7 @@ async Task<List<DipDupItem>> GetDipDupMetadata<T>(Dictionary<(string, string), T
Encoding.UTF8, "application/json"))).EnsureSuccessStatusCode();

var _items = JsonSerializer.Deserialize<DipDupResponse<DipDupItem>>(
await res.Content.ReadAsStringAsync(), options).Data.Items;
Utf8.Parse(await res.Content.ReadAsStringAsync()), options).Data.Items;

items.AddRange(_items.Where(x => tokens.ContainsKey((x.Contract, x.TokenId))));
if (_items.Count < dipDupConfig.SelectLimit) break;
Expand Down Expand Up @@ -386,7 +389,7 @@ async Task<int> SaveTokenMetadata(List<DipDupItem> items)
{
if (any) sql.AppendLine(",");
else any = true;
param.Add($"@p{j}", JsonSerializer.Serialize(item.Metadata, options).Replace("\u0000", string.Empty));
param.Add($"@p{j}", Regex.Replace(JsonSerializer.Serialize(item.Metadata, options), string.Empty));
sql.Append($"({contractId}, '{item.TokenId}', @p{j}::jsonb)");
}
}
Expand Down

0 comments on commit ff99ad0

Please sign in to comment.