Use of msapp.execunsafelocalfunction¶
Scope and lifecycle¶
MSApp.execUnsafeLocalFunction belongs to the legacy Windows Store JavaScript application environment, not ordinary modern browser applications. It temporarily bypasses the local context's script-injection checks for the callback. Microsoft's archived API documentation explains the narrow runtime scope.
The API is not automatically an exploit: risk depends on the operations and data inside the callback. Passing untrusted strings to HTML-parsing sinks is particularly dangerous. The WinJS project states that it is not adding features and limits its work to substantial fixes for existing deployments. Plan migration with that maintenance status in mind.
Matched label example¶
Assume message is an untrusted string and container is the existing label container. The feature needs text, not user-authored HTML.
Unsafe in the legacy runtime: bypass checks for HTML insertion.
MSApp.execUnsafeLocalFunction(function () {
container.innerHTML = '<p>' + message + '</p>';
});
Safer: construct the element without a bypass or HTML parsing.
var paragraph = document.createElement('p');
paragraph.textContent = message;
container.appendChild(paragraph);
The replacement uses fixed element structure and a text assignment. createElement alone is not a general sanitizer: later unsafe URL, event-handler or innerHTML assignments can reintroduce risk. Keep attributes application-controlled or validate them for their specific purpose. Review textContent behavior.
Regression check¶
In an appropriate legacy application test environment, verify a harmless <em>sample</em> label displays literally and the feature works without the bypass. A modern browser cannot validate the removed host environment's security checks; test the migrated DOM behavior separately. See HTML injection and document.write migration.