Skip to content

🔴 4. Fragile String Parsing #158

@juancolchete

Description

@juancolchete

Location: templates/add_area.html:407-414

let issueKeys = ["Community name : ","Location: ","Associated areas: ","GeoJSON: ","Icon URL: ","Lightning: ","Social links: ","Community leader contact: ","Notes: ","Created at: "]
for(let i=0;i< issueKeys.length-1; i++){
  issueData[issueKeysNames[i]] = issueText.substring(issueText.indexOf(issueKeys[i])+issueKeys[i].length,issueText.indexOf(issueKeys[i+1]))
}

Problems:

  • indexOf() returns -1 if key not found → produces incorrect substring
  • Assumes exact field order and presence
  • No validation that data was extracted
  • Fails silently with corrupted data

Fix:

async function treatIssueData(issueText){
    const issueData = {}
    const issueTemplate = {
        "Community name : ": "communityName",
        "Location: ": "location",
        "Associated areas: ": "associatedAreas",
        "GeoJSON: ": "geoJSON",
        "Icon URL: ": "icon",
        "Lightning: ": "lightningTips",
        "Social links: ": "contact",
        "Community leader contact: ": "leaderContact",
        "Notes: ": "description"
    }
    
    const keys = Object.keys(issueTemplate)
    
    for (let i = 0; i < keys.length; i++) {
        const startKey = keys[i]
        const endKey = keys[i + 1] || "Created at: "
        const fieldName = issueTemplate[startKey]
        
        const startIdx = issueText.indexOf(startKey)
        if (startIdx === -1) {
            console.warn(`Issue template missing field: ${startKey}`)
            issueData[fieldName] = ""
            continue
        }
        
        const endIdx = issueText.indexOf(endKey, startIdx + startKey.length)
        if (endIdx === -1) {
            issueData[fieldName] = issueText.substring(startIdx + startKey.length).trim()
        } else {
            issueData[fieldName] = issueText.substring(startIdx + startKey.length, endIdx).trim()
        }
    }
    
    return issueData
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions