Skip to content

Commit

Permalink
bugfix: 修复 OPTIONS.js 在 OPTIONS 请求伪造成功后,实际请求却不含跨域响应头 `Access-Control-…
Browse files Browse the repository at this point in the history
…Allow-Origin`,导致跨域请求失败的问题。
  • Loading branch information
wangliang181230 committed Apr 21, 2024
1 parent 120279c commit 667cd25
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const responseReplaceApi = require('./responseReplace')

module.exports = {
name: 'OPTIONSHeaders',
desc: '开启了options.js功能时,正常请求时,会需要增加响应头 `Access-Control-Allow-Origin: xxx`',
priority: 201,
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { rOptions, log } = context

if (rOptions.method === 'OPTIONS') {
return
}

const headers = {
'Access-Control-Allow-Origin': rOptions.headers.origin
}

// 替换响应头
responseReplaceApi.replaceResponseHeaders(headers, res, proxyRes)

res.setHeader('DS-AfterOPTIONSHeaders-Interceptor', true)
log.info('AfterOPTIONSHeaders intercept:', JSON.stringify(headers))
},
is (interceptOpt) {
return !!interceptOpt.options
}
}
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/interceptor/impl/res/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getScriptByUrlOrPath (scriptUrlOrPath) {

module.exports = {
name: 'script',
priority: 203,
priority: 211,
responseIntercept (context, interceptOpt, req, res, proxyReq, proxyRes, ssl, next) {
const { rOptions, log, setting } = context

Expand Down
7 changes: 5 additions & 2 deletions packages/mitmproxy/src/lib/interceptor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const proxy = require('./impl/req/proxy')
const sni = require('./impl/req/sni')

// response interceptor impls
const responseReplace = require('./impl/res/responseReplace')
const OPTIONSHeaders = require('./impl/res/AfterOPTIONSHeaders')
const cacheRes = require('./impl/res/cacheRes')
const responseReplace = require('./impl/res/responseReplace')

const script = require('./impl/res/script')

module.exports = [
Expand All @@ -24,5 +26,6 @@ module.exports = [
proxy, sni,

// response interceptor impls
responseReplace, cacheRes, script
OPTIONSHeaders, cacheRes, responseReplace,
script
]

0 comments on commit 667cd25

Please sign in to comment.