Among those learning how to program, and some more experienced software developers, it's common practice to copy and paste code snippets from Stack Overflow, a Q&A forum for asking about coding problems.
There's even a faux O'Reilly-styled book of sorts, "Copying and Pasting from Stack Overflow," to highlight the practice, which turns out to be not just lazy but also a security risk.
In a research paper submitted to pre-print service ArXiv, six computer science boffins who hail from Shiraz University, Iran, Polytechnique Montreal University, Quebec, Canada, and Chamran University, Iran – Morteza Verdi, Ashkan Sami, Jafar Akhondali, Foutse Khomh, Gias Uddin, and Alireza Karami Motlagh – say that they looked at more than 72,000 C++ code snippets in 1,325 Stack Overflow posts and found 69 vulnerable snippets of 29 different types.
That's not a lot in absolute terms but those 69 vulnerable snippets show up in 2,589 GitHub projects. The researchers say they notified the authors of affected projects and some, but not all, chose to fix the flaws, which consist of known CWEs.
The paper, "An Empirical Study of C++ Vulnerabilities in Crowd-Sourced Code Examples," is being reviewed for possible publication in the journal IEEE Transactions on Software Engineering.
In a phone interview with The Register, Ashkan Sami, associate professor of computer science, engineering, and information technology at Shiraz University in Iran, said the research represents an attempt to look at how flawed code migrates from Stack Overflow to GitHub.
"Basically, what we tried to show is that using Stack Overflow without reviewing it carefully can lead to potential vulnerabilities inside applications," said Sami.
The research echoes an academic paper from 2017 that found 1,161 insecure code snippets posted on Stack Overflow had been copied and pasted into 1.3m Android applications available on Google Play.
The boffins relied on a Stack Overflow data set called SOTorrent data-set Version 2018-09-23. It covers posts from 2008 through 2018 and contains some duplicate code snippets.
The researchers chose to focus on C++ because it's popular, particularly for embedded, resource-constrained programs and large, distributed systems. Vulnerabilities in such systems, they contend, are likely to have a significant impact.
The mod firing squad: Stack Exchange embroiled in 'he said, she said, they said' rowREAD MORE
"Stack Overflow is only about asking and answering questions and a lot of time when developers are responding to give an answer they may not be properly educated in terms of security or may assume that those asking questions will know what to do," said Gias Uddin, a computer science PhD from McGill University who's currently working in the tech industry. "But that's not a good assumption to make."
Sami voiced similar sentiments. "The people who are using Stack Overflow, they shouldn't trust it fully," he said. "It's better for programmers to do it the hard way and learn secure coding."
Even so, the researchers developed a Chrome extension to help developers be more mindful of security when copying and pasting Stack Overflow code snippets. The extension checks copied code against the CWE database and throws up an alert if the snippet is flawed. Uddin said the plan is to release it when the paper is formally published.
Uddin said he hopes this research not only improves the quality of answers at Stack Overflow but also serves to remind developers that there are flaws in code shared on social media. ®