Skip to content

Commit

Permalink
fix: move revoking of object urls to content script and update firefo…
Browse files Browse the repository at this point in the history
…x implementation for binary data (#308)

* chore: move revoking of object urls to content script

* chore: update CSP for firefox to allow fetching blobs

* fix: update firefox implementation

* chore: revert version bump
  • Loading branch information
amk-dev authored Nov 26, 2024
1 parent 881c1cb commit 0e1c664
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
6 changes: 2 additions & 4 deletions manifest.firefox.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"background": {
"scripts": [
"index.js"
]
"scripts": ["index.js"]
},
"browser_specific_settings": {
"gecko": {
Expand All @@ -12,4 +10,4 @@
"content_security_policy": {
"extension_pages": "script-src 'self'"
}
}
}
4 changes: 4 additions & 0 deletions src/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ function main() {
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.action === "__POSTWOMAN_EXTENSION_PING__") {
sendResponse(true)
} else if (msg.action === "__POSTWOMAN_EXTENSION_REVOKE_OBJECT_URLS__") {
msg.objectURLsToRevoke.forEach((objectURL: string) => {
URL.revokeObjectURL(objectURL)
})
}
})
}
Expand Down
14 changes: 8 additions & 6 deletions src/hookContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@
})

if (config.data instanceof File) {
config.binaryContent = {
name: config.data.name,
type: config.data.type,
size: config.data.size,
// we'll convert the file to a url to access it from the extension
objectURL: URL.createObjectURL(config.data),
// firefox successfully sends the file object as is, but chrome malforms it
// so we're using objectURLs to access the file from the extension in chrome
if (process.env.HOPP_EXTENSION_TARGET === "CHROME") {
config.binaryContent = {
name: config.data.name,
// we'll convert the file to a url to access it from the extension
objectURL: URL.createObjectURL(config.data),
}
}
}

Expand Down
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ async function fetchUsingAxiosConfig(
method: axiosConfig.method,

// Ignore the body for GET and HEAD requests to prevent error with axios
body: (["get", "head"].includes(axiosConfig.method?.toLowerCase())) ? undefined : axiosConfig.data,
body: ["get", "head"].includes(axiosConfig.method?.toLowerCase())
? undefined
: axiosConfig.data,
signal: abortController.signal,
})

Expand Down Expand Up @@ -212,8 +214,6 @@ const processBinaryBody: (
const blob = await response.blob()

reqConfig.data = new File([blob], reqConfig.binaryContent.name, {})

URL.revokeObjectURL(objectURL)
}

return reqConfig
Expand Down Expand Up @@ -263,7 +263,7 @@ const getAllFetchResponseHeaders = (
return headers
}

const handleSendRequestMessage = async (config: any) => {
const handleSendRequestMessage = async (config: any, tabID?: number) => {
try {
const processedConfig = await processRequest(config)

Expand Down Expand Up @@ -331,6 +331,25 @@ const handleSendRequestMessage = async (config: any) => {
},
}
} finally {
// revoke the objectURLs, if any
// keeping this as an array, cause in the future, if we're adding support for formdata files in a similar way, we can add those objectURLs here
const objectURLsToRevoke: string[] = []

if (config.binaryContent?.objectURL) {
objectURLsToRevoke.push(config.binaryContent.objectURL)
}

if (objectURLsToRevoke.length > 0 && tabID) {
chrome.tabs.sendMessage(
tabID,
{
action: "__POSTWOMAN_EXTENSION_REVOKE_OBJECT_URLS__",
objectURLsToRevoke,
},
() => {}
)
}

// remove the cookies set for this request
await removeRequestCookies()
}
Expand All @@ -346,7 +365,7 @@ const cancelRequest = () => {
chrome.runtime.onMessage.addListener(
(message: PWChromeMessage<any>, _sender, sendResponse) => {
if (message.messageType === "send-req") {
handleSendRequestMessage(message.data).then(sendResponse)
handleSendRequestMessage(message.data, _sender.tab.id).then(sendResponse)
return true
} else if (message.messageType === "cancel-req") {
cancelRequest()
Expand Down

0 comments on commit 0e1c664

Please sign in to comment.