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

Feature/flint concise prefix #180

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions paima-concise/src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EncodingVersion } from './types.js';
import { isHexString } from './utils.js';
import { separator } from './v1/consts.js';
import { toConciseValue } from './v1/utils.js';
import { checkSecurityPrefix, stripSecuirtyPrefix } from './security.js';

const initializeSpecific = (input: string, version: EncodingVersion): ConciseConsumer => {
const { conciseValues, concisePrefix, conciseInput } = preParse(input, version);
Expand Down Expand Up @@ -62,6 +63,12 @@ const preParse = (input: string, version: EncodingVersion): ConciseConsumerInter
return getEmptyInternals();
}

if (!checkSecurityPrefix(conciseInput)) {
// Invalid input, discard entire message.
return getEmptyInternals();
}
conciseInput = stripSecuirtyPrefix(conciseInput);

const [inputPrefix, ...stringValues] = conciseInput.split(separator);
const hasImplicitUser = inputPrefix.match(/^@(\w+)/);
if (hasImplicitUser) {
Expand Down
2 changes: 1 addition & 1 deletion paima-concise/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { builder } from './builder.js';
export { consumer } from './consumer.js';

export { checkSecurityPrefix, stripSecuirtyPrefix } from './security.js';
export * from './types';
25 changes: 25 additions & 0 deletions paima-concise/src/security.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ENV } from '@paima/utils';
import { separator } from './v1/consts';

// For security, we add the .env CONCISE_GAME_NAME to the string.
export const getSecurityPrefix = (): string => {
return ENV.CONCISE_GAME_NAME
? `${separator}${ENV.CONCISE_GAME_NAME}${separator}`
: '';
};

export const checkSecurityPrefix = (conciseInput: string): boolean => {
const securityPrefix = getSecurityPrefix();
if (securityPrefix) {
return conciseInput.startsWith(securityPrefix);
}
return true;
};

export const stripSecuirtyPrefix = (conciseInput: string): string => {
const securityPrefix = getSecurityPrefix();
if (securityPrefix) {
return conciseInput.slice(securityPrefix.length);
}
return conciseInput;
};
5 changes: 3 additions & 2 deletions paima-concise/src/v1/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ConciseValue, UTF8String } from '../types.js';
import { separator, stateIdentifier } from './consts.js';
import { getSecurityPrefix } from '../security.js';

const toString = (val: ConciseValue): string => {
return val.isStateIdentifier ? `${stateIdentifier}${val.value}` : val.value;
Expand All @@ -9,9 +10,9 @@ const build = (concisePrefix: string, conciseValues: ConciseValue[]): UTF8String
if (!concisePrefix) {
throw new Error(`Missing prefix value in concise builder for input: ${conciseValues}`);
}

const securityPrefix = getSecurityPrefix();
const conciseValueInput = conciseValues.map(toString).join(separator);
const conciseInput = `${concisePrefix}${separator}${conciseValueInput}`;
const conciseInput = `${securityPrefix}${concisePrefix}${separator}${conciseValueInput}`;

return conciseInput;
};
Expand Down
15 changes: 13 additions & 2 deletions paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Prando from '@paima/prando';

import { randomnessRouter } from './randomness.js';
import { cdeTransitionFunction, getProcessedCdeDatumCount } from './cde-processing.js';
import { checkSecurityPrefix, stripSecuirtyPrefix } from '@paima/concise';

const SM: GameStateMachineInitializer = {
initialize: (
Expand Down Expand Up @@ -214,7 +215,10 @@ async function processScheduledData(
for (const data of scheduledData) {
const inputData: SubmittedData = {
userAddress: SCHEDULED_DATA_ADDRESS,
inputData: data.input_data,
// security prefix is optional in scheduled data.
inputData: checkSecurityPrefix(data.input_data)
? stripSecuirtyPrefix(data.input_data)
: data.input_data,
inputNonce: '',
suppliedValue: '0',
scheduled: true,
Expand Down Expand Up @@ -261,10 +265,17 @@ async function processUserInputs(
doLog(`Skipping inputData with duplicate nonce: ${inputData}`);
continue;
}
if (!checkSecurityPrefix(inputData.inputData)) {
doLog(`Skipping inputData with invalid security prefix: ${inputData}`);
continue;
}

// Trigger STF
const sqlQueries = await gameStateTransition(
inputData,
{
...inputData,
inputData: stripSecuirtyPrefix(inputData.inputData),
},
latestChainData.blockNumber,
randomnessGenerator,
readonlyDBConn
Expand Down
7 changes: 7 additions & 0 deletions paima-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export class ENV {
static get ENABLE_DRY_RUN(): boolean {
return process.env.ENABLE_DRY_RUN === 'true';
}
static get CONCISE_GAME_NAME(): string {
const prefix = process.env.CONCISE_GAME_NAME || '';
if (prefix && prefix.match(/\|/)) {
throw new Error('Concise security prefix cannot contain the pipe character.');
}
return prefix;
}

// Middleware config:
static get BACKEND_URI(): string {
Expand Down