Search code examples
winjswin-universal-app

Check URL exists in ApplicationContentUriRules


I have an app that is loading URLs into a WebView (x-ms-webview). When the user makes the request, I would like to compare the URL they are trying to load with the "whitelisted" URLs in the ApplicationContentUriRules and warn them if it is not there. Any ideas how I might accomplish this?


Solution

  • There isn't a direct API for pulling information out of the manifest, but there are options.

    First, you can just maintain an array of those same URIs in your code, because to change them you'd have to change the manifest and update your package anyway, so you would update the array to match. This would make it easy to check, but increase code maintenance.

    Such an array would also let you create a UI in which the user can enter only URIs that will work, e.g. you can offer possibilities from a drop-down list instead of letting the user enter anything.

    Second, you can read the manifest XML into a document directly and parse through it to get to the rules. Here's some code that will do that:

    var uri = new Windows.Foundation.Uri("ms-appx:///appxmanifest.xml");
    var doc;
    
    Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) {
        return Windows.Storage.FileIO.readTextAsync(file);                    
    }).done(function (text) {
        doc = new Windows.Data.Xml.Dom.XmlDocument();
        doc.loadXml(text);
    
        var acur = doc.getElementsByTagName("ApplicationContentUriRules");
        if (acur !== null) {
            var rules = acur[0].getElementsByTagName("Rule");
    
            for (var i = 0; i < rules.length; i++) {
                console.log(rules[i].getAttribute("Match") + " - " + rules[i].getAttribute("Type"));
            }
        }
    });
    

    You could probably just get "Rule" tags directly from the root doc, because I don't think anything else in the manifest uses that kind of node, but to future-proof it's better to get the ApplicationContentUriRules first. As you can see, the "Match" attribute is what holds the URI for that rule, but you also need to make sure that "Type" is "include" and not "exclude".