avatarTeri Radichel

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2629

Abstract

le;"</span>) <span class="hljs-comment"># Show the result.</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"The webpage's title is:"</span>, title) <span class="hljs-comment"># Close the driver.</span> driver.quit()</pre></div><p id="7c03">This script runs JavaScript to get the current webpage’s title, which is then stored in the Python variable <code>title</code> and printed. You can replace "return document.title;" with any JavaScript that returns data, which will then be passed back to your Python script.</p><p id="feca">Passing Data to JavaScript Arguments can be passed to the execute_script() function in Selenium with Python. These arguments are then available in your JavaScript code, accessed through arguments[index], with ‘index’ denoting the position of the argument.</p><div id="f021"><pre><span class="hljs-keyword">from</span> selenium <span class="hljs-keyword">import</span> webdriver <span class="hljs-comment"># Initialize the WebDriver, using ChromeDriver here.</span> driver = webdriver.Chrome(<span class="hljs-string">'/path/to/chromedriver'</span>) <span class="hljs-comment"># Go to a website.</span> driver.get(<span class="hljs-string">'http://example.com'</span>) <span class="hljs-comment"># Define a JavaScript script with parameters.</span> js_code = <span class="hljs-string">"return arguments[0] + arguments[1];"</span> <span class="hljs-comment"># Pass arguments to execute_script</span> result = driver.execute_script(js_code, <span class="hljs-number">10</span>, <span class="hljs-number">20</span>) <span class="hljs-comment"># Show the result.</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"The calculated result is:"</span>, result) <span class="hljs-comment"># End the WebDriver session.</span> driver.quit()</pre></div><p id="81b9">In this sample, the JavaScript code is programmed to sum two numbers, with 10 and 20 being passed as arguments. The result is then stored in <code>result</code> and displayed.</p><p id="3cb2">Direct insertion of variables into the JavaScript string is another method, though it requires careful handling of quotes and strings.</p><p id="dc38">Managing Complex JavaScript Objects The execute_script() function can also manage complex JavaScript objects. This is particularly useful for interacting with objects within the structure of a webpage, like in single-page applications (SPAs).</p><div id="7f18"><pre><span class="hljs-comment"># Example: Accessing a complex JavaScript object.</span> complex_object = driver.execute_script(<span class="hljs-string">"return window.someComplexObject;"</spa

Options

n>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Retrieved Complex Object:"</span>, complex_object)</pre></div><p id="0188">Working with Browser Storage execute_script() can also be used for interactions with local storage and session storage in a browser, which is beneficial for setting or retrieving items for testing.</p><div id="ef3f"><pre><span class="hljs-comment"># Add an item to local storage.</span> driver.execute_script(<span class="hljs-string">"localStorage.setItem('key', 'value');"</span>) <span class="hljs-comment"># Retrieve an item from local storage.</span> value = driver.execute_script(<span class="hljs-string">"return localStorage.getItem('key');"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Data from Local Storage:"</span>, value)</pre></div><p id="80f4">Running Asynchronous JavaScript The execute_script() method in Selenium can execute asynchronous JavaScript, crucial for handling AJAX calls or delayed operations.</p><div id="8c1f"><pre><span class="hljs-comment"># Example: Running asynchronous JavaScript</span> async_result = driver.execute_script(<span class="hljs-string">"return new Promise(resolve => setTimeout(() => resolve('Result after 2 seconds'), 2000));"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Asynchronous output:"</span>, async_result)</pre></div><p id="c824">By understanding and utilizing the execute_script() method in Selenium with Python, a wide range of web automation and testing tasks become feasible, surpassing the limitations of standard Selenium APIs. Mastering this function ensures more thorough and effective automated testing scenarios.</p><p id="34f7">I hope this note will help someone to be more productive at work. If it’s useful, give it a like and subscribe to my <a href="https://medium.com/@itwebmind.com/about">Medium</a> so you don’t miss out 😊</p><p id="8a5e">More useful articles on my website <a href="http://itwebmind.com/">ItWebMind.com</a>, <a href="https://www.facebook.com/itwebmind/">Facebook</a>, <a href="https://twitter.com/itwebmind">Twitter</a>, <a href="https://www.linkedin.com/company/itwebmind/">LinkedIn</a> and <a href="https://www.reddit.com/user/ITWEBMIND">Reddit</a>. You can support me on <a href="https://www.patreon.com/itwebmind">Patreon</a> or <a href="https://www.buymeacoffee.com/itwebmind">ByMeACoffee</a>. I published the original of this article here — «<a href="https://itwebmind.com/how-to-run-javascript-in-selenium-python">How to run JavaScript in Selenium Python, pass and get values from it</a>».</p></article></body>

