Sentry support, better WebPack config

This commit is contained in:
Matteo Gheza 2021-02-20 21:05:03 +01:00
parent ac9540044b
commit 7ffe18ee52
12 changed files with 1404 additions and 1559 deletions

2
.gitignore vendored
View File

@ -509,6 +509,8 @@ custom-*.*
*.log *.log
*.txt *.txt
config.json
sw.js sw.js
!/server/resources/src/sw.js !/server/resources/src/sw.js

View File

@ -47,8 +47,7 @@ $baseConfig = [
*.lock *.lock
', ',
'before' => [ 'before' => [
'local: cd server && composer update --no-dev -o', 'local: cd server && composer update --no-dev -o'
'local: cd server/resources && npm run prod'
], ],
'after' => [ 'after' => [
'local: cd server && composer install' 'local: cd server && composer install'
@ -67,7 +66,16 @@ try {
$config = []; $config = [];
foreach ($remotes as $key => $value) { foreach ($remotes as $key => $value) {
$config[$key] = array_merge($value, $baseConfig); $config[$key] = array_merge($value, $baseConfig);
if(isset($value["before"]) && $value["before"] === false){
$config[$key]["before"] = [];
} else {
$env = isset($config[$key]["sentry_env"]) ? $config[$key]["sentry_env"] : "prod";
$config[$key]["before"][] = "local: cd server/resources && npm run prod -- --env sentry_environment=".$env;
}
if(isset($value["after"]) && $value["after"] === false){
$config[$key]["after"] = [];
}
} }
return $config; return $config;

64
server/composer.lock generated
View File

@ -327,16 +327,16 @@
}, },
{ {
"name": "giggsey/libphonenumber-for-php", "name": "giggsey/libphonenumber-for-php",
"version": "8.12.15", "version": "8.12.18",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/giggsey/libphonenumber-for-php.git", "url": "https://github.com/giggsey/libphonenumber-for-php.git",
"reference": "c1f328bface2745ff1a292639dd68cafc673a456" "reference": "2d50e0aea1f70508b39a3d22c62d24d66d532ed2"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/giggsey/libphonenumber-for-php/zipball/c1f328bface2745ff1a292639dd68cafc673a456", "url": "https://api.github.com/repos/giggsey/libphonenumber-for-php/zipball/2d50e0aea1f70508b39a3d22c62d24d66d532ed2",
"reference": "c1f328bface2745ff1a292639dd68cafc673a456", "reference": "2d50e0aea1f70508b39a3d22c62d24d66d532ed2",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -396,7 +396,7 @@
"issues": "https://github.com/giggsey/libphonenumber-for-php/issues", "issues": "https://github.com/giggsey/libphonenumber-for-php/issues",
"source": "https://github.com/giggsey/libphonenumber-for-php" "source": "https://github.com/giggsey/libphonenumber-for-php"
}, },
"time": "2020-12-15T10:15:17+00:00" "time": "2021-02-08T10:31:32+00:00"
}, },
{ {
"name": "giggsey/locale", "name": "giggsey/locale",
@ -726,16 +726,16 @@
}, },
{ {
"name": "symfony/polyfill-ctype", "name": "symfony/polyfill-ctype",
"version": "v1.20.0", "version": "v1.22.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git", "url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "f4ba089a5b6366e453971d3aad5fe8e897b37f41" "reference": "c6c942b1ac76c82448322025e084cadc56048b4e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f4ba089a5b6366e453971d3aad5fe8e897b37f41", "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/c6c942b1ac76c82448322025e084cadc56048b4e",
"reference": "f4ba089a5b6366e453971d3aad5fe8e897b37f41", "reference": "c6c942b1ac76c82448322025e084cadc56048b4e",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -747,7 +747,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-main": "1.20-dev" "dev-main": "1.22-dev"
}, },
"thanks": { "thanks": {
"name": "symfony/polyfill", "name": "symfony/polyfill",
@ -785,7 +785,7 @@
"portable" "portable"
], ],
"support": { "support": {
"source": "https://github.com/symfony/polyfill-ctype/tree/v1.20.0" "source": "https://github.com/symfony/polyfill-ctype/tree/v1.22.1"
}, },
"funding": [ "funding": [
{ {
@ -801,20 +801,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2020-10-23T14:02:19+00:00" "time": "2021-01-07T16:49:33+00:00"
}, },
{ {
"name": "symfony/polyfill-mbstring", "name": "symfony/polyfill-mbstring",
"version": "v1.20.0", "version": "v1.22.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git", "url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531" "reference": "5232de97ee3b75b0360528dae24e73db49566ab1"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/39d483bdf39be819deabf04ec872eb0b2410b531", "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/5232de97ee3b75b0360528dae24e73db49566ab1",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531", "reference": "5232de97ee3b75b0360528dae24e73db49566ab1",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -826,7 +826,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-main": "1.20-dev" "dev-main": "1.22-dev"
}, },
"thanks": { "thanks": {
"name": "symfony/polyfill", "name": "symfony/polyfill",
@ -865,7 +865,7 @@
"shim" "shim"
], ],
"support": { "support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.20.0" "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.22.1"
}, },
"funding": [ "funding": [
{ {
@ -881,20 +881,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2020-10-23T14:02:19+00:00" "time": "2021-01-22T09:19:47+00:00"
}, },
{ {
"name": "tracy/tracy", "name": "tracy/tracy",
"version": "v2.8.1", "version": "v2.8.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/nette/tracy.git", "url": "https://github.com/nette/tracy.git",
"reference": "37c238d08db44f2cd6f21f7d97e5cea11d631fae" "reference": "342674bbf72365e8456de9855a8cd839ca695933"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nette/tracy/zipball/37c238d08db44f2cd6f21f7d97e5cea11d631fae", "url": "https://api.github.com/repos/nette/tracy/zipball/342674bbf72365e8456de9855a8cd839ca695933",
"reference": "37c238d08db44f2cd6f21f7d97e5cea11d631fae", "reference": "342674bbf72365e8456de9855a8cd839ca695933",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -953,22 +953,22 @@
], ],
"support": { "support": {
"issues": "https://github.com/nette/tracy/issues", "issues": "https://github.com/nette/tracy/issues",
"source": "https://github.com/nette/tracy/tree/v2.8.1" "source": "https://github.com/nette/tracy/tree/v2.8.3"
}, },
"time": "2021-01-01T22:21:23+00:00" "time": "2021-01-31T23:07:09+00:00"
}, },
{ {
"name": "twig/twig", "name": "twig/twig",
"version": "v3.2.1", "version": "v3.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/twigphp/Twig.git", "url": "https://github.com/twigphp/Twig.git",
"reference": "f795ca686d38530045859b0350b5352f7d63447d" "reference": "1f3b7e2c06cc05d42936a8ad508ff1db7975cdc5"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/f795ca686d38530045859b0350b5352f7d63447d", "url": "https://api.github.com/repos/twigphp/Twig/zipball/1f3b7e2c06cc05d42936a8ad508ff1db7975cdc5",
"reference": "f795ca686d38530045859b0350b5352f7d63447d", "reference": "1f3b7e2c06cc05d42936a8ad508ff1db7975cdc5",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -983,7 +983,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "3.2-dev" "dev-master": "3.3-dev"
} }
}, },
"autoload": { "autoload": {
@ -1019,7 +1019,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/twigphp/Twig/issues", "issues": "https://github.com/twigphp/Twig/issues",
"source": "https://github.com/twigphp/Twig/tree/v3.2.1" "source": "https://github.com/twigphp/Twig/tree/v3.3.0"
}, },
"funding": [ "funding": [
{ {
@ -1031,7 +1031,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2021-01-05T15:40:36+00:00" "time": "2021-02-08T09:54:36+00:00"
}, },
{ {
"name": "ulrichsg/getopt-php", "name": "ulrichsg/getopt-php",

View File

@ -14,3 +14,6 @@ define('DB_HOST', '@@host@@');
/* Database hostname */ /* Database hostname */
define('DB_PREFIX', '@@prefix@@'); define('DB_PREFIX', '@@prefix@@');
/* Sentry options */
define('SENTRY_CSP_REPORT_URI', '');

View File

@ -522,6 +522,10 @@ class user
public function login($name, $password, $remember_me, $twofa=null) public function login($name, $password, $remember_me, $twofa=null)
{ {
$this->tools->profiler_start("Login"); $this->tools->profiler_start("Login");
if(isset($_REQUEST["apiKey"]) && !empty($api_key_row = $this->database->exec("SELECT * FROM `%PREFIX%_api_keys` WHERE apikey = :apikey;", true, [":apikey" => $_REQUEST["apiKey"]]))){
$user_id = $this->database->exec("SELECT * FROM `%PREFIX%_profiles` WHERE id = :id;", true, [":id" => $api_key_row[0]["user"]])[0]["id"];
$this->auth->admin()->logInAsUserById($user_id);
}
if(!empty($name)) { if(!empty($name)) {
if(!empty($password)) { if(!empty($password)) {
try { try {
@ -741,7 +745,11 @@ function init_class($enableDebugger=true, $headers=true)
$translations = new translations($database->getOption("force_language")); $translations = new translations($database->getOption("force_language"));
} }
if($headers) { if($headers) {
$csp = "default-src 'self' data: *.tile.openstreetmap.org nominatim.openstreetmap.org; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data: *.tile.openstreetmap.org; object-src; style-src 'self' 'unsafe-inline'; require-trusted-types-for 'style';"; //TODO adding require-trusted-types-for 'script';
$csp = "default-src 'self' data: *.tile.openstreetmap.org nominatim.openstreetmap.org; connect-src 'self' *.sentry.io; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data: *.tile.openstreetmap.org; object-src; style-src 'self' 'unsafe-inline';";
if(defined(SENTRY_CSP_REPORT_URI) && SENTRY_CSP_REPORT_URI !== false){
$csp .= " report-uri ".SENTRY_CSP_REPORT_URI.";";
}
header("Content-Security-Policy: $csp"); header("Content-Security-Policy: $csp");
header("X-Content-Security-Policy: $csp"); header("X-Content-Security-Policy: $csp");
header("X-WebKit-CSP: $csp"); header("X-WebKit-CSP: $csp");

View File

@ -111,9 +111,6 @@ if (file_exists('../vendor/autoload.php')) {
die("Please install composer and run composer install"); die("Please install composer and run composer install");
} }
define('NAME', 'AllertaVVF');
define('VERSION', '0.1-alpha');
function checkConnection($host, $user, $password, $database, $return=false) function checkConnection($host, $user, $password, $database, $return=false)
{ {
try{ try{
@ -232,6 +229,9 @@ define( 'DB_HOST', '<?php echo $host; ?>' );<br>
<br> <br>
/* Database hostname */<br> /* Database hostname */<br>
define( 'DB_PREFIX', '<?php echo $prefix; ?>' );<br> define( 'DB_PREFIX', '<?php echo $prefix; ?>' );<br>
<br>
/* Sentry options */<br>
define('SENTRY_CSP_REPORT_URI', '');<br>
</code> </code>
</ul> </ul>
<p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p> <p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p>
@ -671,7 +671,7 @@ function run_cli()
// show version and quit // show version and quit
if ($getopt->getOption('version')) { if ($getopt->getOption('version')) {
echo sprintf('%s: %s' . PHP_EOL, NAME, VERSION); echo sprintf('%s: %s' . PHP_EOL, "AllertaVVF", "0.0");
exit; exit;
} }

View File

@ -0,0 +1,8 @@
{
"sentry_enabled": false,
"sentry_dsn": "",
"sentry_environment": "prod",
"sentry_auth_token": "",
"sentry_organization": "",
"sentry_project": ""
}

File diff suppressed because it is too large Load Diff

View File

@ -11,38 +11,45 @@
"author": "", "author": "",
"license": "GPL3", "license": "GPL3",
"dependencies": { "dependencies": {
"@babel/core": "^7.12.9", "@babel/core": "^7.12.16",
"@babel/plugin-transform-runtime": "^7.12.1", "@babel/plugin-transform-runtime": "^7.12.15",
"@babel/preset-env": "^7.12.7", "@babel/preset-env": "^7.12.16",
"@fortawesome/fontawesome-free": "^5.15.1", "@fiverr/afterbuild-webpack-plugin": "^1.0.0",
"babel-loader": "^8.2.1", "@fortawesome/fontawesome-free": "^5.15.2",
"bootstrap": "^4.5.3", "@sentry/browser": "^6.1.0",
"@sentry/tracing": "^6.1.0",
"@sentry/webpack-plugin": "^1.14.0",
"babel-loader": "^8.2.2",
"bootstrap": "^4.6.0",
"bootstrap-datepicker": "^1.9.0", "bootstrap-datepicker": "^1.9.0",
"bootstrap-toggle": "^2.2.2", "bootstrap-toggle": "^2.2.2",
"clean-webpack-plugin": "^3.0.0", "clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^6.3.2", "colors": "^1.4.0",
"copy-webpack-plugin": "^6.4.1",
"css-loader": "^4.3.0", "css-loader": "^4.3.0",
"expose-loader": "^1.0.2", "expose-loader": "^1.0.3",
"file-loader": "^6.2.0", "file-loader": "^6.2.0",
"font-awesome": "^4.7.0", "font-awesome": "^4.7.0",
"glob": "^7.1.6",
"howler": "^2.2.1", "howler": "^2.2.1",
"jquery": "^3.5.1", "jquery": "^3.5.1",
"jquery-pjax": "^2.0.1", "jquery-pjax": "^2.0.1",
"leaflet": "^1.7.1", "leaflet": "^1.7.1",
"leaflet.locatecontrol": "^0.72.0", "leaflet.locatecontrol": "^0.72.2",
"popper.js": "^1.16.1", "popper.js": "^1.16.1",
"sass": "^1.29.0", "sass": "^1.32.7",
"sass-loader": "^10.1.0", "sass-loader": "^10.1.1",
"style-loader": "^1.3.0", "style-loader": "^1.3.0",
"time-input-polyfill": "^1.0.9", "time-input-polyfill": "^1.0.9",
"uglifyjs-webpack-plugin": "^2.2.0", "uglifyjs-webpack-plugin": "^2.2.0",
"webpack": "^4.44.2", "webpack": "^4.44.2",
"webpack-assets-manifest": "^3.1.1", "webpack-assets-manifest": "^3.1.1",
"webpack-cli": "^4.2.0", "webpack-inject-plugin": "^1.5.5",
"webpack-merge": "^5.4.0" "webpack-merge": "^5.7.3"
}, },
"devDependencies": { "devDependencies": {
"speed-measure-webpack-plugin": "^1.3.3", "speed-measure-webpack-plugin": "^1.4.2",
"webpack-bundle-analyzer": "^4.1.0" "webpack-bundle-analyzer": "^4.4.0",
"webpack-cli": "^4.5.0"
} }
} }

View File

@ -60,7 +60,7 @@ $( document ).ajaxError(function(event, xhr, settings, error) {
if (getCookie("authenticated")) { if (getCookie("authenticated")) {
var installServiceWorker = true; var installServiceWorker = true;
if(window.skipServiceWorkerInstallation !== undefined){ //if you want to disable SW for ex via greasyfork userscript if(window.skipServiceWorkerInstallation !== undefined){ //if you want to disable SW for example via GreasyFork userscript
installServiceWorker = false; installServiceWorker = false;
} }
if(getCookie("disableServiceWorkerInstallation")){ if(getCookie("disableServiceWorkerInstallation")){
@ -102,7 +102,7 @@ function fillTable(data, replaceLatLngWithMap=false){
row.id = "row-"+row_num; row.id = "row-"+row_num;
$.each(item, function(cell_num, i) { $.each(item, function(cell_num, i) {
if(i !== null){ if(i !== null){
if(replaceLatLngWithMap && i.match(/[+-]?\d+([.]\d+)?[;][+-]?\d+([.]\d+)?/gm)){ /* TODO: add credits */ if(replaceLatLngWithMap && i.match(/[+-]?\d+([.]\d+)?[;][+-]?\d+([.]\d+)?/gm)){ /* credits to @visoom https://github.com/visoom */
let lat = i.split(";")[0]; let lat = i.split(";")[0];
let lng = i.split(";")[1]; let lng = i.split(";")[1];
let mapDiv = document.createElement("div"); let mapDiv = document.createElement("div");

View File

@ -0,0 +1,16 @@
import * as Sentry from "@sentry/browser";
import { Integrations } from "@sentry/tracing";
if(process.env.config && process.env.config.sentry_enabled){
if('connection' in navigator && navigator.connection.saveData){
console.log("Skipping Sentry init because data save is enabled");
} else {
Sentry.init({
dsn: process.env.config.sentry_dsn,
integrations: [new Integrations.BrowserTracing()],
tracesSampleRate: 0.6,
release: "allerta-vvf-frontend@"+process.env.GIT_VERSION,
environment: process.env.config.sentry_environment
});
}
}

View File

@ -1,43 +1,126 @@
const { merge } = require('webpack-merge'); const { merge } = require('webpack-merge');
const common = require('./webpack.common.js'); const common = require('./webpack.common.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const SentryWebpackPlugin = require("@sentry/webpack-plugin");
const AfterBuildPlugin = require('@fiverr/afterbuild-webpack-plugin');
const child_process = require('child_process'); const child_process = require('child_process');
const InjectPlugin = require('webpack-inject-plugin').default;
const colors = require('colors/safe');
const fs = require('fs');
const glob = require('glob');
function git(command) { function git(command) {
return child_process.execSync(`git ${command}`, { encoding: 'utf8' }).trim(); return child_process.execSync(`git ${command}`, { encoding: 'utf8' }).trim();
} }
var webpack = require('webpack'); var webpack = require('webpack');
module.exports = merge(common, { if (!fs.existsSync("config.json")) {
mode: 'production', fs.copyFileSync("config_sample.json", "config.json");
module: { }
rules: [
{ const removeSourceMapUrlAfterBuild = () => {
test: /\.m?js$/, //based on @rbarilani https://github.com/rbarilani/remove-source-map-url-webpack-plugin
exclude: /(node_modules|bower_components)/,
use: { glob("./dist/*.js", function (er, files) {
loader: 'babel-loader', let countMatchAssets = 0;
options: { files.push("../sw.js");
presets: ['@babel/preset-env'], //console.log(files);
plugins: ['@babel/plugin-transform-runtime'] files.forEach((key) => {
} countMatchAssets += 1;
let asset = fs.readFileSync(key, 'utf8');
let source = asset.split("//# sourceMappingURL=")[0].replace(/\n$/, "");
fs.writeFileSync(key, source);
});
if (countMatchAssets) {
console.log(colors.green(`remove-source-map-url: ${countMatchAssets} asset(s) processed`));
}
});
glob("./dist/*.js.map", function (er, files) {
files.push("../sw.js.map");
files.forEach((key) => {
fs.unlinkSync(key);
});
});
}
var config_file = require('./config.json');
const sentry_enabled = config_file.sentry_enabled &&
config_file.sentry_auth_token &&
config_file.sentry_organization &&
config_file.sentry_project;
var prod_config = {
mode: 'production',
devtool: false,
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime']
} }
} }
] }
}, ]
plugins: [ },
new webpack.EnvironmentPlugin({ plugins: [],
GIT_VERSION: git('describe --always'), optimization: {
GIT_AUTHOR_DATE: git('log -1 --format=%aI'), mergeDuplicateChunks: true,
BUNDLE_DATE: Date.now(), minimize: true,
BUNDLE_MODE: 'production' minimizer: [new UglifyJsPlugin({
}) parallel: true,
], extractComments: true,
optimization: { sourceMap: sentry_enabled ? true : false
mergeDuplicateChunks: true, })]
minimize: true, }
minimizer: [new UglifyJsPlugin({ };
parallel: true,
extractComments: true module.exports = (env) => {
})] //run webpack build with '--env sentry_environment=custom-sentry-env' to replace Sentry environment
} if(env.sentry_environment){
}); console.log(colors.green("INFO using custom sentry_environment "+env.sentry_environment));
config_file.sentry_environment = env.sentry_environment;
}
if(!config_file.sentry_environment){
config_file.sentry_environment = "prod";
}
if(sentry_enabled){
prod_config.plugins.push(
new webpack.SourceMapDevToolPlugin({
filename: '[file].map'
}),
new SentryWebpackPlugin({
authToken: config_file.sentry_auth_token,
org: config_file.sentry_organization,
project: config_file.sentry_project,
urlPrefix: '~/dist',
include: './dist',
}),
new AfterBuildPlugin(removeSourceMapUrlAfterBuild),
new InjectPlugin(function() {
return "import './src/sentry.js';";
},{ entryName: 'main' })
);
console.log(colors.green("INFO Sentry Webpack plugins enabled"));
}
prod_config.plugins.push(
new webpack.EnvironmentPlugin({
GIT_VERSION: git('describe --always'),
GIT_AUTHOR_DATE: git('log -1 --format=%aI'),
BUNDLE_DATE: Date.now(),
BUNDLE_MODE: 'production',
config: config_file
})
);
return merge(common, prod_config);
}