improve ruleset unmarshalling behavior if there isnt a top level rule key

This commit is contained in:
Kevin Pham
2023-12-09 08:37:23 -06:00
parent 27c3892b0b
commit 22533dc739
6 changed files with 58 additions and 6 deletions

View File

@@ -513,6 +513,15 @@ var AllMods Modifiers = Modifiers{
{Name: "val", Type: "string"}, {Name: "val", Type: "string"},
}, },
}, },
{
Name: "ModifyIncomingScriptsWithRegex",
Description: "ModifyIncomingScriptsWithRegex modifies all incoming javascript (application/javascript and inline <script> in text/html) using a regex match and replacement.",
CodeEditLink: "https://github.com/everywall/ladder/edit/origin/proxy_v2/proxychain/responsemodifiers/modify_incoming_scripts_with_regex.go",
Params: []Param{
{Name: "matchRegex", Type: "string"},
{Name: "replacement", Type: "string"},
},
},
{ {
Name: "SetResponseHeader", Name: "SetResponseHeader",
Description: "SetResponseHeader modifies response headers from the upstream server", Description: "SetResponseHeader modifies response headers from the upstream server",

View File

@@ -45,7 +45,7 @@ func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler {
). ).
AddResponseModifications( AddResponseModifications(
tx.ForwardResponseHeaders(), tx.ForwardResponseHeaders(),
//tx.BlockThirdPartyScripts(), tx.BlockThirdPartyScripts(),
tx.DeleteIncomingCookies(), tx.DeleteIncomingCookies(),
tx.DeleteLocalStorageData(), tx.DeleteLocalStorageData(),
tx.DeleteSessionStorageData(), tx.DeleteSessionStorageData(),
@@ -54,7 +54,7 @@ func NewProxySiteHandler(opts *ProxyOptions) fiber.Handler {
tx.RewriteHTMLResourceURLs(), tx.RewriteHTMLResourceURLs(),
tx.PatchDynamicResourceURLs(), tx.PatchDynamicResourceURLs(),
tx.PatchTrackerScripts(), tx.PatchTrackerScripts(),
tx.BlockElementRemoval(".article-content"), // techcrunch //tx.BlockElementRemoval(".article-content"), // techcrunch
//tx.BlockElementRemoval(".available-content"), // substack //tx.BlockElementRemoval(".available-content"), // substack
// tx.SetContentSecurityPolicy("default-src * 'unsafe-inline' 'unsafe-eval' data: blob:;"), // tx.SetContentSecurityPolicy("default-src * 'unsafe-inline' 'unsafe-eval' data: blob:;"),
) )

View File

@@ -52,6 +52,19 @@ func PatchDynamicResourceURLs() proxychain.ResponseModification {
htmlRewriter := rewriters.NewHTMLRewriter(chain.Response.Body, rr) htmlRewriter := rewriters.NewHTMLRewriter(chain.Response.Body, rr)
chain.Response.Body = htmlRewriter chain.Response.Body = htmlRewriter
// window.location
/*
spoofedLocationAPI := fmt.Sprintf(`{href:"%s", origin:"%s", pathname:"%s", protocol:"%s:", port:"%s"}`,
reqURL.String(), reqURL.Host,
reqURL.Path, reqURL.Scheme, reqURL.Port())
spoofedLocationAPI := fmt.Sprintf(`{origin: "%s"}`, reqURL.Host)
fmt.Println(spoofedLocationAPI)
chain.AddOnceResponseModifications(
ModifyIncomingScriptsWithRegex(`window\.location`, spoofedLocationAPI),
)
*/
return nil return nil
} }
} }

View File

@@ -84,6 +84,10 @@ func init() {
return tx.SetIncomingCookie(params[0], params[1]) return tx.SetIncomingCookie(params[0], params[1])
} }
rsmModMap["ModifyIncomingScriptsWithRegex"] = func(params ...string) proxychain.ResponseModification {
return tx.ModifyIncomingScriptsWithRegex(params[0], params[1])
}
rsmModMap["SetResponseHeader"] = func(params ...string) proxychain.ResponseModification { rsmModMap["SetResponseHeader"] = func(params ...string) proxychain.ResponseModification {
return tx.SetResponseHeader(params[0], params[1]) return tx.SetResponseHeader(params[0], params[1])
} }

View File

@@ -33,14 +33,24 @@ func (rs *Ruleset) UnmarshalYAML(unmarshal func(interface{}) error) error {
type AuxRuleset struct { type AuxRuleset struct {
Rules []Rule `yaml:"rules"` Rules []Rule `yaml:"rules"`
} }
yr := &AuxRuleset{} yamlRuleset := &AuxRuleset{}
if err := unmarshal(&yr); err != nil { if err := unmarshal(&yamlRuleset); err != nil {
return err // if there is no top-level rule key, we'll try to unmarshal as if it is just a bare rule
recovered := false
yamlRule := &Rule{}
if err := unmarshal(&yamlRule); err != nil {
yamlRuleset.Rules = append(yamlRuleset.Rules, *yamlRule)
recovered = true
}
if !recovered {
return err
}
} }
rs._rulemap = make(map[string]*Rule) rs._rulemap = make(map[string]*Rule)
rs.Rules = yr.Rules rs.Rules = yamlRuleset.Rules
// create a map of pointers to rules loaded above based on domain string keys // create a map of pointers to rules loaded above based on domain string keys
// this way we don't have two copies of the rule in ruleset // this way we don't have two copies of the rule in ruleset

View File

@@ -15,3 +15,19 @@ rules:
requestmodifications: requestmodifications:
- name: ForwardRequestHeaders - name: ForwardRequestHeaders
params: [] params: []
- domains:
- quantamagzine.org
responsemodifications:
- name: BlockElementRemoval
params:
- "#postContent"
- domains:
- techcrunch.com
responsemodifications:
- name: ModifyIncomingScriptsWithRegex
params:
- "window\\.location"
- |
{origin: "techcrunch.com"}