Restricting Access to Call Lambda Functions to a VPC Endpoint in a Service Control Policy

ACM.314 An SCP policy with multiple conditions evaluated as NOT OR or NOR

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⚙️ Check out my series on Automating Cybersecurity Metrics | Code.

🔒 Related Stories: Lambda | Container Security | Application Security

💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the last post, I was attempting to use IP addresses to restrict access to using the AWS IP restrictions in a Service Control Policy. It worked for public IPs but not private IP addresses.

I felt like I was forgetting something (it happens) and sure enough while reading around I ran across this post:

Create an identity-based policy with the IAM aws:SourceIp and aws:ViaAWSService condition keys that denies access to all actions outside the specified IP address range. Only public IP addresses or public IP ranges are supported. The aws:SourceIp condition key is always included in the request, except for requests that use [include] an Amazon VPC endpoint.

That last line isn’t true, because the SourceIP exists in the CloudWatch data events we were looking at in the last post, which made this entirely confusing.

But ah, yes, now I remember. When we added VPC endpoints to S3 buckets we had to restrict to the VPC Endpoint in the request. That really should be called out in this post, which shows private IPs in the policy but that won’t work when the traffic is coming from a VPC Endpoint.

Sent feedback on that.

Meanwhile, I found this post that has all available policy condition context keys, which is helpful.

There’s a key for source VPC endpoint:

From the linked documentation our policy condition will look like this:

Let’s see if our data event request has a VPC endpoint in it. Yes it does.

I’m going to add the VPCE condition to my policy along with the IP address restriction and test to see that it works.

How can we add multiple conditions to a policy that say deny the action if it is not this OR that?

When multiple values are specified for a single context key in a policy with negated matching condition operators, the effective permissions work like a logical NOR. In negated matching, a logical NOR or NOT OR returns true only if all values evaluate to false.

Multiple conditions are added to a policy in this format:

After adding that condition, I can both invoke the Lambda function from the EC2 instance in the VPC by way of the VPC Endpoint. I can also still view the Lambda function in the console and test it there by way of the public IP address. That is obviously not my public IP below. Also notice I have two VPC endpoints in the policy, as I expect to be calling functions from other functions in the future.

Why was this important? The Lambda functions I’m going to invoke are going to have credentials and access to my GitHub repositories. Now I have a way to restrict access both by IP address and a secret access token. I can still do one other thing to limit access to execution of the Lambda function when using MFA and updating secrets only when hardware MFA is in use.

If you are having problems with your VPC Endpoint check out these posts:

Follow for updates.

Teri Radichel | © 2nd Sight Lab 2023

About Teri Radichel:
~~~~~~~~~~~~~~~~~~~~
⭐️ Author: Cybersecurity Books
⭐️ Presentations: Presentations by Teri Radichel
⭐️ Recognition: SANS Award, AWS Security Hero, IANS Faculty
⭐️ Certifications: SANS ~ GSE 240
⭐️ Education: BA Business, Master of Software Engineering, Master of Infosec
⭐️ Company: Penetration Tests, Assessments, Phone Consulting ~ 2nd Sight Lab
Need Help With Cybersecurity, Cloud, or Application Security?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🔒 Request a penetration test or security assessment
🔒 Schedule a consulting call
🔒 Cybersecurity Speaker for Presentation
Follow for more stories like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
❤️ Sign Up my Medium Email List
❤️ Twitter: @teriradichel
❤️ LinkedIn: https://www.linkedin.com/in/teriradichel
❤️ Mastodon: @teriradichel@infosec.exchange
❤️ Facebook: 2nd Sight Lab
❤️ YouTube: @2ndsightlab
Vpc Endpoint
Private
IP
Lambda
Policy
Recommended from ReadMedium