src/cache/server/cache-server.js
function reportResult (body) {
	// Reject invalid reports.
	if (!body) {
		log(`reportResult: no body`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Required fields.
	let isInvalid = false;
	['pattern', 'language', 'result'].forEach((f) => {
		if (!body.hasOwnProperty(f) || body[f] === null) {
			isInvalid = true;
		}
	});
	if (isInvalid) {
		log(`reportResult: invalid: ${jsonStringify(body)}`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Supported results.
	if (body.result === PATTERN_UNKNOWN || body.result === PATTERN_SAFE || body.result === PATTERN_VULNERABLE) {
	} else {
		log(`reportResult: invalid result ${body.result}`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Vulnerable must include proof.
	if (body.result === PATTERN_VULNERABLE && !body.hasOwnProperty('evilInput')) {
		log(`reportResult: ${body.result} but no evilInput`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Malicious client could spam us with already-solved requests.
	// Check if we know the answer already.
	return isVulnerable(body)
		.then((result) => {
			if (result !== PATTERN_UNKNOWN) {
				log(`reportResult: already known. Malicious client, or racing clients?`);
				return Promise.resolve(result);
			}
			// New pattern, add to dbUploadCollectionName.
			log(`reportResult: new result, updating dbUploadCollectionName`);
			return MongoClient.connect(dbUrl)
				.then((client) => {
					const db = client.db(dbName);
					log(`reportResult: connected, now updating DB for {${body.pattern}, ${body.language}} with ${body.result}`);
					return collectionUpdate(db.collection(dbUploadCollectionName), {pattern: body.pattern, language: body.language, result: body.result, evilInput: body.evilInput})
						.then((result) => {
							client.close();
							return result;
						})
						.catch((e) => {
							log(`reportResult: db error: ${e}`);
							client.close();
							return Promise.resolve(PATTERN_UNKNOWN);
						});
				})
				.catch((e) => {
					log(`reportResult: db error: ${e}`);
					return Promise.resolve(PATTERN_UNKNOWN);
				});
		});
}
function reportResult (body) {
	// Reject invalid reports.
	if (!body) {
		log(`reportResult: no body`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Required fields.
	let isInvalid = false;
	['pattern', 'language', 'result'].forEach((f) => {
		if (!body.hasOwnProperty(f) || body[f] === null) {
			isInvalid = true;
		}
	});
	if (isInvalid) {
		log(`reportResult: invalid: ${jsonStringify(body)}`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Supported results.
	if (body.result === PATTERN_UNKNOWN || body.result === PATTERN_SAFE || body.result === PATTERN_VULNERABLE) {
	} else {
		log(`reportResult: invalid result ${body.result}`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Vulnerable must include proof.
	if (body.result === PATTERN_VULNERABLE && !body.hasOwnProperty('evilInput')) {
		log(`reportResult: ${body.result} but no evilInput`);
		return Promise.resolve(PATTERN_INVALID);
	}

	// Malicious client could spam us with already-solved requests.
	// Check if we know the answer already.
	return isVulnerable(body)
		.then((result) => {
			if (result !== PATTERN_UNKNOWN) {
				log(`reportResult: already known. Malicious client, or racing clients?`);
				return Promise.resolve(result);
			}
			// New pattern, add to dbUploadCollectionName.
			log(`reportResult: new result, updating dbUploadCollectionName`);
			return MongoClient.connect(dbUrl)
				.then((client) => {
					const db = client.db(dbName);
					log(`reportResult: connected, now updating DB for {${body.pattern}, ${body.language}} with ${body.result}`);
					return collectionUpdate(db.collection(dbUploadCollectionName), {pattern: body.pattern, language: body.language, result: body.result, evilInput: body.evilInput})
						.then((result) => {
							client.close();
							return result;
						})
						.catch((e) => {
							log(`reportResult: db error: ${e}`);
							client.close();
							return Promise.resolve(PATTERN_UNKNOWN);
						});
				})
				.catch((e) => {
					log(`reportResult: db error: ${e}`);
					return Promise.resolve(PATTERN_UNKNOWN);
				});
		});
}
function reportResult ( body ) { // Reject invalid reports. if ( ! body ) { log ( ` ` ) ; return Promise . resolve ( PATTERN_INVALID ) ; } // Required fields. let isInvalid = false ; [ 'pattern' , 'language' , 'result' ] . forEach ( ( f ) => { if ( ! body . hasOwnProperty ( f ) || body [ f ] === null ) { isInvalid = true ; } } ) ; if ( isInvalid ) { log ( ` ${ jsonStringify ( body ) } ` ) ; return Promise . resolve ( PATTERN_INVALID ) ; } // Supported results. if ( body . result === PATTERN_UNKNOWN || body . result === PATTERN_SAFE || body . result === PATTERN_VULNERABLE ) { } else { log ( ` ${ body . result } ` ) ; return Promise . resolve ( PATTERN_INVALID ) ; } // Vulnerable must include proof. if ( body . result === PATTERN_VULNERABLE && ! body . hasOwnProperty ( 'evilInput' ) ) { log ( ` ${ body . result } ` ) ; return Promise . resolve ( PATTERN_INVALID ) ; } // Malicious client could spam us with already-solved requests. // Check if we know the answer already. return isVulnerable ( body ) . then ( ( result ) => { if ( result !== PATTERN_UNKNOWN ) { log ( ` ` ) ; return Promise . resolve ( result ) ; } // New pattern, add to dbUploadCollectionName. log ( ` ` ) ; return MongoClient . connect ( dbUrl ) . then ( ( client ) => { const db = client . db ( dbName ) ; log ( ` ${ body . pattern } ${ body . language } ${ body . result } ` ) ; return collectionUpdate ( db . collection ( dbUploadCollectionName ) , { pattern : body . pattern , language : body . language , result : body . result , evilInput : body . evilInput } ) . then ( ( result ) => { client . close ( ) ; return result ; } ) . catch ( ( e ) => { log ( ` ${ e } ` ) ; client . close ( ) ; return Promise . resolve ( PATTERN_UNKNOWN ) ; } ) ; } ) . catch ( ( e ) => { log ( ` ${ e } ` ) ; return Promise . resolve ( PATTERN_UNKNOWN ) ; } ) ; } ) ; }
Returns a Promise that resolves to one of the PATTERN_X results.
Returns a Promise that resolves to one of the PATTERN_X results .