Skip to content

Add Square Credentials Dynamic Validator#515

Merged
Bpendragon merged 12 commits into
mainfrom
users/bpendragon/AddStripeValidator
Jul 30, 2021
Merged

Add Square Credentials Dynamic Validator#515
Bpendragon merged 12 commits into
mainfrom
users/bpendragon/AddStripeValidator

Conversation

@Bpendragon

Copy link
Copy Markdown
Contributor
  • Adds a Dynamic Stripe Validator
  • Adds a DynamicStripeValidatopr_Test class as well.


const string uri = "https://connect.squareup.com/oauth2/token";
const string CodeNotFoundMessage = "Authorization code not found";
const string CodeForRequest = "123";

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. order by string length
  2. Code* starts with uppercase and uri lowercase. Make everything lowercase, pls #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in iteration 3

dict.Add("grant_type", "authorization_code");
dict.Add("code", CodeForRequest); // Needs some value for call to work.
dict.Add("client_id", id);
dict.Add("client_secret", secret);

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can auto-initialize and order by string length #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in iteration 3

.GetAwaiter()
.GetResult();

string res = response.Content.ReadAsStringAsync().Result; // Actually runs synchronously when "Result" is called.

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result

getawaiter().getresult()

also, change to content instead of res #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in iteration 3

{
case HttpStatusCode.OK:
{
return ValidationState.Authorized; // This should theoretically never be hit, a "code" of 123 is much too short. But if we do get a 200, it's a valid Credential.

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the comments before the return and break in multiple lines #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in iteration 3


// Credential not valid.
return ValidationState.Unauthorized;
}

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a new line here #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in iteration 3

{
return ReturnUnhandledException(ref message, e);
}

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra new line #Closed

var keyValuePairs = new Dictionary<string, string>();

SquareCredentialsValidator.IsValidDynamic(ref fingerprint,
ref message,

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                                                      [](http://example.com/codeflow?start=0&length=58)

align with the first argument #Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in iteration 4

.GetAwaiter()
.GetResult();

string res = response.Content.ReadAsStringAsync().Result; // Actually runs synchronously when "Result" is called.

@eddynaka eddynaka Jul 29, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res

you can move this to the unauthorized section to prevent this being used in a 404 (for example) #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in iteration 3


try
{
HttpClient client = CreateHttpClient();

@michaelcfanning michaelcfanning Jul 29, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateHttpClient

This code doesn't actually create a client, does it? i.e., we implemented a caching scheme underneath the covers?

If so, this is a bad name for an API. It should be CreateOrRetrieveCachedHttpClient or something... #Pending

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename in a new pr. but, yes, we should rename it :)

// This should theoretically never be hit, a "code" of 123 is much too short.
// But if we do get a 200, it's a valid Credential.
return ValidationState.Authorized;
}

@michaelcfanning michaelcfanning Jul 29, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to have the courage of your comment's convictions here. If this code should never be hit, you should be throwing an exception or throwing an error condition.

Since I agree with you this code shouldn't be hit, it seems like any execution through this code path, in fact, would be a bug and not flag an actual find.

In general, it doesn't help us to code defensively. Instead, aggressively fail unexpected code paths and our test process (if we are testing properly) should flush those out. #Resolved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My specific suggestion is to use unexpected response code for this code path. Because that's what it is, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, just going to remove the block entirely and we can have it fall to 'Unexpected Response Code'

.ReadAsStringAsync()
.GetAwaiter()
.GetResult();
if (content.Contains(codeNotFoundMessage))

@michaelcfanning michaelcfanning Jul 29, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codeNotFoundMessage

please add a blank vertical line before this if condition. #Closed

Dictionary<string, string> options,
ref ResultLevelKind resultLevelKind)
{
string id = fingerprint.Id;

@michaelcfanning michaelcfanning Jul 29, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fingerprint

It looks like this code doesn't return any asset information, i.e., the identifier for the compromised thing. Please see other rules for examples of proper reporting. I think 'id' is the thing we should be surfacing that we are not.

Please make sure to review actual user-facing strings returned by this change.

@eddynaka, enough is enough: we need proper unit and functional testing that forces testing through the dynamic validation piece. :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to report the id for this code path but can delay getting the additional testing out there. But the very next thing we should do is implement an approach for this.

Basically, for every dynamic validator, we need a way to exercise/validate all possible code paths. This will be tricky/likely require a mocked HTTP client/other things to accomplish.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added asset information to retrun messages.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an example of what the user-facing string looks like for authorized/unauthorized access? I think there's another overload for some helpers that allow you to provide a descriptor of the id, when helpful. to clarify the output. So looking at current example would help understand whether we need to do something like that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelcfanning

For successfully validating the response is

The compromised asset is 'a'

For a credential that doesn't validate it's

The provided secret is not authorized to access 'a'.

In both cases 'a' is replaced with the client_id of the Square app that was detected. (which normally looks like sq0idp-<alphanumeric-identifier>

Comment thread Src/Plugins/Tests.Security/Tests.Security.csproj Outdated

@eddynaka eddynaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@Bpendragon Bpendragon enabled auto-merge (squash) July 30, 2021 20:25
@Bpendragon Bpendragon merged commit 06ff25f into main Jul 30, 2021
@Bpendragon Bpendragon deleted the users/bpendragon/AddStripeValidator branch July 30, 2021 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants