Can you see the security vulnerability in the following snippet of code?
string returnValue = String.Empty;
string sql =
"select description from products where prodID = '"
+ Request.Params["pid"] + "';";
SqlCommand sqlcmd = new SqlCommand(sql);
sqlcmd.Connection = sqlConn;
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
returnValue = (string)sdr[0];
}
sdr.Close();
return returnValue;
Wednesday, April 29, 2009
Security Vulnerability
Thursday, April 9, 2009
Powershell script to remove empty directories
Here's a Powershell script that I've just created to remove empty folders which I'm sure I'm not going to need again in the future.
$items = Get-ChildItem -Recurse
foreach($item in $items)
{
if( $item.PSIsContainer )
{
$subitems = Get-ChildItem -Recurse -Path $item.FullName
if($subitems -eq $null)
{
"Remove item: " + $item.FullName
Remove-Item $item.FullName
}
$subitems = $null
}
}
Powershell Copy-Item does not honor the exclude parameter
I've been having a hard time getting Powershell's Copy-Item to work for me. My problem is that the -exclude parameter for the Copy-Item only honors the excluded items for the root directory in the destination but not for sub-directories if you also include the recurse parameter.
I believe that I have finally solved the problem with this snippet of powershell:
$source = 'd:\t1'
$dest = 'd:\t2'
$exclude = @('*.pdb','*.config')
$items = Get-ChildItem $source -Recurse -Exclude $exclude
foreach($item in $items)
{
$target = Join-Path $dest $item.FullName.Substring($source.length)
if( -not( $item.PSIsContainer -and (Test-Path($target))))
{
Copy-Item -Path $item.FullName -Destination $target
}
}
Tuesday, March 31, 2009
Defaulting a class to public from Visual Studio
class Class1
{
}
What I want is the class to be created as such:
public class Class1
{
}
And I know that I'm not alone because when I ask my co-workers they agree. I asked this question on StackOverlow and was pointed in the right direction. On my 64 bit machine I still haven't solved this problem but on my 32-bit XP machine I have.
I first edited the Class.cs file inside the C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip package and added the public modifier to the class.
This didn't work.
I then found this folder: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip and did the same addition on the Class.cs file in this folder and it worked.
I'm not sure if I can blow away this cache version of the ItemTemplates and it would be re-created? I didn't want to try because my development environment is too brittle to mess with at this level. I would guess that at some point the cache (if it is a cache) may become invalidated and so the first edit will be necessary and prevail in the long term.
The prefix cannot be redefined from within the same start element tag
The following snippet of code had me stumped for a while:
XDocument xDoc = new XDocument(new XDeclaration("1.0",
"utf-8", "yes"),
new XComment("This is a comment"),
new XElement("elementName",
new XAttribute("xmlns", "http://fake/namespace"),
new XAttribute("version", "1.00"),
new XElement("innerelement",
new XAttribute("myAttribute", "att_value"))
));
// The prefix '' cannot be redefined from ''
// to 'http://fake/namespace'
// within the same start element tag..
string test = xDoc.ToString();
I was getting a "The prefix '' cannot be redefined from '' to 'http://fake/namespace' within the same start element tag.." when running this.
The solution is to define the namespace before creating the XML document and prefixing this namespace to all elements that you create. Here is the working code.
XNamespace xn = "http://fake/namespace";
XDocument xDoc = new XDocument(new XDeclaration("1.0",
"utf-8", "yes"),
new XComment("This is a comment"),
new XElement(xn + "elementName",
new XAttribute("version", "1.00"),
new XElement(xn + "innerelement",
new XAttribute("myAttribute", "att_value"))
));
string test = xDoc.ToString();
Saturday, March 28, 2009
Hybrid ASP.NET MVC and WebForms app
I'm in the process of adding ASP.NET MVC to an existing ASP.NET WebForms app in Visual Studio 2008. One of the neat features of an ASP.NET MVC app is that when you right click on the Controllers folder in the Solution Explorer the "Add" menu item has a "Controller..." menu item on it and right clicking on the Views folder causes the "Add" menu item to have a "View..." sub-menu item on it.
It took me forever to work out how to get my WebForms app to also show those menu items. It turns out that you have to edit the .csproj file for the WebForms app and find the section that reads:
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
The to that add the following GUID:
{603c0e0b-db56-11dc-be95-000d561079b0};
Such that it now reads as follows:
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Wednesday, March 25, 2009
Improving my JavaScript
I've been listening to Douglas Crockford's The JavaScript Programming Language at the YUI Theater and have learned a number of interesting things that I wanted to note before I forget them.
JavaScript Values: Numbers (64-bit floating point), Strings, Booleans, Objects, null, undefined
False values: false, null, undefined, "" (empty string), 0, NaN
True values: everything else, e.g. "0", "false"
== and != do coercion before comparing (e.g. different types can be considered the same)
=== and !== do not do coercion and the type as well as value must be equal
&& returns the second operand if the first is true otherwise it returns the first operand, e.g.
if(a) { return a.member; } else { return a; }
can be written as
return a && a.member;
|| returns the first operand if it is true otherwise the second as such it can be used to fill in default values, e.g.
var last = input || nr_items;
labelled breaks, a for() loop can be labelled and then a break statement can refer to that label.