Skip to content

Commit f0a0bd9

Browse files
authored
Not using Path.Combine for joining path segments anymore. (#10781)
1 parent 9fccab4 commit f0a0bd9

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

src/GitHubVulnerabilities2v3/Extensions/BlobStorageVulnerabilityWriter.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private async Task RunUpdate(string stringContentOutput, string currentTime)
308308
var baseFolder = pathParts[pathParts.Length - 2];
309309

310310
// Start special case block
311-
var currentBaseContentUri = _primaryStorage.ResolveUri(Path.Combine(baseFolder, _configuration.BaseFileName));
311+
var currentBaseContentUri = _primaryStorage.ResolveUri(UrlPathCombine(baseFolder, _configuration.BaseFileName));
312312
var currentBaseContent = await _primaryStorage.LoadString(currentBaseContentUri, CancellationToken.None);
313313
var baseContentObject = JsonConvert.DeserializeObject<Dictionary<string, List<Advisory>>>(currentBaseContent);
314314

@@ -334,7 +334,7 @@ private async Task RunUpdate(string stringContentOutput, string currentTime)
334334

335335
_logger.LogInformation("Writing update to files");
336336
var primaryIndexContent = new StringStorageContent(JsonConvert.SerializeObject(indexEntries), contentType: JsonContentType, cacheControl: _configuration.IndexCacheControlHeader);
337-
var updateStorageUri = _primaryStorage.ResolveUri(Path.Combine(baseFolder, currentTime, _configuration.UpdateFileName));
337+
var updateStorageUri = _primaryStorage.ResolveUri(UrlPathCombine(baseFolder, currentTime, _configuration.UpdateFileName));
338338
var updateContent = new StringStorageContent(stringContentOutput, contentType: JsonContentType, cacheControl: _configuration.UpdateCacheControlHeader);
339339

340340
await _primaryStorage.Save(updateStorageUri, updateContent, overwrite: true, CancellationToken.None);
@@ -358,7 +358,7 @@ private async Task RunUpdate(string stringContentOutput, string currentTime)
358358
}
359359
}
360360
var secondaryIndexContent = new StringStorageContent(JsonConvert.SerializeObject(indexEntries), contentType: JsonContentType, cacheControl: _configuration.IndexCacheControlHeader);
361-
updateStorageUri = _secondaryStorage.ResolveUri(Path.Combine(baseFolder, currentTime, _configuration.UpdateFileName));
361+
updateStorageUri = _secondaryStorage.ResolveUri(UrlPathCombine(baseFolder, currentTime, _configuration.UpdateFileName));
362362
var secondaryIndexStorageUri = _secondaryStorage.ResolveUri(_configuration.IndexFileName);
363363

364364
await _secondaryStorage.Save(updateStorageUri, updateContent, overwrite: true, CancellationToken.None);
@@ -427,8 +427,8 @@ private async Task RunRegenerate(string stringContentOutput, string currentTime)
427427
var indexContent = new StringStorageContent(JsonConvert.SerializeObject(indexEntries), contentType: JsonContentType, cacheControl: _configuration.IndexCacheControlHeader);
428428

429429
var primaryIndexStorageUri = _primaryStorage.ResolveUri(_configuration.IndexFileName);
430-
var baseStorageUri = _primaryStorage.ResolveUri(Path.Combine(currentTime, _configuration.BaseFileName));
431-
var updateStorageUri = _primaryStorage.ResolveUri(Path.Combine(currentTime, currentTime, _configuration.UpdateFileName));
430+
var baseStorageUri = _primaryStorage.ResolveUri(UrlPathCombine(currentTime, _configuration.BaseFileName));
431+
var updateStorageUri = _primaryStorage.ResolveUri(UrlPathCombine(currentTime, currentTime, _configuration.UpdateFileName));
432432

433433
await _primaryStorage.Save(baseStorageUri, baseContent, overwrite: true, CancellationToken.None);
434434
await _primaryStorage.Save(updateStorageUri, updateContent, overwrite: true, CancellationToken.None);
@@ -442,8 +442,8 @@ private async Task RunRegenerate(string stringContentOutput, string currentTime)
442442

443443
_logger.LogInformation("Writing regenerated files to secondary storage");
444444
var secondaryIndexStorageUri = _secondaryStorage.ResolveUri(_configuration.IndexFileName);
445-
var secondaryBaseStorageUri = _secondaryStorage.ResolveUri(Path.Combine(currentTime, _configuration.BaseFileName));
446-
var secondaryUpdateStorageUri = _secondaryStorage.ResolveUri(Path.Combine(currentTime, currentTime, _configuration.UpdateFileName));
445+
var secondaryBaseStorageUri = _secondaryStorage.ResolveUri(UrlPathCombine(currentTime, _configuration.BaseFileName));
446+
var secondaryUpdateStorageUri = _secondaryStorage.ResolveUri(UrlPathCombine(currentTime, currentTime, _configuration.UpdateFileName));
447447

448448
await _secondaryStorage.Save(secondaryBaseStorageUri, baseContent, overwrite: true, CancellationToken.None);
449449
await _secondaryStorage.Save(secondaryUpdateStorageUri, updateContent, overwrite: true, CancellationToken.None);
@@ -454,5 +454,27 @@ private async Task RunRegenerate(string stringContentOutput, string currentTime)
454454
_cursor.Value = _firstVulnWrittenTimestamp;
455455
await _cursor.Save(CancellationToken.None);
456456
}
457+
458+
private static string UrlPathCombine(params string[] segments)
459+
{
460+
StringBuilder sb = new StringBuilder();
461+
foreach (var segment in segments)
462+
{
463+
if (!string.IsNullOrEmpty(segment))
464+
{
465+
if (segment.Contains("/") || segment.Contains("\\"))
466+
{
467+
throw new ArgumentException($"Path segments must not contain '/' or '\\' characters (segment: {segment}).", nameof(segments));
468+
}
469+
470+
if (sb.Length > 0)
471+
{
472+
sb.Append('/');
473+
}
474+
sb.Append(segment);
475+
}
476+
}
477+
return sb.ToString();
478+
}
457479
}
458480
}

0 commit comments

Comments
 (0)