Workbox Service Worker - Ignore external URL for caching? - progressive-web-apps

I have a fairly simple service worker that will cache my requests and use StaleWhileRevalidate.
I have a handful of urls that will use NetworkOnly which does what I need.
workbox.routing.registerRoute('/login/*', new workbox.strategies.NetworkOnly());
I have an ad file that is needed to show ads on the site. I don't want this to be cached so I've added a new route:
workbox.routing.registerRoute('https://s.nitropay.com/ads-757.js', new workbox.strategies.NetworkOnly());
However this causes issue with the service worker and no matter what workbox strategy I use for this, I will cause an error:
Uncaught (in promise) no-response: no-response :: [{"url":"https://s.nitropay.com/ads-757.js"}]
at Object.handle (https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-strategies.prod.js:1:1881)
This causes me to have no ads after the page is cached by the service worker.
How can I exclude this URL from being included in the service worker?
pwabuilder-sw.js file:
// This is the service worker with the Cache-first network
const CACHE = "pwabuilder-precache";
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js');
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
workbox.routing.registerRoute('https://s.nitropay.com/ads-752.js', new workbox.strategies.NetworkOnly());
workbox.routing.registerRoute('/login/*', new workbox.strategies.NetworkOnly());
workbox.routing.registerRoute('/profile/*', new workbox.strategies.NetworkOnly());
workbox.routing.registerRoute('/logout.php', new workbox.strategies.NetworkOnly());
workbox.routing.registerRoute('/edit-deck/*', new workbox.strategies.NetworkOnly());
workbox.routing.registerRoute('/deckbuilder/*', new workbox.strategies.NetworkOnly());
workbox.routing.registerRoute(
new RegExp('/*'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: CACHE
})
);

Related

Using Nuget.Protocol to delete a package from Azure DevOps

I'm writing a simple application to implement package retention in our AzureDevOps NuGet feed, but I can't delete a package -- nothing happens.
(I want to keep all release packages, but delete all pre-release packages for branches that nolonger exist and keep only the latest three pre-release packages for branches that do exist.)
// Log in and whatnot.
PackageSource packageSource = new PackageSource("https://pkgs.dev.azure.com/Organisation/_packaging/Organisation/nuget/v3/index.json")
{
Credentials = new PackageSourceCredential(
source: "https://pkgs.dev.azure.com/Organisation/_packaging/Organisation/nuget/v3/index.json",
username: "rwb#organisation.co.uk",
passwordText: _Pat,
isPasswordClearText: true,
validAuthenticationTypesText: null)
};
NuGet.Protocol.Core.Types.SourceRepository repository = NuGet.Protocol.Core.Types.Repository.Factory.GetCoreV3(packageSource);
PackageSearchResource packageSearch = repository.GetResourceAsync<NuGet.Protocol.Core.Types.PackageSearchResource>().Result;
PackageUpdateResource packageUpdate = repository.GetResourceAsync<PackageUpdateResource>().Result;
// Delete a package -- doesn't work!
packageUpdate.Delete("Organisation.MyDll", "1.0.1-branch.build", packageSource => _Pat, packageSource => true, false, _Logger);
The .Delete doesn't throw an exception or log any error, but the package remains in AzureDevOps.
I think I need to add .Wait() because of the async nonsense, but even so: the packages appear srikethrough in the Azure DevOps website, but not in the feed's recycle bin, and they're still listed in NuGet package manager in VisualStudio.
How do you actually delete the things?
Update
So apparently the C# only delists and you have to use the HTTP API directly to actually delete. However, it's impossible to authenticate.
private static void Delete(NuGet.Packaging.Core.PackageIdentity packageIdentity)
{
// https://learn.microsoft.com/en-us/nuget/api/package-publish-resource
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-NuGet-ApiKey", "VSTS");
//httpClient.DefaultRequestHeaders.Add("X-NuGet-ApiKey", _Pat);
//httpClient.DefaultRequestHeaders.Add("X-NuGet-ApiKey", "rwb#organisation.co.uk:" + _Pat);
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", ":" + _Pat);
//httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", "rwb#organisation.co.uk:" + _Pat);
//httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Pat);
// https://learn.microsoft.com/en-us/rest/api/azure/devops/artifactspackagetypes/nuget/delete-package-version?view=azure-devops-rest-6.0
HttpResponseMessage rx = httpClient.DeleteAsync($"https://pkgs.dev.azure.com/Organisation/_apis/packaging/feeds/FeedName/nuget/packages/{packageIdentity.Id}/versions/{packageIdentity.Version}?api-version=6.0-preview.1").Result;
}
I had exactly the same issue that the standard NuGet delete will only delist the package.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", _Pat))));
What worked for me (authentication-wise) was the ToBase64String conversion.

