Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ad snippet analysis #69

Merged
merged 1 commit into from Aug 9, 2017
Merged

Ad snippet analysis #69

merged 1 commit into from Aug 9, 2017

Conversation

senky
Copy link
Contributor

@senky senky commented Aug 6, 2017

Analysis of entered snippet will be presented to the admin, such as “untrusted connection” when board runs on HTTPS but advertisement snippet will try to load HTTP JS file, “blocking javascript” when <script> tag without async attribute will be present, etc.

@phpbb-user phpbb-user added WIP and removed WIP labels Aug 6, 2017
<dd><textarea id="ad_code" name="ad_code" rows="20" cols="60" style="width: 95%;">{{ AD_CODE }}</textarea></dd>
<dd>
<textarea id="ad_code" name="ad_code" rows="20" cols="60" style="width: 95%;">{{ AD_CODE }}</textarea>
<p>{{ lang('AD_CODE_NOT_SURE') }} <input type="submit" class="button2" id="analyse_ad_code" name="analyse_ad_code" value="{{ lang('ANALYSE_AD_CODE') }}"></p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all we need here is the button (dont need the NOT_SURE line)

</ul>
{% endif %}
{% else %}
{{ lang('EVERYTHING_OK') }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't say anything here. If the ad code has not been analyzed, we shouldn't presume to say the code is ok.

<h4>{{ lang('WARNINGS') }}</h4>
<ul>
{% for warning in loops.analyser_results_warning %}
<li>{{ warning.MESSAGE }}</li>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a little inconsistency in the indentations between these two blocks of code here and above.

We should only say something if the test has been run and passed (maybe define this template var that everything is OK only if there are no test fails, after it has been run).

$this->lang = $lang;
}

public function test($ad_code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docblock?

$this->lang = $lang;
}

