0

I am trying to use sub_filter to replace pieces of this code in a .js file.

SYNO.SDS.AzureSSOUtils = function() {
    var a = 600
      , c = 500
      , e = (screen.width / 2) - c / 2
      , d = (screen.height / 2) - a / 2
      , b = String.format("height={0},width={1},left={2},top={3}", a, c, e, d);
    return {
        login: function(l, i) {
            var f = (function(m) {
                l(i, m)
            }
            ).createDelegate(this);
            var g = _S("dsm_https_port");
            var k = Ext.urlEncode({
                action: "signin",
                method: "azure_oidc",
                origin: location.origin
            });
            var j = location.hostname;
            var h = "https://" + j + ":" + g + "/webman/index.cgi?" + k;
            window.addEventListener("message", f);
            window.open(h, "OIDC", b)
        },
        logout: function() {
            var g = Ext.urlEncode({
                asso: "true"
            });
            var f = "webman/logout.cgi?" + g;
            window.open(f, "OIDC", b)
        }
    }

I have managed to use sub_filter to replace simple fragments

For example all of these examples work just fine and replace what i expect:

sub_filter              'dsm_https_port' '443';
sub_filter              "dsm_https_port" "443";
sub_filter              'SYNO.SDS.AzureSSOUtils' 'PleaseWorkDamnYou'

What I really want to do is one of these two replacements:

sub_filter 'var g = _S("dsm_https_port");' 'var g = 443;';
or
sub filter 'var h = "https://" + j + ":" + g + "/webman/index.cgi?" + k;' 'var h = "https://" + j + + "/webman/index.cgi?" + k;'; 

However neither of these seem to do anything. I have tried escaping every space, quote and semi colon by placing \ in front of them - while that didn't error it didn't make a difference.

What am I doing wrong? I assume this is because the text to be replaced has ambiguous characters in it but i am not sure.

for reference, yes i have made sure the following is enabled in my location section (which is what most articles advise, first all of this was need to be able to edit the small fragments that are working:

        gzip                    off;    
        proxy_set_header        Accept-Encoding         "";
        sub_filter_once         off;
        sub_filter_types        *;
  • Can you describe what is your actual problem you are trying to solve? To me, it looks you are trying to use `sub_filter` when you actually need to define variables outside the scope of JavaScript file. – Tero Kilkanen Sep 14 '20 at 06:03
  • 1
    Alex Balcanquall you need to escape the slashes like for example: "https:\/\/" – Lorem ipsum Sep 14 '20 at 07:05
  • @TeroKilkanen i can't edit the javascript directly, i want to use the sub_filter to edit the .js file as it is downloaded to the client; assume the .js is uneditable. – Alex Balcanquall Sep 14 '20 at 17:09
  • @LukasRäpple thanks i will try that again, do you have a complete list of what does and doesn't need to be escaped? I tried escaping the quotes and spaces in this and couldn't get it to work. ```sub_filter 'var g = _S("dsm_https_port")' 'var g = 443'``` also one example i saw only escaped the find string and not the replacement string. Do i need to escape both sides of the sub_filter parameters? – Alex Balcanquall Sep 14 '20 at 17:12
  • How do you exactly use this JS file? – Tero Kilkanen Sep 14 '20 at 17:51
  • @TeroKilkanen i go to a web page on my synology diskstation, i click a button called next, this opens a new windows to a URL, this URL has the wrong port (because the folks at synology hard coded it). I am trying to override that silly decision on sinology's part without editing the .js file on the synology as that would put me out of support / break when OS is updated etc. URL rewiting would not work as the request is on wrong port and not processed by nginx. so one has to sub_filter the .js file as it is downloaded by the local browser. Anyhoo, solved it - was my mistake about whitespace. – Alex Balcanquall Sep 14 '20 at 19:09
  • So you are assuming that modifying nginx configuration in the unit would not void support / warranty? – Tero Kilkanen Sep 14 '20 at 19:46
  • @TeroKilkanen I never said I am modifying nginx on the synology. I have my own nginx server in a docker container on a different host. The question i posted was about ngix syntax for a sub_filter replacement in a download file - the desktop.js file i used for asking was one of multiple use cases, some not involving synology at all. I just chose this one as my most pressing issue to solve. – Alex Balcanquall Sep 14 '20 at 20:12

1 Answers1

1

Ok, this is entirely my mistake (comes from not being a coder).

I was using chrome's developer tools to look at the desktop.js file the browser downloaded using the preview function in the network tab of chrome developer tools. It makes the code pretty. It never occurred to me it would insert spaces where it felt was good.

I downloaded the desktop.js file locally raw with no formatting and it turned out, that the text I wanted to replace was var g=_S("dsm_https_port") not var g = _S("dsm_https_port");

and var h="https://"+j+":"+g+"/webman/index.cgi?"+k not var h = "https://" + j + ":" + g + "/webman/index.cgi?" + k

once I fixed the whitespace issue and used escape chars everything worked as expected (i tried escaping before, but the whitespace issue was the root issue that blocked me).

Lesson / Answer = don't look at prettified code, look at raw file as returned by server to the browser.

For completeness of answer, this was the final sub_filter that worked:

sub_filter 'var g=_S(\"dsm_https_port\")' 'var g=443';

Thanks for everyone's comments, that was helpful.