Skip to content

Commit

Permalink
fix: Custom Endpoint URL crash [HEAD-536] (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asaf Agami authored Jul 27, 2023
1 parent 0c6ba0c commit e3c60f9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Snyk Changelog

## [1.1.38]

### Fixed
- Custom endpoints validation in the settings page did not work correctly and led to crashes.

## [1.1.37]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Snyk.VisualStudio.Extension.Shared/CLI/SnykCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public string GetApiToken()
}
catch (InvalidTokenException e)
{
Logger.Error(e, "Error on get api token via cli for settings");
Logger.Warning(e, "Failed to retrieve api token from CLI configuration");

apiToken = string.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Data.SqlClient;

namespace Snyk.VisualStudio.Extension.Shared.Settings;

using System.Windows.Forms;

public static class ErrorProviderExtensions
{
public static void SetNoError(this ErrorProvider errorProvider, Control control) =>
errorProvider.SetError(control, string.Empty);
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public string CustomEndpoint
get => this.customEndpoint;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
{
Logger.Warning("Custom endpoint value is not a well-formed URI. Setting custom endpoint to empty string");
value = string.Empty;
}

if (this.customEndpoint == value)
{
return;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,22 @@ private async Task AuthenticateButtonClickAsync()
}
}

private bool IsValidUrl(string url) => Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
private bool IsValidUrl(string url) => Uri.IsWellFormedUriString(url, UriKind.Absolute);

private void TokenTextBox_TextChanged(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.Enabled);

this.OptionsDialogPage.SetApiToken(this.tokenTextBox.Text);
if (this.ValidateChildren(ValidationConstraints.Enabled))
{
this.OptionsDialogPage.SetApiToken(this.tokenTextBox.Text);
}
}

private void CustomEndpointTextBox_LostFocus(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.Enabled);

this.OptionsDialogPage.CustomEndpoint = this.customEndpointTextBox.Text;
if (this.ValidateChildren(ValidationConstraints.Enabled))
{
this.OptionsDialogPage.CustomEndpoint = this.customEndpointTextBox.Text;
}
}

private void OrganizationTextBox_TextChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -286,27 +288,15 @@ private void TokenTextBox_Validating(object sender, System.ComponentModel.Cancel

private void CustomEndpointTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs cancelEventArgs)
{
if (string.IsNullOrEmpty(this.tokenTextBox.Text))
if (string.IsNullOrWhiteSpace(this.customEndpointTextBox.Text) || this.IsValidUrl(this.customEndpointTextBox.Text))
{
this.errorProvider.SetError(this.tokenTextBox, string.Empty);

this.errorProvider.SetNoError(this.customEndpointTextBox);
return;
}

if (!string.IsNullOrWhiteSpace(this.customEndpointTextBox.Text) && !this.IsValidUrl(this.customEndpointTextBox.Text))
{
cancelEventArgs.Cancel = true;

this.customEndpointTextBox.Focus();

this.errorProvider.SetError(this.customEndpointTextBox, "Not valid URL.");
}
else
{
cancelEventArgs.Cancel = false;

this.errorProvider.SetError(this.customEndpointTextBox, string.Empty);
}
cancelEventArgs.Cancel = true;
//this.customEndpointTextBox.Focus();
this.errorProvider.SetError(this.customEndpointTextBox, "Needs to be a full absolute well-formed URL (including protocol)");
}

private void SnykGeneralSettingsUserControl_Load(object sender, EventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Service\OssScanException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Service\OssService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Service\WorkspaceTrustService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Settings\ErrorProviderExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Settings\IUserStorageSettingsService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Settings\SnykGeneralOptionsDialogPage.cs">
<SubType>Component</SubType>
Expand Down

0 comments on commit e3c60f9

Please sign in to comment.