[00:00] Here we have some JavaScript code that iterates over this test array and then just applies this makeFullName function to it. The newly created array, we're just going to have one key, FullName.
[00:16] And this is created by concatenating this firstName and lastName property. So if we look at our console, we'll see that displayNames is displaying this array for us.
[00:32] And then below displayNames, we see we have a group of log statements. And within this checking globals group, we're seeing the result of evaluating objects.isFrozen on the object and array prototype.
[00:51] Seeing as JavaScript allows developers to mutate global variables, we shouldn't be surprised to see each of these checks evaluating to false. And on that note, let's engage in some mutating ourselves.
[01:09] Here, I'm just going to extend the array prototype and reimplement the map method. So inside our function body, first we're going to initialize an empty array result.
[01:27] Next, we'll introduce our for loop that is going to iterate through every value in the array map is called on. So in the context of our function, the this keyword refers to our source array.
[01:46] So when setting the condition of our loop, we'll constrain i to be less than this.length.
[01:53] And then finally, we're going to use the increment operator as our loop's afterthought so that i will continue to grow up until it is equal to this.length.
[02:09] Inside the body of our loop, we're going to create a variable current. And using bracket notation, we'll just have this hold each element as we process the array. Directly below this, we'll use the push method to populate this result array.
[02:27] And here is where we're going to use our function's callback argument or CB as I've named it. And the last thing we'll do is just return this result array.
[02:41] Now if we save and look into our terminal, we'll see that our behavior has not changed at all. We get the same display names as we had before. We've achieved our desired outcome. And this is great. It's all sunshine and rainbows for us. Well, until it isn't.
[03:01] So the property of global mutability can cause the occurrence of prototype. This occurs when an attacker takes advantage of JavaScript's mute ability to pollute a prototype for the purpose of executing some sort of attack.
[03:17] To demonstrate this, I'm going to extend our map implementation so that it includes a second array. Secret result. As we loop through our code, secret result will be updated with the current element of the array as well as the newly transformed value.
[03:40] Now we can log out secret result and we'll see that we've successfully captured these values. We certainly don't want to show our hand here, so let's remove this log statement.
[03:55] And from here, we're going to grab this send data to secret server function. And now I'm going to change the return statement here so that it sends off our secret result using this send data to secret server function.
[04:17] Before returning the result array. Now we'll see that the display name is showing just like it was before, giving the user a false sense of confidence. Now we do have some logging going on, so we'll remove that.
[04:37] Okay, so now at the bottom of our log statements, I'm going to add an additional log. And here, I'm just going to use this secret database, which is just a JavaScript map.
[04:52] And what I'm going to do here is just get this data set one key, which is going to open up this secret database.
[05:05] Which is going to hold the data we've just sneakily gained access to. And I'll just name this first stolen data set. Okay, and we see that we have our array in here and I can just map over this array.
[05:32] And let's just quickly inspect the current and transform the values. And just like that, we've been able to extract information about Thomas Greco, C.D. Lamb, and Stuart Little without much work.
[05:49] On a more serious note, this can lead to extremely big problems. So, it's important that we remove any instances of global mutability whenever possible. Let's see how we can use the SESS library to accomplish this.
[06:08] So, atop the page, I'm just importing SESS, as I have it locally within my project. And right below that, I'm invoking the lockdown method.
[06:22] And as its name suggests, lockdown is going to lock down our JavaScript. The result, no more global mutability. So, if we save our file and we take a look in our console, we shouldn't be surprised to see that we now get an error.
[06:41] Specifically, this error is telling us that this property is not configurable. It's read-only. Okay, so, let's remove this custom map implementation.
[07:00] Now, if we look in our console, we'll see that display names is showing correctly. But, we still have an error at the bottom, and that's because first stolen dataset no longer exists. We've prevented this from ever being created.
[07:20] The last thing we want to feast our eyes on is just the fact that the object prototype, as well as the array prototype, is now frozen by default. All we had to do was invoke one method, lockdown.
[07:35] And in return, we've received a JavaScript environment in which all globals are now immutable.