How to redirect requests to another host using ZAP?

I'm new to ZAP and I don't know much about it's js/ecma scripting.
Basically, I was trying to redirect request to another host.
Say an application that is connected to the ZAP proxy makes a request in a URL:
http://www.somesite.com/path/to/a/file
but I want to change the hostname in the URL to:
another.site.com
so it will actually request to: http://www.anothersite.com/path/to/a/file
Here's the code that I was trying to work but the URL remains unchanged in the request.
function proxyRequest(msg) {
// Debugging can be done using println like this
var uri = msg.getRequestHeader().getURI().toString()
var host = msg.getRequestHeader().getURI().getHost().toString()
print('proxyResponse called for url=' + uri)
if (host == 'download.qt.io') {
uri = uri.replace('download.qt.io/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
if (host == 'ftp.jaist.ac.jp') {
uri = uri.replace('ftp.jaist.ac.jp/pub/qtproject/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
if (host == 'qtproject.mirror.liquidtelecom.com') {
uri = uri.replace('qtproject.mirror.liquidtelecom.com/online/', 'mirrors.ocf.berkeley.edu/qt/online/')
msg.getRequestHeader().setHeader('Location', uri)
print('proxyRequest changed to url=' + uri)
}
return true
}
Option 1: Replacer Rule
Install the Replacer addon, from the marketplace:
Goto the Tools menu and select 'Replacer Options'.
Setup a rule as shown in the following screenshot.
Save/Okay as appropriate.
Now when your browse etc all your traffic will be redirected/rewritten.
Option 2: HttpSender Script
Create a new HttpSender script, similar to the following example:
function sendingRequest(msg, initiator, helper) {
var host = msg.getRequestHeader().getURI().getHost();
if (host.equals("www.somesite.com")) {
uri = msg.getRequestHeader().getURI();
uri.setEscapedAuthority("www.anothersite.com");
msg.getRequestHeader().setURI(uri);
}
return msg;
}
function responseReceived(msg, initiator, helper) {}
Option 3: Hosts File Entry
Goto a command prompt and nslookup www.somesite.com, note the IP address (w.x.y.z).
In your hosts file, add an entry associating the noted IP (w.x.y.z) with www.anothersite.com.
(You may need to restart ZAP/browsers for this change to take effect. On linux you'll likely need to sudo to edit the file, on Windows you'll need to edit it as an admin user.)
(Further details WRT editing your hosts file: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/)

Calling External WCF Service (using generated client) from CRM sandboxed plugin OnPremise is failing

How to call HTTPS WCF web service in Plugin, plugin assembly is registered in sandbox mode. I am getting System.Security.SecurityException exception, Can somebody please provide the way to all https web service. My code is below :
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.MaxReceivedMessageSize = Int32.MaxValue;
myBinding.Name = “basicHttpBinding”;
if (EndPoint.ToLower().Contains(“https://”))
{
//Throwing exception here – System.Security.SecurityException exception,
ServicePointManager.ServerCertificateValidationCallback += (sendr, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072 | (SecurityProtocolType)192;
myBinding.Security.Mode = BasicHttpSecurityMode.Transport;
}
else
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
myBinding.Security.Mode = BasicHttpSecurityMode.None;
}
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endPointAddress = new EndpointAddress(EndPoint);
WebIALClient myClient = new WebIALClient(myBinding, endPointAddress)
Since you are in on-premise version, you can register the plugin assembly in non-sandbox mode. ie Isolation mode = none to overcome such errors.
In case you wanted to use sandbox mode, try using WebClient class for invoking WCF service call. Read more
using (WebClient client = new WebClient())
{
byte[] responseBytes = client.DownloadData(webAddress);
string response = Encoding.UTF8.GetString(responseBytes);
tracingService.Trace(response);
// For demonstration purposes, throw an exception so that the response
// is shown in the trace dialog of the Microsoft Dynamics CRM user interface.
throw new InvalidPluginExecutionException("WebClientPlugin completed successfully.");
}
Can you try and also include: using System.Web.Http.Cors;
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/ConvertUpload/{env}/{id}")]
public string Get(string env, string id)
{
return "hi";
}
You may have to use WebClient as #Arun has mentioned.

Selenium WebDriver, when loading an HTTPS website, closes and reopens browser and does not even attempt to interact with the DOM

Selenium WebDriver (run from Eclipse Oxygen.3a Release (4.7.3a)) seems to be unable to handle any HTTPS website.
By that I mean this: upon loading the page (regardless of whether it redirects or not, tried with gitlab and AWS logon sites)
Logon page loads properly (no SSL certificate issues, any redirect happens just fine), yet Selenium doesn't do anything (debug mode shows it's not even trying to execute the logon code)
It times out (browserWaitTimeout=15) and closes/reopens the logon page
This happens until the 4th time, upon which Selenium finally reaches my logon code (a breakpoint I set there is finally hit), but this happens between browser page closing and reopening, thus it never detects the DOM and fails.
And the most frustrating thing is, console shows NO ERRORS.
Here: Chrome:
Jul 03, 2019 8:40:44 AM com.fincad.vcr.qa.support.WebDriverFactory createWebDriver
INFO: Web driver is created successfully
Jul 03, 2019 8:40:55 AM com.fincad.vcr.qa.support.WebDriverFactory quitWebDriver
INFO: Web driver quits successfully
That's it, as in literally it (notice the 15 second gap? That's browserWaitTimeout).
a) I tried FF, where I saw errors from Marionette:
1562172157538 Marionette INFO Listening on port 56792
1562172157572 addons.xpi-utils DEBUG Successfully read XPI database
1562172157603 addons.manager DEBUG Registering upgrade listener for
formautofill#mozilla.org
Jul 03, 2019 9:42:37 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Jul 03, 2019 9:42:38 AM com.fincad.vcr.qa.support.WebDriverFactory createWebDriver
INFO: Web driver is created successfully
1562172170219 Marionette INFO Stopped listening on port 56792
1562172170243 addons.xpi DEBUG Calling bootstrap method shutdown on webcompat#mozilla.org version 4.0.0
1562172170249 addons.xpi DEBUG Calling bootstrap method shutdown on screenshots#mozilla.org version 37.1.0
1562172170253 addons.xpi DEBUG Calling bootstrap method shutdown on fxmonitor#mozilla.org version 3.0
1562172170254 addons.xpi DEBUG Calling bootstrap method shutdown on formautofill#mozilla.org version 1.0
[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Child 21788, Chrome_ChildThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Child 21788, Chrome_Chi[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Child 6644, Chrome_ChildThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Child 6644, Chrome_ChildThread] WAR[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Child 17844, Chrome_ChildThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Child 17844, Chrome_ChildThread] WARNING: pipe er[Parent 12824, Gecko_IOThread] WARNING: pipe error: 109: file z:/task_1560988628/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 341
[Chi###!!! [Parent][MessageChannel] Error: (msgtype=0x1F0099,name=PBrowser::Msg_UpdateNativeWindowHandle) Closed channel: cannot send/recv
I googled and people mentioned it's due to outdated FF driver, so I upgraded, but it didn't do any good (same errors appear). And in IE it's like Chrome: no error or warning msg at all.
My drivers are these:
Selenium WebDriver: 3.14.0 (32-bit)
geckodriver 0.24.0 (32-bit)
chromedriver 74.0.3729.6 (32-bit)
IEDriverServer 3.8.0 (32-bit)
And my browsers:
Chrome browser: 75.0.3770.100
FF browser: 67.0.4
IE browser: 11.557.17763
This is how I create my drivers:
private static void createFirefoxDriver() {
setCapability("firefox");
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
GeckoDriverService service = builder.build();
FirefoxOptions options = new FirefoxOptions(capabilities);
// Read the default firefox profile
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myProfile = profile.getProfile("default");
// Disable the web page from asking if really want to leave
myProfile.setPreference("dom.disable_beforeunload", true);
options.setProfile(myProfile);
driver = new FirefoxDriver(service, options);
}
private static void createChromeDriver() {
setCapability("chrome");
ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
ChromeDriverService service = builder.build();
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);
driver = new ChromeDriver(service, options);
}
This is how I load the URL (local webdriver):
private static void createLocalWebDriver() {
String browser = ConfigParser.getBrowser();
LOGGER.info("Target Browser: " + browser);
switch (browser) {
case "firefox":
System.setProperty("webdriver.gecko.driver", ConfigParser.getGeckoDriver());
System.out.println("GeckoDriver on: " + ConfigParser.getGeckoDriver());
createFirefoxDriver();
break;
case "chrome":
System.setProperty("webdriver.chrome.driver", ConfigParser.getChromeDriver());
System.out.println("ChromeDriver on: " + ConfigParser.getChromeDriver());
createChromeDriver();
break;
case "ie":
System.setProperty("webdriver.ie.driver", ConfigParser.getIEDriverServer());
System.out.println("IEDriver on: " + ConfigParser.getIEDriverServer());
createIEDriver();
break;
default:
LOGGER.warning("Unsupported Browser: " + browser);
break;
}
}
private static void createFirefoxDriver() {`enter code here`
setCapability("firefox");
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
GeckoDriverService service = builder.build();
FirefoxOptions options = new FirefoxOptions(capabilities);
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myProfile = profile.getProfile("default");
myProfile.setPreference("dom.disable_beforeunload", true);
options.setProfile(myProfile);
driver = new FirefoxDriver(service, options);
}
private static void createChromeDriver() {
setCapability("chrome");
ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
ChromeDriverService service = builder.build();
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);
driver = new ChromeDriver(service, options);
}
private static void createIEDriver() {
setCapability("ie");
InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder();
InternetExplorerDriverService service = builder.build();
InternetExplorerOptions options = new InternetExplorerOptions(capabilities);
driver = new InternetExplorerDriver(service, options);
CommonJS.executeScript(driver, "window.localStorage.clear();");
CommonJS.executeScript(driver, "window.sessionStorage.clear();");
}
private static void setCapability(String browser) {
capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, true);
if (browser.equalsIgnoreCase("ie")) {
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
capabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, ConfigParser.getAppUrl());
} else if (browser.equalsIgnoreCase("chrome")) {
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
}
}
Sorry for all the code, but I'm sure I'll be asked about those details, hence my putting there.
Long story short:
This only happens with HTTPS URL's
Non HTTPS URL's work just fine
It's not an SSL certificate issue otherwise I would have seen it on the screen "This connection is untrusted".
It was a problem at page load code. Found a section that was explicitly expecting HTTP only, thus refusing to load any HTTPS URL.

fulljid is empty after connection to BOSH service with XMPHP

I am trying to pre-bind an XMPP session via XMPHP and pass the rid/sid/jid to a strophe client to attach to the session.
connection code here:
$conn = new CIRCUIT_BOSH('server.com', 7070, $username, $pass, $resource, 'server.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);
$conn->autoSubscribe();
try{
$conn->connect('http://xmpp.server.com/http-bind', 1, true);
$log->lwrite('Connected!');
}catch(XMPPHP_Exception $e){
die($e->getMessage());
}
I am getting the rid and sid but the fulljid in the $conn object stays empty and I cant see a session started on my openfire admin console.
If I create the jid manually by using the given resource and passing jid/rid/sid to strophe to use in attach, I get the ATTACHED status and I see calls from the client to the BOSH ip but I still dont see a session and I cant use the connection.
Strophe Client Code:
Called on document ready:
var sid = $.cookie('sid');
var rid = $.cookie('rid');
var jid = $.cookie('jid');
$(document).trigger('attach', {
sid: sid,
rid: rid,
jid: jid,
});
$(document).bind('attach', function (ev, data) {
var conn = new Strophe.Connection(
"http://xmpp.server.com/http-bind");
conn.attach(data.jid, data.sid, data.rid, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
} else if (status === Strophe.Status.ATTACHED){
$(document).trigger('attached');
}
});
Object.connection = conn;
});
I think the problem starts on the XMPPHP side which is not creating the session properly.
'attached' is triggered but never 'connected', is status 'connected' supposed to be sent?
What am I missing?
Ok, solved, I saw that XMPPHP lib didn't create a session at all on the openfire server, so I wrote a simple test for the XMPP class which was good and created the session, and for the XMPP_BOSH class that didn't manage create one. Then I saw the issue report here: http://code.google.com/p/xmpphp/issues/detail?id=47 comment no.9 worked, it fixed the issue by copying the processUntil() function from the XMLStream.php to BOSH.php, still can't figure out why this is working. Then I found I had an overlapping bug also with some of the passwords set for users on the openfire server. These passwords contained these ! # % ^ characters, for some reason the XMPP_BOSH is sending the password corrupted or changed so I got Auth Failed exception. Changing the password fixed the issue and I can now attach to the session XMPPHP created with the Strophe.js library.