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

Add support for multiple QUERY patterns in Value utility #464

Merged
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
6 changes: 3 additions & 3 deletions apps/acf-options-page/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const APP_LINK = {
BLOG: 'https://blog.getautoclicker.com/',
CONFIGS: 'https://configs.getautoclicker.com/',
TEST: 'https://test.getautoclicker.com/',
ISSUES: 'https://github.com/Dhruv-Techapps/acf-docs/issues',
DISCUSSIONS: 'https://github.com/Dhruv-Techapps/acf-docs/discussions',
ISSUES: 'https://github.com/Dhruv-Techapps/auto-clicker-auto-fill/issues',
DISCUSSIONS: 'https://github.com/Dhruv-Techapps/auto-clicker-auto-fill/discussions',
};
// Application Languages
export const APP_LANGUAGES = ['en', 'ar', 'de', 'es', 'fi', 'fr', 'id', 'it', 'ja', 'ko', 'nl', 'pt', 'ru', 'sv', 'vi', 'zh_CN'];
Expand Down Expand Up @@ -35,7 +35,7 @@ export const SOCIAL_LINKS = {
DISCORD: 'https://discord.gg/ubMBeX3',
GOOGLE_GROUP: 'https://groups.google.com/g/auto-clicker-autofill',
TWITTER: `https://twitter.com/intent/tweet?text=${message}&url=${url}`,
GITHUB: 'https://github.com/Dhruv-Techapps/acf-docs',
GITHUB: 'https://github.com/Dhruv-Techapps/auto-clicker-auto-fill',
FACEBOOK: `https://www.facebook.com/sharer.php?u=${url}&quote=${message}`,
WHATSAPP: `https://wa.me/?text=${message}%5Cn%20${url}`,
RATE_US: `https://chromewebstore.google.com/detail/${extensionId}/reviews`,
Expand Down
2 changes: 1 addition & 1 deletion apps/acf-options-page/src/store/blog/blog.api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createAsyncThunk } from '@reduxjs/toolkit';

export const blogCheckAPI = createAsyncThunk('blog/check', async (version: string) => {
const response = await fetch(`https://api.github.com/repos/Dhruv-Techapps/acf-docs/releases/tags/v${version}`);
const response = await fetch(`https://api.github.com/repos/Dhruv-Techapps/auto-clicker-auto-fill/releases/tags/v${version}`);
if (response.status === 200) {
const release = await response.json();
return release;
Expand Down
27 changes: 26 additions & 1 deletion libs/acf/util/src/lib/value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe('getValue', () => {

it('should handle RANDOM pattern correctly', async () => {
const result = await Value.getValue('<random[a-zA-Z]{5}>');
console.log(result);
expect(result).toMatch(/[a-zA-Z]{5}/);
});

Expand All @@ -35,4 +34,30 @@ describe('getValue', () => {
const result = await Value.getValue('<sessionCount>');
expect(result).toBe('10');
});

it('should handle multiple QUERY patterns correctly', async () => {
const searchParams = new URLSearchParams();
searchParams.set('param1', 'value1');
searchParams.set('param2', 'value2');
const originalSearch = window.location.search;
window.history.replaceState({}, '', `${window.location.pathname}?${searchParams.toString()}`);

const result = await Value.getValue('<query::param1> and <query::param2>');
expect(result).toBe('value1 and value2');

window.history.replaceState({}, '', `${window.location.pathname}${originalSearch}`);
});

it('should handle multiple QUERY patterns correctly with sanitization and validation', async () => {
const searchParams = new URLSearchParams();
searchParams.set('param1', 'value1');
searchParams.set('param2', 'value2<script>alert(1)</script>');
const originalSearch = window.location.search;
window.history.replaceState({}, '', `${window.location.pathname}?${searchParams.toString()}`);

const result = await Value.getValue('<query::param1> and <query::param2>');
expect(result).toBe('value1 and param2');

window.history.replaceState({}, '', `${window.location.pathname}${originalSearch}`);
});
});
34 changes: 32 additions & 2 deletions libs/acf/util/src/lib/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

export const VALUE_MATCHER = {
QUERY_PARAM: /^Query::/i,
QUERY: /<query::(.*?)>/gi,
API: /^Api::/i,
RANDOM: /<random(.+?)(\/[a-z]+)?>/gi,
BATCH_REPEAT: /<batchRepeat>/,
Expand All @@ -38,15 +39,41 @@

const getSessionCount = (value: string) => value.replaceAll('<sessionCount>', String(window.__sessionCount));

const sanitizeInput = (input: string): string => {
const element = document.createElement('div');
element.textContent = input;
return element.innerHTML;
};

const validateQueryParam = (key: string, value: string): boolean => {
const pattern = /^[a-zA-Z0-9_-]+$/;
return pattern.test(key) && pattern.test(value);
};

const getQueryParam = (value: string) => {
const [, key] = value.split('::');
const searchParams = new URLSearchParams(window.location.search);
const [, key] = value.split('::');
if (searchParams.has(key)) {
value = searchParams.get(key) || key;
const paramValue = searchParams.get(key) ?? key;
if (validateQueryParam(key, paramValue)) {
value = sanitizeInput(paramValue);
}
}
return value;
};

const getMultiQueryParam = (value: string) => {
const searchParams = new URLSearchParams(window.location.search);
value = value.replace(VALUE_MATCHER.QUERY, (_, key) => {
const paramValue = searchParams.get(key) ?? key;
if (validateQueryParam(key, paramValue)) {
return sanitizeInput(paramValue);
}
return key;
});
Dismissed Show dismissed Hide dismissed
return value;
};

const getApiValue = (value: string): string => {
const [, key] = value.split('::');
const apiValue = window.__api?.[key];
Expand Down Expand Up @@ -76,6 +103,9 @@
if (VALUE_MATCHER.QUERY_PARAM.test(value)) {
value = getQueryParam(value);
}
if (VALUE_MATCHER.QUERY.test(value)) {
Dismissed Show dismissed Hide dismissed
value = getMultiQueryParam(value);
}
if (VALUE_MATCHER.BATCH_REPEAT.test(value)) {
value = getBatchRepeat(value);
}
Expand Down
6 changes: 3 additions & 3 deletions libs/ui/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export const APP_LINK = {
BLOG: 'https://blog.getautoclicker.com/',
CONFIGS: 'https://gist.github.com/dharmesh-hemaram',
TEST: 'https://test.getautoclicker.com/',
ISSUES: 'https://github.com/Dhruv-Techapps/acf-docs/issues',
DISCUSSIONS: 'https://github.com/Dhruv-Techapps/acf-docs/discussions',
ISSUES: 'https://github.com/Dhruv-Techapps/auto-clicker-auto-fill/issues',
DISCUSSIONS: 'https://github.com/Dhruv-Techapps/auto-clicker-auto-fill/discussions',
};
// Application Languages
export const APP_LANGUAGES = ['en', 'ar', 'de', 'es', 'fi', 'fr', 'id', 'it', 'ja', 'ko', 'nl', 'pt', 'ru', 'sv', 'vi', 'zh_CN'];
Expand Down Expand Up @@ -35,7 +35,7 @@ export const SOCIAL_LINKS = {
DISCORD: 'https://discord.gg/ubMBeX3',
GOOGLE_GROUP: 'https://groups.google.com/g/auto-clicker-autofill',
TWITTER: `https://twitter.com/intent/tweet?text=${message}&url=${url}`,
GITHUB: 'https://github.com/Dhruv-Techapps/acf-docs',
GITHUB: 'https://github.com/Dhruv-Techapps/auto-clicker-auto-fill',
FACEBOOK: `https://www.facebook.com/sharer.php?u=${url}&quote=${message}`,
WHATSAPP: `https://wa.me/?text=${message}%5Cn%20${url}`,
RATE_US: `https://chromewebstore.google.com/detail/${extensionId}/reviews`,
Expand Down