1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // This component is used for handling dragover and drop of urls.
7 // It checks to see whether a drop of a url is allowed. For instance, a url
8 // cannot be dropped if it is not a valid uri or the source of the drag cannot
9 // access the uri. This prevents, for example, a source document from tricking
10 // the user into dragging a chrome url.
12 export function ContentAreaDropListener() {}
14 ContentAreaDropListener.prototype = {
15 classID: Components.ID("{1f34bc80-1bc7-11d6-a384-d705dd0746fc}"),
16 QueryInterface: ChromeUtils.generateQI(["nsIDroppedLinkHandler"]),
18 _addLink(links, url, name, type) {
19 links.push({ url, name, type });
22 _addLinksFromItem(links, dt, i) {
23 let types = dt.mozTypesAt(i);
26 type = "text/uri-list";
27 if (types.contains(type)) {
28 data = dt.mozGetDataAt(type, i);
30 let urls = data.split("\n");
31 for (let url of urls) {
32 // lines beginning with # are comments
33 if (url.startsWith("#")) {
36 url = url.replace(/^\s+|\s+$/g, "");
37 this._addLink(links, url, url, type);
43 type = "text/x-moz-url";
44 if (types.contains(type)) {
45 data = dt.mozGetDataAt(type, i);
47 let lines = data.split("\n");
48 for (let i = 0, length = lines.length; i < length; i += 2) {
49 this._addLink(links, lines[i], lines[i + 1], type);
55 for (let type of ["text/plain", "text/x-moz-text-internal"]) {
56 if (types.contains(type)) {
57 data = dt.mozGetDataAt(type, i);
59 let lines = data.replace(/^\s+|\s+$/gm, "").split("\n");
64 // For plain text, there are 2 cases:
65 // * if there is at least one URI:
66 // Add all URIs, ignoring non-URI lines, so that all URIs
67 // are opened in tabs.
68 // * if there's no URI:
69 // Add the entire text as a single entry, so that the entire
72 // We don't care whether we are in a private context, because we are
73 // only using fixedURI and thus there's no risk to use the wrong
76 Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
77 Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
78 for (let line of lines) {
79 let info = Services.uriFixup.getFixupURIInfo(line, flags);
81 // Use the original line here, and let the caller decide
82 // whether to perform fixup or not.
84 this._addLink(links, line, line, type);
89 this._addLink(links, data, data, type);
96 // For shortcuts, we want to check for the file type last, so that the
97 // url pointed to in one of the url types is found first before the file
98 // type, which points to the actual file.
100 if (files && i < files.length) {
103 PathUtils.toFileURI(files[i].mozFullPath),
105 "application/x-moz-file"
112 for (let i = 0; i < dt.mozItemCount; i++) {
113 this._addLinksFromItem(links, dt, i);
118 _validateURI(dataTransfer, uriString, disallowInherit, triggeringPrincipal) {
123 // Strip leading and trailing whitespace, then try to create a
124 // URI from the dropped string. If that succeeds, we're
125 // dropping a URI and we need to do a security check to make
126 // sure the source document can load the dropped URI.
127 uriString = uriString.replace(/^\s*|\s*$/g, "");
129 // Apply URI fixup so that this validation prevents bad URIs even if the
130 // similar fixup is applied later, especialy fixing typos up will convert
132 // We don't know if the uri comes from a private context, but luckily we
133 // are only using fixedURI, so there's no risk to use the wrong search
136 Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
137 Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
138 let info = Services.uriFixup.getFixupURIInfo(uriString, fixupFlags);
139 if (!info.fixedURI || info.keywordProviderName) {
140 // Loading a keyword search should always be fine for all cases.
143 let uri = info.fixedURI;
145 let secMan = Services.scriptSecurityManager;
146 let flags = secMan.STANDARD;
147 if (disallowInherit) {
148 flags |= secMan.DISALLOW_INHERIT_PRINCIPAL;
151 secMan.checkLoadURIWithPrincipal(triggeringPrincipal, uri, flags);
153 // Once we validated, return the URI after fixup, instead of the original
158 _getTriggeringPrincipalFromDataTransfer(
160 fallbackToSystemPrincipal
162 let sourceNode = aDataTransfer.mozSourceNode;
165 (sourceNode.localName !== "browser" ||
166 sourceNode.namespaceURI !==
167 "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
169 // Use sourceNode's principal only if the sourceNode is not browser.
171 // If sourceNode is browser, the actual triggering principal may be
172 // differ than sourceNode's principal, since sourceNode's principal is
173 // top level document's one and the drag may be triggered from a frame
174 // with different principal.
175 if (sourceNode.nodePrincipal) {
176 return sourceNode.nodePrincipal;
180 // First, fallback to mozTriggeringPrincipalURISpec that is set when the
181 // drop comes from another content process.
182 let principalURISpec = aDataTransfer.mozTriggeringPrincipalURISpec;
183 if (!principalURISpec) {
184 // Fallback to either system principal or file principal, supposing
185 // the drop comes from outside of the browser, so that drops of file
186 // URIs are always allowed.
188 // TODO: Investigate and describe the difference between them,
189 // or use only one principal. (Bug 1367038)
190 if (fallbackToSystemPrincipal) {
191 return Services.scriptSecurityManager.getSystemPrincipal();
194 principalURISpec = "file:///";
196 return Services.scriptSecurityManager.createContentPrincipal(
197 Services.io.newURI(principalURISpec),
202 getTriggeringPrincipal(aEvent) {
203 let dataTransfer = aEvent.dataTransfer;
204 return this._getTriggeringPrincipalFromDataTransfer(dataTransfer, true);
208 let sourceNode = aEvent.dataTransfer.mozSourceNode;
209 if (aEvent.dataTransfer.mozCSP !== null) {
210 return aEvent.dataTransfer.mozCSP;
215 (sourceNode.localName !== "browser" ||
216 sourceNode.namespaceURI !==
217 "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
219 // Use sourceNode's csp only if the sourceNode is not browser.
221 // If sourceNode is browser, the actual triggering csp may be differ than sourceNode's csp,
222 // since sourceNode's csp is top level document's one and the drag may be triggered from a
223 // frame with different csp.
224 return sourceNode.csp;
229 canDropLink(aEvent, aAllowSameDocument) {
230 if (this._eventTargetIsDisabled(aEvent)) {
234 let dataTransfer = aEvent.dataTransfer;
235 let types = dataTransfer.types;
237 !types.includes("application/x-moz-file") &&
238 !types.includes("text/x-moz-url") &&
239 !types.includes("text/uri-list") &&
240 !types.includes("text/x-moz-text-internal") &&
241 !types.includes("text/plain")
246 if (aAllowSameDocument) {
250 // If this is an external drag, allow drop.
251 let sourceTopWC = dataTransfer.sourceTopWindowContext;
256 // If drag source and drop target are in the same top window, don't allow.
258 aEvent.originalTarget.ownerGlobal.browsingContext.currentWindowContext;
259 if (eventWC && sourceTopWC == eventWC.topWindowContext) {
266 dropLinks(aEvent, aDisallowInherit) {
267 if (aEvent && this._eventTargetIsDisabled(aEvent)) {
271 let dataTransfer = aEvent.dataTransfer;
272 let links = this._getDropLinks(dataTransfer);
273 let triggeringPrincipal = this._getTriggeringPrincipalFromDataTransfer(
278 for (let link of links) {
280 link.url = this._validateURI(
287 // Prevent the drop entirely if any of the links are invalid even if
288 // one of them is valid.
289 aEvent.stopPropagation();
290 aEvent.preventDefault();
298 validateURIsForDrop(aEvent, aURIs, aDisallowInherit) {
299 let dataTransfer = aEvent.dataTransfer;
300 let triggeringPrincipal = this._getTriggeringPrincipalFromDataTransfer(
305 for (let uri of aURIs) {
315 queryLinks(aDataTransfer) {
316 return this._getDropLinks(aDataTransfer);
319 _eventTargetIsDisabled(aEvent) {
320 let ownerDoc = aEvent.originalTarget.ownerDocument;
321 if (!ownerDoc || !ownerDoc.defaultView) {
325 return ownerDoc.defaultView.windowUtils.isNodeDisabledForEvents(
326 aEvent.originalTarget