fix(json-to-go):Security Hotspots

This commit is contained in:
zxysilent 2023-04-07 19:44:25 +08:00
parent 46df1bcf77
commit e3ea35ba18
3 changed files with 16 additions and 12 deletions

View file

@ -2,10 +2,10 @@ import { ArrowsShuffle } from '@vicons/tabler';
import { defineTool } from '../tool'; import { defineTool } from '../tool';
export const tool = defineTool({ export const tool = defineTool({
name: 'Json to go', name: 'JSON to GO',
path: '/json-to-go', path: '/json-to-go',
description: '', description: '',
keywords: ['json', 'go'], keywords: ['json', 'JSON', 'go'],
component: () => import('./json-to-go.vue'), component: () => import('./json-to-go.vue'),
icon: ArrowsShuffle, icon: ArrowsShuffle,
createdAt: new Date('2023-04-07'), createdAt: new Date('2023-04-07'),

View file

@ -10,11 +10,11 @@
</n-space> </n-space>
</div> </div>
<n-form-item label="Your Json"> <n-form-item label="Your JSON">
<n-input <n-input
ref="inputElement" ref="inputElement"
v-model:value="rawSQL" v-model:value="rawJSON"
placeholder="Put your Json..." placeholder="Put your JSON..."
type="textarea" type="textarea"
rows="20" rows="20"
autocomplete="off" autocomplete="off"
@ -41,13 +41,12 @@ const config = reactive<Partial<FormatFnOptions>>({
inline: false, inline: false,
omitempty: false, omitempty: false,
}); });
const rawJSON = ref('');
const rawSQL = ref('');
// function jsonToGo(json, typename, flatten = true, example = false, allOmitempty = false) // function jsonToGo(json, typename, flatten = true, example = false, allOmitempty = false)
const goCode = computed(() => { const goCode = computed(() => {
let result = jsonToGo(rawSQL.value, '', config.inline, false, config.omitempty); let result = jsonToGo(rawJSON.value, '', config.inline, false, config.omitempty);
if (result.error) { if (result.error) {
return rawSQL ? '' : result.error; return !rawJSON.value ? '' : result.error;
} }
return result.go; return result.go;
}); });

View file

@ -6,7 +6,12 @@
A simple utility to translate JSON into a Go type definition. A simple utility to translate JSON into a Go type definition.
*/ */
const cryptoRand = () => {
const crypto = window.crypto || window.msCrypto;
const randomBuffer = new Uint32Array(1);
crypto.getRandomValues(randomBuffer);
return randomBuffer[0] / (0xffffffff + 1); // 0xFFFFFFFF = uint32.MaxValue (+1 because Math.random is inclusive of 0, but not 1)
};
function jsonToGo(json, typename, flatten = true, example = false, allOmitempty = false) { function jsonToGo(json, typename, flatten = true, example = false, allOmitempty = false) {
let data; let data;
let scope; let scope;
@ -20,7 +25,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
let parent = ''; let parent = '';
try { try {
data = JSON.parse(json.replace(/(:\s*\[?\s*-?\d*)\.0/g, '$1.1')); // hack that forces floats to stay as floats data = JSON.parse(json.replace(/(:\s{0,128}\[?\s{0,128}-?\d{0,128})\.0/g, '$1.1')); // hack that forces floats to stay as floats
scope = data; scope = data;
} catch (e) { } catch (e) {
return { return {
@ -360,7 +365,7 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
function uuidv4() { function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0, let r = (cryptoRand() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8; v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16); return v.toString(16);
}); });