public function test($ad_code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer calling this run. Also in the following classes, calling it run. It looks better when we have:

$test->run();
$analyser->run();

'ANALYSE_AD_CODE' => 'analyse advertisement code',
'RESULTS' => 'Results',
'NOTICES' => 'Notices',
'EVERYTHING_OK' => 'The code seems to be OK.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code appears OK.

@@ -80,6 +85,12 @@
'ACP_AD_DISABLE_SUCCESS' => 'Advertisement disabled successfully.',
'ACP_AD_DISABLE_ERRORED' => 'There was an error disabling the advertisement.',

// Analyser tests
'UNSECURE_CONNECTION' => '<strong>Mixed Content</strong><br />Your code will probably generate “Mixed Content” warning in the browser, because your board runs on HTTPS, but the ad code attempts to load a file from unsecure HTTP connection.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed Content
Your board runs on a secure HTTPS connection, however the ad code is attempting to load content from an insecure HTTP connection. This can cause browsers to generate a "Mixed Content" warning to let users know that the page contains insecure resources.

@@ -80,6 +85,12 @@
'ACP_AD_DISABLE_SUCCESS' => 'Advertisement disabled successfully.',
'ACP_AD_DISABLE_ERRORED' => 'There was an error disabling the advertisement.',

// Analyser tests
'UNSECURE_CONNECTION' => '<strong>Mixed Content</strong><br />Your code will probably generate “Mixed Content” warning in the browser, because your board runs on HTTPS, but the ad code attempts to load a file from unsecure HTTP connection.',
'SCRIPT_WITHOUT_ASYNC' => '<strong>script without async attribute</strong><br />Your code will load script synchronously, which affects page load performance. Use <samp>async</samp> attribute to speed up the page load.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-asynchronous javascript
This ad code loads JavaScript code in a non-asynchronous way. This means it will block any other Javascript from loading until it has completed loading, which can affect page load performance. Use of the async attribute can speed up the page load.

// Analyser tests
'UNSECURE_CONNECTION' => '<strong>Mixed Content</strong><br />Your code will probably generate “Mixed Content” warning in the browser, because your board runs on HTTPS, but the ad code attempts to load a file from unsecure HTTP connection.',
'SCRIPT_WITHOUT_ASYNC' => '<strong>script without async attribute</strong><br />Your code will load script synchronously, which affects page load performance. Use <samp>async</samp> attribute to speed up the page load.',
'ALERT_USAGE' => '<strong>Usage of <samp>alert()</samp></strong><br />Your code uses <samp>alert()</samp> function which annoys users and even blocks them using your board before they close it. Some browsers will even block page load and display warning to the user.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage of alert()
Your code uses the alert() function which is not a good practice and can distract users. Some browsers may also block page load and display additional warnings to the user.

'UNSECURE_CONNECTION' => '<strong>Mixed Content</strong><br />Your code will probably generate “Mixed Content” warning in the browser, because your board runs on HTTPS, but the ad code attempts to load a file from unsecure HTTP connection.',
'SCRIPT_WITHOUT_ASYNC' => '<strong>script without async attribute</strong><br />Your code will load script synchronously, which affects page load performance. Use <samp>async</samp> attribute to speed up the page load.',
'ALERT_USAGE' => '<strong>Usage of <samp>alert()</samp></strong><br />Your code uses <samp>alert()</samp> function which annoys users and even blocks them using your board before they close it. Some browsers will even block page load and display warning to the user.',
'LOCATION_CHANGE' => '<strong>Redirection</strong><br />Your code will redirect user to another (probably malicious) page.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redirection
Your code appears it can redirect user to another page or site. Redirects can sometimes send users to unintended, often malicious, destinations. Please verify the integrity of your ad code's redirection destination.

*/
public function test($ad_code)
{
if (preg_match_all('/&lt;script(.*)&gt;/U', $ad_code, $matches))
Copy link
Contributor

@iMattPro iMattPro Aug 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing for me because this first regex is matching the closing script tag as well, and since the closing tag has no async...false positive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? It can't match closing tag, there is a slash after &lt; in that case...

@iMattPro
Copy link
Contributor

iMattPro commented Aug 8, 2017 via email

@senky
Copy link
Contributor Author

senky commented Aug 9, 2017

@VSEphpbb can you post your ad code here?

@iMattPro
Copy link
Contributor

iMattPro commented Aug 9, 2017

@senky I see what it is.. there's a script within a script in Google Adsense

<div class="stat-block" style="width:100%">
	<h3 style="margin-top:10px;">Advertisements</h3>
	<div  style="text-align: center; padding: 12px 0">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- Blog Leaderboard -->
		<ins class="adsbygoogle"
		     style="display:inline-block;width:728px;height:90px"
		     data-ad-client="x"
		     data-ad-slot="x"></ins>
		<script>
		    (adsbygoogle = window.adsbygoogle || []).push({});
		</script>
	</div>
</div>

@iMattPro
Copy link
Contributor

iMattPro commented Aug 9, 2017

@senky you should update your regex to only check for async in script tags that contain a src attribute I think

);
foreach ($this->tests as $test)
{
$result = array_merge_recursive($result, $test->run($ad_code));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this triggers an inspection warning that merging arrays in a loop causes high CPU usage and is a resources greedy construction. Perhaps there is an alternative approach?

https://github.com/kalessil/phpinspectionsea/blob/master/docs/performance.md#slow-array-function-used-in-loop

{% endfor %}
{% endif %}
{% elseif CODE_ANALYSED %}
{{ lang('EVERYTHING_OK') }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might as well wrap this in a <p class="successbox""></p> so it looks like the other messages and makes users feel extra good about themselves.

{% if loops.analyser_results_notice or loops.analyser_results_warning %}
<h2 class="centered-text">{{ lang('RESULTS') }}</h2>
{% if loops.analyser_results_notice %}
<h4>{{ lang('NOTICES') }}</h4>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt we need the Results, Warnings and Notices headings anymore. The newly stylized message boxes should be enough, and IMO look a little cleaner without all the headings now.

@iMattPro iMattPro merged commit 451a88b into phpbb-extensions:master Aug 9, 2017
@senky senky deleted the ad-code-analysis branch August 9, 2017 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants