mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
The security event log said what a guard DID - a request blocked, a filename
sanitized - which answers "is WeKan defending itself" and not the question an
admin asks next: who did that, and from where. It also could not tell a browser
that got confused from somebody working through the Hall of Fame one entry at a
time.
A CANARY is a tripwire at a point that only a permission-override attempt
reaches. Ordinary use never gets there - the code path runs when a request asks
for something the rules forbid - so a trip is not noise, and it is worth an
admin's attention with the actor attached. Design:
docs/Security/Remediation/WeKan.md §12.
Three properties, each enforced by a test:
SILENT. Tripping one changes NOTHING the caller can observe. tripCanary()
always returns false and tripCanaryDeny() always returns true, so a call site
reads as the refusal it replaces:
if (forbidden) return false; // before
if (forbidden) return tripCanary('card.vote-field', { userId });
The return value is decided before any reporting is attempted and the reporting
is wrapped, so a canary can never throw into, delay or disturb the request it
watches. The REST handler re-throws the ORIGINAL error object. This is not
politeness: a canary that announces itself is a map of which paths are watched,
and a probe would simply avoid them.
BOUNDED. A canary sits where an attacker can loop, so one row per attempt would
be a denial of service they get for free. models/lib/canaryTokens.js - pure, no
Meteor, no clock of its own - records the first trip of a (canary, actor) pair
at once, COUNTS the rest of the window, flushes one summary carrying the total
when the window closes, and stops writing after 60 summaries for a pair that
never gives up. The tracked-pair map is capped at 5000 and evicts the LEAST
RECENTLY seen, so a long-running attacker is not pushed out by a passing one and
a botnet varying its address cannot grow it. Idle pairs are swept on a timer. A
thousand attempts in a minute cost ONE row, and a suppressed trip costs one map
lookup. The username lookup is cached and capped, and neither it nor the insert
is awaited on the caller's path.
ATTRIBUTED. Every event carries userId, username, ip and count. The username is
denormalised at write time on purpose: it is what the account was CALLED when it
tried, so a rename does not rewrite history and a deletion does not erase it.
The address uses the same spoofing-safe resolver as the login throttle -
X-Forwarded-For only as far as HTTP_FORWARDED_COUNT says to trust it - or an
attacker could write a colleague's address into the security log by sending a
header. `detail` says what was attempted in the words of the feature, never the
payload.
Seventeen canaries are wired at the permission checks that refuse the attempts
behind BoardBleed, ParentBleed, ChecklistBleed, PathBleed and CommentBleed, plus
the direct vote/poker field writes and a stored path that points outside the
storage root. All server-side: no Playwright, no browser, nothing to install.
Admin Panel / Problems / Security gains Username, IP address and Attempts
columns, and both new columns are searchable - the thing an admin does with one
security event is pivot on it.
tests/canaryTokens.test.cjs (25 checks) drives the limiter through bursts, an
endless probe, eviction order and junk input; tests/canaryCoverage.test.cjs (18)
pins every canary to its call site and asserts none of them changes what the
caller gets back.
Thanks to xet7 !
55 lines
2.4 KiB
JavaScript
55 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
// General "sanitize known exploits from an uploaded file at its staging location,
|
|
// BEFORE it is promoted to the default storage (filesystem final location, S3,
|
|
// GridFS, ...)". Centralizes the exploit-removal step so every upload path
|
|
// (attachments, avatars) runs the same sanitization in the same place.
|
|
//
|
|
// Today this strips JavaScript (<script>, event handlers, javascript: URIs,
|
|
// embedded HTML) and XML-loop / billion-laughs DOCTYPE/ENTITY constructs from SVG
|
|
// uploads, in place, so the image still uploads but cannot execute script or
|
|
// expand entities. The file's on-disk bytes at version.path are rewritten while it
|
|
// is still on the local filesystem staging area; only after this returns should
|
|
// the caller move the file to its final storage backend.
|
|
|
|
const { isSvgFile, sanitizeSvgFileSync } = require('./sanitizeSvg');
|
|
|
|
// Sanitize known exploits from every version of fileObj in place. Returns true if
|
|
// any version's bytes were modified (so the caller can persist the new sizes).
|
|
function sanitizeUploadedFileExploits(fileObj) {
|
|
if (!fileObj || !fileObj.versions) return false;
|
|
let changed = false;
|
|
|
|
// SVG: remove script / event handlers / javascript: URIs / XML DOCTYPE/ENTITY.
|
|
if (isSvgFile(fileObj.type, fileObj.name)) {
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
|
const version = fileObj.versions[versionName];
|
|
const newSize = sanitizeSvgFileSync(version && version.path);
|
|
if (newSize !== null) {
|
|
version.size = newSize;
|
|
changed = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
// Active markup had to be REMOVED from an uploaded file - a script, an event
|
|
// handler, a javascript: URI inside an SVG. Nothing a drawing program produces
|
|
// needs any of it, so this is a stored-XSS attempt that was defused rather than
|
|
// a file that needed tidying, and it trips a canary with the uploader attached
|
|
// (docs/Security/Remediation/WeKan.md §12.6). The sanitized file is stored
|
|
// exactly as before; the uploader is told nothing.
|
|
if (changed) {
|
|
try {
|
|
const { tripCanary } = require('/server/lib/canary');
|
|
tripCanary('sanitize.dangerous-content', {
|
|
userId: (fileObj.userId || (fileObj.meta && fileObj.meta.userId)) || undefined,
|
|
detail: 'active markup removed from an uploaded ' + (fileObj.type || 'file'),
|
|
});
|
|
} catch (e) { /* a canary must never break an upload */ }
|
|
}
|
|
|
|
return changed;
|
|
}
|
|
|
|
module.exports = { sanitizeUploadedFileExploits };
|